flarebyte/bubblegum-entity/1.1.1

Bubblegum.Entity.Outcome

An outcome is a type which borrows concepts from both Elm Maybe and Result

Basics

type Outcome value
= Valid value
| None
| Warning String

Representation of an outcome which can be:

  • Valid: a valid value, similar to Just of Maybe.
  • None: no value, similar to Nothing of Maybe.
  • Warning: a warning message, similar to Err of Result.

If the outcome is Valid return the value. If the outcome is a None then return a given default value. If the outcome is a Warning then propagate this warning.

map : (a -> value) -> Outcome a -> Outcome value

Apply a function to an outcome. If the result is Valid, it will be converted. If the outcome is a Warning or None, the same value will propagate through. map sqrt (Valid 4.0) == Valid 2.0

map2 : (a -> b -> value) -> Outcome a -> Outcome b -> Outcome value

Apply a function to two outcomes, if both outcome are Valid. If not, a valid outcome and a None will propagate None, a Warning will always propagate. Two warnings will be merged

or : Outcome a -> Outcome a -> Outcome a

Like the boolean '||' this will return the first value that is positive ('Valid').

or None (Valid "str") -- Valid "str"
fromMaybe : Maybe a -> Outcome a

Convert a maybe to an outcome

fromMaybe (Just "str") -- Valid "str"


fromMaybe Nothing -- None
toMaybe : Outcome a -> Maybe a

Convert an outcome to a maybe

toMaybe (Valid "str") -- Just "str"


toMaybe None -- Nothing

Checking

check : (a -> Bool) -> String -> Outcome a -> Outcome a

Check that a valid outcome verifies the criteria otherwise raise a warning

check String.isEmpty "should not be empty string" (Valid "some text") -- Valid "some text"


check String.isEmpty "should not be empty string" (Valid "") -- Warning "should not be empty string"
checkOrNone : (a -> Bool) -> Outcome a -> Outcome a

Check that a valid outcome verifies the criteria otherwise return none

checkOrNone String.isEmpty (Valid "some text") -- Valid "some text"


checkOrNone String.isEmpty (Valid "") -- None

An outcome with a true value will produce a constant outcome

trueMapToConstant [ "alpha" ] (Valid True) -- Valid ["alpha"]


trueMapToConstant [ "alpha" ] (Valid False) -- None
isValid : Outcome a -> Bool

Return true if the outcome is valid

isNone : Outcome a -> Bool

Return true if the outcome is none

isWarning : Outcome a -> Bool

Return true if the outcome is none