jmpavlick/q/1.1.0

Q

Combinators for navigating deeply-hierarchical 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 (because 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 scooch 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.

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

Same as Q.qqqq, just easier to read.

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

Same as Q.qqqqq, jsut easier to read.

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 Q.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 Q.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 Q.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 Q.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 Q.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 Q.qqqqqqqqqqq, just easier to read.