Skip to content

Commit

Permalink
Add map2, map3, mapAll
Browse files Browse the repository at this point in the history
  • Loading branch information
arowM committed Jul 14, 2024
1 parent 2953496 commit 5a6b4b2
Showing 1 changed file with 62 additions and 3 deletions.
65 changes: 62 additions & 3 deletions src/Tepa/Next.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ module Tepa.Next exposing
, setKey, setKeyAndId, fullKeyNameFor, values, checks_
, valueFor, checkFor_
, checks, checkFor
, map2, map3, mapAll
)

{-| Provide functionality that will be introduced or changed in the next version.
### New functions for convenience
@map2, map3, mapAll
### Pseudo-namespace for keys
You may sometimes find yourself using an element many times in a page. In such cases, it is useful to give key a pseudo-namespace.
Expand Down Expand Up @@ -157,14 +163,67 @@ In this case, using `setKey context ".foo"` in `userNameForm` will actually give
@docs layerView
@docs ViewContext
@docs pushNamespace
@docs setKey, setKeyAndId, fullKeyNameFor, values, checks
@docs valueFor, checkFor
@docs setKey, setKeyAndId, fullKeyNameFor, values, checks_
@docs valueFor, checkFor_
DEPRECATED
@docs checks, checkFor
-}

import Dict exposing (Dict)
import Mixin
import Tepa
import Tepa exposing (Promise)


{-| Run two Promises concurrently, and append a function to the results when both are complete.
-}
map2 :
(a -> b -> c)
-> Promise m a
-> Promise m b
-> Promise m c
map2 f promA promB =
Tepa.succeed f
|> Tepa.sync promA
|> Tepa.sync promB


{-| Run three Promises concurrently, and append a function to the results when all are complete.
If you need to handle more Promises, use `mapAll` or `sync`.
-}
map3 :
(a -> b -> c -> d)
-> Promise m a
-> Promise m b
-> Promise m c
-> Promise m d
map3 f promA promB promC =
Tepa.succeed f
|> Tepa.sync promA
|> Tepa.sync promB
|> Tepa.sync promC


{-| Run Promises concurrently, and apply a function to the results when all are complete.
-}
mapAll :
(List a -> b)
-> List (Promise m a)
-> Promise m b
mapAll f ps =
List.foldl
(\p acc ->
Tepa.succeed (::)
|> Tepa.sync p
|> Tepa.sync acc
)
(Tepa.succeed [])
ps
|> Tepa.map f


{-| Context about the current View, including its namespace.
Expand Down

0 comments on commit 5a6b4b2

Please sign in to comment.