-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Result.Extra.combineFoldl #60
Comments
Why not
|
@gampleman opinions? |
|
|
Yeah but |
withCombineFoldl : List String -> Result String (Dict String Int)
withCombineFoldl list =
-- Result.Extra.combineFoldl
combineFoldl
(\item acc ->
case parse item of
Err e ->
Err e
Ok ( k, v ) ->
Ok (Dict.insert k v acc)
)
Dict.empty
list
withStoppableFoldl : List String -> Result String (Dict String Int)
withStoppableFoldl list =
List.Extra.stoppableFoldl
(\item acc ->
case acc of
Err e ->
-- This never happens
List.Extra.Stop (Err e)
Ok okAcc ->
case parse item of
Err e ->
List.Extra.Stop (Err e)
Ok ( k, v ) ->
List.Extra.Continue (Ok (Dict.insert k v okAcc))
)
(Ok Dict.empty)
list It's not terrible to be honest. But it's also not great. |
And perhaps like this, which is fairly nice withCombineFoldl : List String -> Result String (Dict String Int)
withCombineFoldl =
Result.Extra.combineFoldl
(\item acc ->
parse item
|> Result.map (\( k, v ) -> Dict.insert k v acc)
)
Dict.empty hm OK, I think I'm coming around to this. I'm still not in love with the name. What about |
That's not a terrible name! |
combineFoldl
Motivating use case
It's common to fold over a list while keeping track of some state. Doing
Result.andThen
/Result.map
is aList.foldl
does not short circuit, whereas this function does.The text was updated successfully, but these errors were encountered: