[elm-discuss] Re: Missing json decoding tool to decode multitouch events cleanly

2017-05-08 Thread Matthieu Pizenberg
On Monday, May 8, 2017 at 11:36:13 PM UTC+8, Erik Lott wrote:
>
> Yeah, I think you spotted my bad logic. You're talking about functions 
> like this, right?
>

Yep that was it :) 

-- 
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] Re: Missing json decoding tool to decode multitouch events cleanly

2017-05-06 Thread art yerkes
Something to note about event objects in javascript is that they can go out 
of scope when you carry them away from the event handler, consequently, elm 
does all decoding on the same stack as the event, and you're likely to get 
a runtime error if you store the raw event value.

I decided to write a bit about the topic of hairy decoders:

https://medium.com/@prozacchiwawa/the-im-stupid-elm-language-nugget-16-295f201eb458

Using a Dict to enumerate all keys can work as you see, but you're also 
exposed to strange values (such as the "item" function, and elm doesn't 
like function values much).

Kind of wrote this as well because, despite that there's nothing super 
wrong with it in the case of touch events (hopefully it only goes to 10), 
my programmer sense goes off matching length to constants as in Max 
Goldstein's reply.

TLDR for here: you can easily capture a Json.Decode.Value to use as part of 
a decoder, as long as you take your info from it in the same stack, and 
from there, you can convert code that uses map, andThen and Ok as results 
to Json.Decode.Decoder.

On Saturday, May 6, 2017 at 9:12:32 AM UTC-7, Max Goldstein wrote:
>
> What about something like this?
>
> decodeTouches : Decoder (List Touch)
> decodeTouches =
> Decode.field "length" Decode.int
> |> Decode.andThen
> (\len ->
> case len of
> 1 ->
> Decode.map (\a -> [ a ])
> (Decode.field "1" touchDecoder)
>
> 2 ->
> Decode.map2 (\a b -> [ a, b ])
> (Decode.field "1" touchDecoder)
> (Decode.field "2" touchDecoder)
>
> 3 ->
> Decode.map3 (\a b c -> [ a, b, c ])
> (Decode.field "1" touchDecoder)
> (Decode.field "2" touchDecoder)
> (Decode.field "3" touchDecoder)
>
> _ ->
> Decode.fail "Unexpected length"
> )
>

-- 
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] Re: Missing json decoding tool to decode multitouch events cleanly

2017-05-06 Thread Max Goldstein
What about something like this?

decodeTouches : Decoder (List Touch)
decodeTouches =
Decode.field "length" Decode.int
|> Decode.andThen
(\len ->
case len of
1 ->
Decode.map (\a -> [ a ])
(Decode.field "1" touchDecoder)

2 ->
Decode.map2 (\a b -> [ a, b ])
(Decode.field "1" touchDecoder)
(Decode.field "2" touchDecoder)

3 ->
Decode.map3 (\a b c -> [ a, b, c ])
(Decode.field "1" touchDecoder)
(Decode.field "2" touchDecoder)
(Decode.field "3" touchDecoder)

_ ->
Decode.fail "Unexpected length"
)

-- 
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.