Oops, in the first case it should return Just [], not [] On Oct 8, 2016 9:02 AM, "Joey Eremondi" <[email protected]> wrote:
> I'm not sure using and then will work like this in the accumulator, you're > giving it a list of thunks, but andThen tries to chain functions together, > and your functions don't chain like that. > > How about this: > > forMaybe list = case list of > [] -> [] > hm::tm -> hm `andThen` \h -> > (forMaybe tm) `andThen` \t -> Just (h::t) > > I apologize for typos, I'm on my phone. This is basically what the forM > function does in Haskell, it works with any type with an andThen, and some > wrapper to put a value in the type (Just in the case of Maybe). > > On Oct 8, 2016 8:42 AM, "Duane Johnson" <[email protected]> wrote: > >> To avoid the XY problem: I'm aiming to create a function that will take a >> List (Maybe a) and return a Maybe (List a), where the result will be >> Nothing if ANY of the (Maybe a)s are Nothing, otherwise it will be a list >> of just the "a"s. >> >> Here's where I'm getting tripped up in that quest, however: when I >> manually combine a list of numbers (0 through 3) things work fine: >> >> ```elm >> {-| This works -} >> test = >> ((((Just 0) `Maybe.andThen` (\_ -> Just 1)) `Maybe.andThen` (\_ -> >> Just 2)) `Maybe.andThen` (\_ -> Just 3)) >> ``` >> >> but when I try to use `foldr` and `andThen`, it fails: >> >> ```elm >> {-| This does NOT work -} >> test : Maybe number >> test = >> let >> func = >> Maybe.andThen >> >> acc = >> (Just 0) >> >> list = >> [ (\_ -> Just 1), (\_ -> Just 2), (\_ -> Just 3) ] >> in >> List.foldr func acc list >> ``` >> >> I get the error: >> >> ``` >> The 3rd argument to function `foldr` is causing a mismatch: >> >> Function `foldr` is expecting the 3rd argument to be: >> >> List (Maybe a) >> >> But it is >> >> List (a -> Maybe number) >> ``` >> >> When I change it and remove the anonymous functions in the list (i.e. >> ```list = [ Just 1, Just 2, Just 3 ]```) I get a new error, which I believe >> is leading me down a wrong path (Function `foldr` is expecting the 2nd >> argument to be `a -> Maybe b` But it is `Maybe number`). >> >> Any hints here? I've been hitting my head against the wall for a few >> hours. Any help would be appreciated. (Is this an Elm bug?) >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Elm Discuss" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> For more options, visit https://groups.google.com/d/optout. >> > -- You received this message because you are subscribed to the Google Groups "Elm Discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
