-
Notifications
You must be signed in to change notification settings - Fork 24
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 the "andThenN" family of functions #50
Conversation
Sorry it took me so long to get around to reviewing this. There's some discussion on slack, but since that's not permanent, I'll recap it here: brianhicks: I think the recommendation there would be to use andMap. Just (+) |> andMap (Just 1) |> andMap (Just 2) == Just 3
Just (+) |> andMap Nothing |> andMap (Just 2) == Nothing
Just (+) |> andMap (Just 1) |> andMap Nothing == Nothing
but the andThenN names would be easier to discover, certainly! @ursi: An example of using I see in the second example you can't use But even in that case, you can use {-| same as in your Ellie -}
f : String -> String -> Maybe Int
f value1 value2 =
map2
(+)
(String.toInt (value1 ++ value2))
(String.toInt (value2 ++ value1))
combineToInt : Int -> Array String -> Maybe Int
combineToInt index array =
Maybe.map2
f
(Array.get index array)
(Array.get (index + 1) array)
|> Maybe.Extra.join (I haven't compiled or run that, so it's possible that I'm missing something.) Generally, I'd like to be pretty conservative about what goes into this package, especially if it's a big family of many functions. But I could be convinced that the readability/discoverability/usability of In your real world use case, can you check the values before hand like the first example, or do you need both to know if it will fail like the 2nd? Do you know of any other use cases? Are there examples of similar |
@skyqrose I agree with you that it would be helpful to see more use cases or precedent for |
I totally understand this mentality, and if this isn't deemed essential enough to be included then so be it. Personally I find using them more readable. I can always just put them in their own package. Perhaps I could even make a general
One solid use case for these is nesting them. Using
Elm is my first functional language so I cannot comment on this. I just saw myself commonly using |
Here's a more involved example https://ellie-app.com/87g4M6W9Fb7a1 pointColor : Float -> Int -> Array Point -> Color
pointColor time index array =
Maybe.withDefault black <|
andThen2
(\p1 p2 ->
andThen2
(\v1 v2 ->
if v1 <= time && time < v2 then
Just p1.color
else
Nothing
)
p1.value
p2.value
)
(Array.get index array)
(Array.get (index + 1) array) I have a time and an array of points with values. I want to use the point's color if the time is after the point, but before the next point, and I want to use black otherwise. |
It's not really necessary in Haskell because of -- With `<$>` and `<*>` we don't even need the `mapN` functions
f :: String -> String -> Maybe Int
f v1 v2 =
(+)
<$> (readMaybe (v1 ++ v2))
<*> (readMaybe (v2 ++ v1))
-- `do` notation for the Maybe type pulls out the Just value or returns Nothing
-- for the whole block if the value on the right side of the `<-` is a Nothing.
combineToInt index array = do
v1 <- get index array
v2 <- get (index + 1) array
f v1 v2 Or the more complex example: getPointColor time index array = fromMaybe black $ do
p1 <- get index array
p1Value <- pointValue p1
p2Value <- get (index + 1) array >>= pointValue
if p1Value <= time && time <= p2Value then
Just $ pointColor p1
else
Nothing Seems like the only other |
What exactly are we waiting on for this to be closed/merged? |
Ah, sorry for the delay, and thanks for the reminder. I've been searching around (example searches: 1 2 3 ) and I'm seeing enough places where people reimplement these (for Maybe and for other data types) that I think it's worth including these. This package I saw seems particularly relevant: ccapndave/elm-flat-map. It does what you were thinking of in having one package with I'll do some organization and cleanup. (I think it should go before the Applicative Functions section, and it needs tests) and then I'll merge it. In all my searching I've only seen one place that used a |
@ursi I pushed some changes. If you're happy with them I'll merge it. |
There is no need for the functions to be implemented as a combination of 'Maybe.map' and 'Maybe.andThen identity'
@skyqrose I just went up to five because |
No description provided.