ianmackenzie / elm-script

Experimental command-line scripting for Elm
34 stars 4 forks source link

Function for mapping both error and ok #18

Open MartinSStewart opened 4 years ago

MartinSStewart commented 4 years ago
mapBoth : (Result e a -> Result e1 a1) -> Script e a -> Script e1 a1
mapBoth map =
    Script.attempt
        >> Script.thenWith
            (\result ->
                case map result of
                    Ok ok ->
                        Script.succeed ok

                    Err err ->
                        Script.fail err
            )

It would be nice to have a function like this as it's not entirely obvious how to implement it with the existing functions and it's useful to have this level of flexibility sometimes.

MartinSStewart commented 4 years ago

As I'm using my own implementation of this, I'm finding that it might be more useful in this form

mapBoth : (Result e a -> Script e1 a1) -> Script e a -> Script e1 a1
mapBoth map =
    Script.attempt >> Script.thenWith map

Edit: the name mapBoth might not make so much sense now though.

ianmackenzie commented 4 years ago

Yeah, I think I would expect a mapBoth to have a signature like

mapBoth : (x -> y) -> (a -> b) -> Script x a -> Script y b

to match Result.Extra.mapBoth. Maybe mapResult and thenWithResult instead?

mapResult : (Result x a -> Result y b) -> Script x a -> Script y b
thenWithResult: (Result x a -> Script y b) -> Script x a -> Script y b