Re: [elm-discuss] Maybe.andThen with List.foldr type trick?

2016-10-08 Thread Duane Johnson
Ah, I misunderstood the order of foldr. Thanks alll.

BTW, this helped me see where I'd gone wrong:
http://stackoverflow.com/questions/1757740/how-foldr-works#1763323

On Sat, Oct 8, 2016 at 10:09 AM, Janis Voigtländer <
janis.voigtlaen...@gmail.com> wrote:

> http://package.elm-lang.org/packages/elm-community/maybe-
> extra/2.0.0/Maybe-Extra#combine
>
> You can also check that function's source code.
>
> Am 08.10.2016 um 17:03 schrieb Max Goldstein :
>
> The Elm compiler is correctly telling you that your list is a list of
> functions that produce Maybes, but you said you had a list of Maybes.
>
> If you wanted the first Just regardless of other values: List.filterMap
> identity >> List.head
>
> My advice is to check the lengths of the original list and the filterMap'd
> list. If they're equal, take the head of the filterMap'd list. If not,
> Nothing.
>
> --
> 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 elm-discuss+unsubscr...@googlegroups.com.
> 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 elm-discuss+unsubscr...@googlegroups.com.
> 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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Maybe.andThen with List.foldr type trick?

2016-10-08 Thread Janis Voigtländer
http://package.elm-lang.org/packages/elm-community/maybe-extra/2.0.0/Maybe-Extra#combine

You can also check that function's source code. 

> Am 08.10.2016 um 17:03 schrieb Max Goldstein :
> 
> The Elm compiler is correctly telling you that your list is a list of 
> functions that produce Maybes, but you said you had a list of Maybes.
> 
> If you wanted the first Just regardless of other values: List.filterMap 
> identity >> List.head
> 
> My advice is to check the lengths of the original list and the filterMap'd 
> list. If they're equal, take the head of the filterMap'd list. If not, 
> Nothing.
> 
> -- 
> 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 elm-discuss+unsubscr...@googlegroups.com.
> 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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Maybe.andThen with List.foldr type trick?

2016-10-08 Thread Joey Eremondi
Oops, in the first case it should return Just [], not []

On Oct 8, 2016 9:02 AM, "Joey Eremondi"  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"  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 elm-discuss+unsubscr...@googlegroups.com.
>> 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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Maybe.andThen with List.foldr type trick?

2016-10-08 Thread Joey Eremondi
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"  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 elm-discuss+unsubscr...@googlegroups.com.
> 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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Maybe.andThen with List.foldr type trick?

2016-10-08 Thread Duane Johnson
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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.