jmpavlick/q/1.0.0

NOTE: the latest version is 1.1.0

Q

Combinators for navigating deeply-hierarchial nested values with optional properties.

The Q module is specialized to operating on values that are a Maybe a.

import Dict
import Q
import Time


type alias User =
    { profile :
        Maybe
            { authProvider : Maybe String
            , signInHistory :
                Result (List Time.Posix) (List Time.Posix)
            }
    }


users : Dict.Dict Int User
users =
    Dict.fromList []


toAuthProvider : Int -> Maybe String
toAuthProvider i =
    Q.qq .profile .authProvider <| Dict.get i users


toLastSignInSuccess : Int -> Maybe Time.Posix
toLastSignInSuccess i =
    Q.qqq .profile (Q.ope .signInHistory) List.head <|
        Dict.get i users
j : (a -> b) -> a -> Maybe b

lift an accessor function a -> b to a "q" (i.e., a -> Maybe b)

name : Maybe String
name =
    qqq .user .profile (j .name) <|
        Just
            { user =
                Just
                    { profile = { name = "John" } }
            }
ope : (a -> Result x b) -> a -> Maybe b

for when you need to scooch right past an error (becuase you can't dip it in ranch)

toLastSignInSuccess : Int -> Maybe Time.Posix
toLastSignInSuccess i =
    qqq .profile (ope .signInHistory) List.head <|
        Dict.get i users
nope : (a -> Result x b) -> a -> Maybe x

for when you need to schooch right past a success

-- Get the error message if sign in failed
qq .profile (nope .signInHistory) user == Just "rate limited"
q : (a -> Maybe b) -> Maybe a -> Maybe b

it's literally just Maybe.andThen

qq : (a -> Maybe b) -> (b -> Maybe c) -> Maybe a -> Maybe c

apply two "and-then-able" functions in order to a Maybe a

qqq : (a -> Maybe b) -> (b -> Maybe c) -> (c -> Maybe d) -> Maybe a -> Maybe d

apply three "and-then-able" functions in order to a Maybe a

(a -> Maybe b)
-> (b -> Maybe c)
-> (c -> Maybe d)
-> (d -> Maybe e)
-> Maybe a
-> Maybe e

apply four "and-then-able" functions in order to a Maybe a

(a -> Maybe b)
-> (b -> Maybe c)
-> (c -> Maybe d)
-> (d -> Maybe e)
-> (e -> Maybe f)
-> Maybe a
-> Maybe f

apply five "and-then-able" functions in order to a Maybe a

(a -> Maybe b)
-> (b -> Maybe c)
-> (c -> Maybe d)
-> (d -> Maybe e)
-> (e -> Maybe f)
-> (f -> Maybe g)
-> Maybe a
-> Maybe g

apply six "and-then-able" functions in order to a Maybe a

(a -> Maybe b)
-> (b -> Maybe c)
-> (c -> Maybe d)
-> (d -> Maybe e)
-> (e -> Maybe f)
-> (f -> Maybe g)
-> (g -> Maybe h)
-> Maybe a
-> Maybe h

apply seven "and-then-able" functions in order to a Maybe a

(a -> Maybe b)
-> (b -> Maybe c)
-> (c -> Maybe d)
-> (d -> Maybe e)
-> (e -> Maybe f)
-> (f -> Maybe g)
-> (g -> Maybe h)
-> (h -> Maybe i)
-> Maybe a
-> Maybe i

apply eight "and-then-able" functions in order to a Maybe a

(a -> Maybe b)
-> (b -> Maybe c)
-> (c -> Maybe d)
-> (d -> Maybe e)
-> (e -> Maybe f)
-> (f -> Maybe g)
-> (g -> Maybe h)
-> (h -> Maybe i)
-> (i -> Maybe j)
-> Maybe a
-> Maybe j

apply nine "and-then-able" functions in order to a Maybe a

(a -> Maybe b)
-> (b -> Maybe c)
-> (c -> Maybe d)
-> (d -> Maybe e)
-> (e -> Maybe f)
-> (f -> Maybe g)
-> (g -> Maybe h)
-> (h -> Maybe i)
-> (i -> Maybe j)
-> (j -> Maybe k)
-> Maybe a
-> Maybe k

apply ten "and-then-able" functions in order to a Maybe a

(a -> Maybe b)
-> (b -> Maybe c)
-> (c -> Maybe d)
-> (d -> Maybe e)
-> (e -> Maybe f)
-> (f -> Maybe g)
-> (g -> Maybe h)
-> (h -> Maybe i)
-> (i -> Maybe j)
-> (j -> Maybe k)
-> (k -> Maybe l)
-> Maybe a
-> Maybe l

apply eleven "and-then-able" functions in order to a Maybe a

q6 :
(a -> Maybe b)
-> (b -> Maybe c)
-> (c -> Maybe d)
-> (d -> Maybe e)
-> (e -> Maybe f)
-> (f -> Maybe g)
-> Maybe a
-> Maybe g

same as qqqqqq, just easier to read

q7 :
(a -> Maybe b)
-> (b -> Maybe c)
-> (c -> Maybe d)
-> (d -> Maybe e)
-> (e -> Maybe f)
-> (f -> Maybe g)
-> (g -> Maybe h)
-> Maybe a
-> Maybe h

same as qqqqqqq, just easier to read

q8 :
(a -> Maybe b)
-> (b -> Maybe c)
-> (c -> Maybe d)
-> (d -> Maybe e)
-> (e -> Maybe f)
-> (f -> Maybe g)
-> (g -> Maybe h)
-> (h -> Maybe i)
-> Maybe a
-> Maybe i

same as qqqqqqqq, just easier to read

q9 :
(a -> Maybe b)
-> (b -> Maybe c)
-> (c -> Maybe d)
-> (d -> Maybe e)
-> (e -> Maybe f)
-> (f -> Maybe g)
-> (g -> Maybe h)
-> (h -> Maybe i)
-> (i -> Maybe j)
-> Maybe a
-> Maybe j

same as qqqqqqqqq, just easier to read

q10 :
(a -> Maybe b)
-> (b -> Maybe c)
-> (c -> Maybe d)
-> (d -> Maybe e)
-> (e -> Maybe f)
-> (f -> Maybe g)
-> (g -> Maybe h)
-> (h -> Maybe i)
-> (i -> Maybe j)
-> (j -> Maybe k)
-> Maybe a
-> Maybe k

same as qqqqqqqqqq, just easier to read

q11 :
(a -> Maybe b)
-> (b -> Maybe c)
-> (c -> Maybe d)
-> (d -> Maybe e)
-> (e -> Maybe f)
-> (f -> Maybe g)
-> (g -> Maybe h)
-> (h -> Maybe i)
-> (i -> Maybe j)
-> (j -> Maybe k)
-> (k -> Maybe l)
-> Maybe a
-> Maybe l

same as qqqqqqqqqqq, just easier to read