Convenience functions for working with Bools
all : List Basics.Bool -> Basics.Bool
All the bools are true.
all [ True, True, True ]
--> True
all [ True, False ]
--> False
all [ False, False ]
--> False
all []
--> True
none : List Basics.Bool -> Basics.Bool
None of the bools are true.
none [ True, True ]
--> False
none [ True, False ]
--> False
none [ False, False ]
--> True
none []
--> True
any : List Basics.Bool -> Basics.Bool
At least one of the bools is true.
any [ True, True ]
--> True
any [ True, False ]
--> True
any [ False, False ]
--> False
any []
--> False
notAll : List Basics.Bool -> Basics.Bool
Not all of them are true
notAll [ True, True ]
--> False
notAll [ True, False ]
--> True
notAll [ False, False ]
--> True
notAll []
--> False
ifElse : a -> a -> Basics.Bool -> a
Return the first argument if the given predicate is True
. Otherwise, return the second argument.
ifElse "yes" "no" True
--> "yes"
ifElse "yes" "no" False
--> "no"
allPass : List (a -> Basics.Bool) -> a -> Basics.Bool
Determine if all predicates are satisfied by the value.
allPass [ (>) 20, (<) 10 ] 11
--> True
allPass [ (>) 20, (<) 10 ] 21
--> False
allPass [ (>) 20, (<) 10 ] 4
--> False
allPass [] 21
--> True
anyPass : List (a -> Basics.Bool) -> a -> Basics.Bool
Determine if any predicate is satisfied by the value.
anyPass [ (>) 20, (<) 10 ] 100
--> True
anyPass [ (>) 20, (==) 10 ] 21
--> False
anyPass [] 21
--> False
toMaybe : a -> Basics.Bool -> Maybe a
Given a value a
and a Bool
, and wrap a
in Just
if the Bool
is True
.
toMaybe 4 True
--> Just 4
toMaybe 4 False
--> Nothing
This kind of function is handy when populating lists..
[ Bool.Extra.toMaybe adminRoute (User.isAdmin user)
, Just dashboardRoute
, Bool.Extra.toMaybe profile (User.isLoggedIn user)
]
|> List.filterMap identity
..or when generating errors during form validation..
{ field
| error =
Bool.Extra.toMaybe FieldIsEmpty (String.isEmpty field.value)
}
toString : Basics.Bool -> String
Turn a bool into a string
toString True
--> "True"
toString False
--> "False"
fromString : String -> Maybe Basics.Bool
Try and extract a Bool
from a String
fromString "true"
--> Just True
fromString "False"
--> Just False
fromString "t"
--> Nothing
fromString "My pal foot foot"
--> Nothing
stringDecoder : Json.Decode.Decoder Basics.Bool
Sometimes webservers will return the unideal json of a string "true"
rather than just the native boolean value true
. This decoder decodes a string that looks like a Bool
, into a Bool
import Json.Decode as Decode
import Json.Encode as Encode
Decode.decodeString stringDecoder "\"true\""
--> Ok True
Decode.decodeString stringDecoder "true"
--> Err (Decode.Failure "Expecting a STRING" (Encode.bool True))
encodeAsString : Basics.Bool -> Json.Encode.Value
Sometimes in weird unideal circumstances you need to encode True
to "true"
instead of just true
.
import Json.Encode exposing (encode)
encode 0 (encodeAsString True)
--> "\"true\""
encode 0 (encodeAsString False)
--> "\"false\""