[elm-discuss] Re: Another documentation item I think is needed

2016-10-12 Thread Wouter In t Velt
Op dinsdag 11 oktober 2016 23:36:49 UTC+2 schreef J. E. Marca:
>
> On first read I laughed out loud --- I don't know what <| does, and it 
> seems a random application of head and drop.  Reading closer, it *is* 
> straightforward as you say... drop first n items (one based count), then 
> get the next one (via List.head) and return that (which ends up being the 
> traditional zero based index count).  Clever, but not something I can come 
> up with on my own.  
>

Touché! my background is also in javascript (React + Flux). For me all the 
"|>" "<|" ">>" symbols where confusing at first as well. I guess I am 
already used to them :)
 

> Regardless, I wanted to *assign* to that element in the update loop, so 
> Array works better for me.  This is what I'm doing.  My view has a bunch of 
> buttons that onClick send a message and an index.  (The index value is 
> built into the html element via  Array.indexedMap).  
>

I pattern that I took with me from javascript as well is similar to your 
example:

   - Get one item from some collection (Array)
   - Apply a change function the item = make a new item
   - Put the new item back in the collection = make new collection with 
   updated item

But I ended up with a lot of "case someMaybe of" etcetera, and a lot of 
code lines.

What I do nowadays is different: pass an index + a change function to a 
collection, and get back the whole collection with the updated item in it.
Array.Extra 

 
has an Array.update function which allows you to do that.

Note however, that with Array.update, you do not know *if *an update has 
taken place, you just get the new Array back.
So the example below is slightly different from your original, because it 
will also call the getColorJson if the pvars did not change.
 
DetectorPlotVars ->
let
changePvars pvars = { pvars | on = not pvars.on }  -- 
local function that takes pvar and toggles "on" parameter

newArr = Array.update index changePVars model.detectorPlotVars

newModel = { model | detectorPlotVars = newArr }
in
( newModel
, getColorJson newModel 
)

-- 
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: Another documentation item I think is needed

2016-10-11 Thread J. E. Marca
Thanks for the reply...

On Tuesday, October 11, 2016 at 4:36:47 AM UTC-7, Wouter In t Velt wrote:
>
> The comparison page you refer to is more a high-level comparison between 
> javascript and elm syntax.
> It does not look like it is intended to be in-depth.
>
> I agree it would be useful to have some docs at some time summing up the 
> pros and cons of List vs Array vs Set vs Dict.
> But I am not sure that the "From javascript" place would be the right 
> place.
>
>
I agree that "javascript" is probably not the best place.  However, as a 
newcomer, it is a place I studied a lot to get a feel for how to translate 
idioms I know from JS into Elm.

 

> PS: Getting an item from index in a List in Elm is quite straightforward
>
> getFromList : Int -> List a -> Maybe a
> getFromList index someList =
>   List.head <| List.drop index someList
>
> The main consideration vs Array or Dict (I think) is performance when the 
> lists get larger.
>
>

Thanks, that is cool.  And clearly "straightforward" is in the eye of the 
beholder.  On first read I laughed out loud --- I don't know what <| does, 
and it seems a random application of head and drop.  Reading closer, it 
*is* straightforward as you say... drop first n items (one based count), 
then get the next one (via List.head) and return that (which ends up being 
the traditional zero based index count).  Clever, but not something I can 
come up with on my own.  

Regardless, I wanted to *assign* to that element in the update loop, so 
Array works better for me.  This is what I'm doing.  My view has a bunch of 
buttons that onClick send a message and an index.  (The index value is 
built into the html element via  Array.indexedMap).  The update gets the 
index as so:


DetectorPlotVars ->
let
pvars = Array.get index model.detectorPlotVars
in
case pvars of
Just pvars ->
let
toggle = {pvars | on = not pvars.on}
newArr =  Array.set index toggle model.detectorPlotVars
in
({model | detectorPlotVars = newArr}
, getColorJson {model | detectorPlotVars = newArr})
_ -> (model, Cmd.none)



Please note that this will return a Maybe type (same as Array, and Dict BTW)
>

-- 
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: Another documentation item I think is needed

2016-10-11 Thread Wouter In t Velt
The comparison page you refer to is more a high-level comparison between 
javascript and elm syntax.
It does not look like it is intended to be in-depth.

I agree it would be useful to have some docs at some time summing up the 
pros and cons of List vs Array vs Set vs Dict.
But I am not sure that the "From javascript" place would be the right place.

PS: Getting an item from index in a List in Elm is quite straightforward

getFromList : Int -> List a -> Maybe a
getFromList index someList =
  List.head <| List.drop index someList

The main consideration vs Array or Dict (I think) is performance when the 
lists get larger.

Please note that this will return a Maybe type (same as Array, and Dict BTW)

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