Re: [elm-discuss] Re: Scaling Elm

2017-04-23 Thread Francesco Orsenigo
Yes I did.
It does not support keyboard.
As I wrote, "implementing keyboard support requires side effects and state".

On Mon, Apr 24, 2017 at 3:50 AM, Lourens Rolograaf  wrote:
> Hi Fransesco, did you read this?
> https://medium.com/elm-shorts/a-reusable-dropdown-in-elm-part-1-d7ac2d106f13
> part 2
> https://medium.com/elm-shorts/a-reusable-dropdown-in-elm-part-2-9659ef988441
>
>
> Op donderdag 20 april 2017 23:45:07 UTC+2 schreef Francesco Orsenigo:
>>
>>
>> I would add a dropdown.
>> Even a simple dropdown poses some interesting challenges to the
>> architecture:
>> - Dropdowns are ubiquitous in web apps.
>> - Dropdowns interact: when opening one, any other dropdown must close.
>> - Implementing keyboard support requires side effects and state.
>> - Dropdowns can have many different options; it can be disabled, it can
>> have a button to clear its selection, it can require a selected value or the
>> selected value can be a Maybe.
>> - Dropdowns requires styling: how do you distribute style with an Elm
>> package? More importantly, how do you organize it within the app?
>>
>>
>> We might want to also consider a numerical input element.
>> - How do I ensure that the input element always produces a number while,
>> at the same time, allowing the user to use Backspace to delete the last
>> cipher in the input?
>> There are going to be different ways of doing this, it would be very good
>> to be able to compare examples.
>>
>>
>>
>>
>>
>> On Wednesday, April 19, 2017 at 7:11:05 PM UTC+10, Peter Damoc wrote:
>>>
>>> Hello community,
>>>
>>> Scaling Elm apps seams to be a recurring topic.
>>>
>>> I was wondering if maybe we could negotiate a minimal set of
>>> functionality, something similar to ToDoMVC, that could be implemented using
>>> different approaches to explore what could be the best way to structure the
>>> code.
>>>
>>> What should this minimal example cover and what this minimal example
>>> should be (topic)?
>>>
>>> I'll start the list with some bits of functionality that I would like:
>>>
>>> - multiple pages with common structure (sidebar/navbar)
>>> - navigation without reloading the app (SPA routing without the hash)
>>> - authentication
>>> - complex widget reuse (a module/widget that generates side-effects; e.g.
>>> a weather widget, some stock ticker, an ad provided by a third party)
>>> - styling (CSS)
>>>
>>> I would also like the example to cover real world concerns of:
>>> - using a build manager to integrate with other technologies
>>> - development mode - deployment build
>>> - testing
>>>
>>> As for topic, I was thinking about an interface to the MusicBrainz
>>> Database (a simplified interface).
>>>
>>> What do you think?
>>> What bits of functionality would you like to see exemplified?
>>> Are you aware of any other project (in other languages) that exemplifies
>>> a minimal set of functionality and could be used as a template?
>>>
>>>
>>> --
>>> There is NO FATE, we are the creators.
>>> blog: http://damoc.ro/
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Elm Discuss" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/elm-discuss/WDDrFq-uP58/unsubscribe.
> To unsubscribe from this group and all its topics, 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] Re: Elm-reactor and static files (images & videos)

2017-04-23 Thread Arnau Orriols
FWIW, 

As a work around, I tried using a common http server to serve the app and 
> was able to have hotreloading using a file system watching library to 
> automatically build and reload the browser. But I lost the debugger :(
>

Elm-live  is basically what you have 
described. I use it in all my projects as replacement of reactor, and I'm 
quite happy.

You should be able to use the debugger outside of elm-reactor if you 
> compile with the --debug flag, for example
> elm make --debug Main.elm --output main.js
>

You can use this:

elm-live Main.elm --output main.js --warn --debug --open


-- 
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: Looking for a better way to use `andThen` chains (and other beginner questions)

2017-04-23 Thread Max Goldstein

>
> If you're looking for a somewhat broader background on data modeling in 
> Elm, this talk is a must-watch: Making Impossible States Impossible
>

Sorry, looks like the YouTube link got put at the top of the post. 

-- 
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: Looking for a better way to use `andThen` chains (and other beginner questions)

2017-04-23 Thread Max Goldstein
https://www.youtube.com/watch?v=IcgmSRJHu_8Instead of lists with numbered 
items, have you thought about using a dictionary 
? Something 
like this?

import Dict exposing (Dict)

type alias No = Int
type alias Game = { stars : Dict No Star, teams : Dict No Team }
type alias Team = { name : String, color : String, species : String }
type alias Star = { name : String, planets : Dict No Planet }
type alias Planet = { name : String, minerals : Int, colony : Maybe Colony }
type alias Colony = { team : No, defense : Int }

getStarPlanet : No -> No -> Game -> Result String Planet
getStarPlanet starNo planetNo {stars} =
  Dict.get starNo stars
|> Result.fromMaybe "Star not found"
|> Result.andThen (\{planets} -> Dict.get planetNo planets |> 
Result.fromMaybe "Planet not found")

It's still a little repetitive, and you need to nest a Result.fromMaybe 
inside a Result.andThen callback, but it works. (You should maybe consider 
a union type for these errors instead of strings.) 

I've also tried to anticipate that you'll have uncolonized planets, which 
will still have things like minerals, size, atmosphere, temperature, things 
like that. Some of those will also have colonies which will have a team, 
defense, buildings, colonists, and so on.

Incidentally, if you're okay fixing the number of teams at compile-time, 
you might be able to avoid the inconsistent state of a colony whose team is 
not in the team dictionary.

type TeamNo = Red | Blue
type alias Teams = { red : Team, blue : Team }
getTeam : TeamNo -> Teams -> Team
getTeam no teams =
  if no == Red then teams.red else teams.blue

If you're looking for a somewhat broader background on data modeling in 
Elm, this talk is a must-watch: Making Impossible States Impossible

-- 
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] Unexpected error when parsing what seems to be reasonable code (Bug in parser maybe?)

2017-04-23 Thread Ilias Van Peer
If I'm not mistaken, this isn't so much about nested records as it is about 
referencing a qualified record in a record update expression.

This seems related to - but not exactly the same thing as 
- https://github.com/elm-lang/elm-compiler/issues/1549

So yeah, you could either create a local binding for it, use an extra 
helper function like Peter described, or expose `defaultViewConfig` in your 
imports so you can use it unqualified.

I've created a new issue for this 
- https://github.com/elm-lang/elm-compiler/issues/1587

Op zondag 23 april 2017 09:42:12 UTC+2 schreef Peter Damoc:
>
> On Sat, Apr 22, 2017 at 11:50 AM, Dwayne Crooks  > wrote:
>
>> However, if I update the let bindings as follows:
>>
>> let
>>> defaultViewConfig = Rating.defaultViewConfig
>>> ratingViewConfig =
>>> { defaultViewConfig | readOnly = model.readOnly }
>>> in
>>> ...
>>
>>
>> Then, there is no error. Am I missing something or is this a bug in the 
>> parser?
>>
>> This is related to an active topic around record update syntax. 
> In short, currently it is not allowed to directly update nested records. ({ 
> Rating.defaultViewConfig | readOnly = model.readOnly } is invalid syntax) 
> The temporary solution is the one you stumbled upon: creating an alias for 
> the nested record. 
>
> Another solution would be to use custom functions:
>
>
> updateReadOnly : { a | readOnly : Bool } -> Bool -> a
> updateReadOnly rec readOnly =
> { rec | readOnly = readOnly }
>
> and then use it like: 
>
> ratingViewConfig =
> updateReadOnly Rating.defaultViewConfig True
>
>
> -- 
> There is NO FATE, we are the creators.
> blog: http://damoc.ro/
>

-- 
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: Scaling Elm

2017-04-23 Thread Lourens Rolograaf
Hi Fransesco, did you read 
this? 
https://medium.com/elm-shorts/a-reusable-dropdown-in-elm-part-1-d7ac2d106f13 
part 
2 https://medium.com/elm-shorts/a-reusable-dropdown-in-elm-part-2-9659ef988441


Op donderdag 20 april 2017 23:45:07 UTC+2 schreef Francesco Orsenigo:
>
>
> I would add a dropdown.
> Even a simple dropdown poses some interesting challenges to the 
> architecture:
> - Dropdowns are ubiquitous in web apps.
> - Dropdowns interact: when opening one, any other dropdown must close.
> - Implementing keyboard support requires side effects and state.
> - Dropdowns can have many different options; it can be disabled, it can 
> have a button to clear its selection, it can require a selected value or 
> the selected value can be a Maybe.
> - Dropdowns requires styling: how do you distribute style with an Elm 
> package? More importantly, how do you organize it within the app?
>
>
> We might want to also consider a numerical input element.
> - How do I ensure that the input element always produces a number while, 
> at the same time, allowing the user to use Backspace to delete the last 
> cipher in the input?
> There are going to be different ways of doing this, it would be very good 
> to be able to compare examples.
>
>
>
>
>
> On Wednesday, April 19, 2017 at 7:11:05 PM UTC+10, Peter Damoc wrote:
>>
>> Hello community, 
>>
>> Scaling Elm apps seams to be a recurring topic. 
>>
>> I was wondering if maybe we could negotiate a minimal set of 
>> functionality, something similar to ToDoMVC, that could be implemented 
>> using different approaches to explore what could be the best way to 
>> structure the code. 
>>
>> What should this minimal example cover and what this minimal example 
>> should be (topic)?
>>
>> I'll start the list with some bits of functionality that I would like: 
>>
>> - multiple pages with common structure (sidebar/navbar)
>> - navigation without reloading the app (SPA routing without the hash) 
>> - authentication 
>> - complex widget reuse (a module/widget that generates side-effects; e.g. 
>> a weather widget, some stock ticker, an ad provided by a third party)
>> - styling (CSS)
>>
>> I would also like the example to cover real world concerns of: 
>> - using a build manager to integrate with other technologies 
>> - development mode - deployment build
>> - testing 
>>
>> As for topic, I was thinking about an interface to the MusicBrainz 
>> Database (a simplified interface).
>>
>> What do you think? 
>> What bits of functionality would you like to see exemplified? 
>> Are you aware of any other project (in other languages) that exemplifies 
>> a minimal set of functionality and could be used as a template?  
>>
>>
>> -- 
>> There is NO FATE, we are the creators.
>> blog: http://damoc.ro/
>>
>

-- 
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: view function to call other view function with different Msg type

2017-04-23 Thread Max Goldstein
Yes, Ian is correct.

Html.Attributes.map 
 
: 
(a -> msg) -> Attribute a -> Attribute msg

Html.map  
: (a -> 
msg) -> Html a -> Html msg

-- 
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: view function to call other view function with different Msg type

2017-04-23 Thread Ian Mackenzie
Shouldn't that be Html.map, not Html.Attributes.map? itemView returns a Html 
msg, not an Html.Attribute msg...

On Saturday, 22 April 2017 21:53:47 UTC-4, Erik Lott wrote:
>
> view : Model -> Html Msg
> view model =
> div []
> (model.items
>   |> List.map itemView 
>   |> List.map (Html.Attributes.map ItemMsg)
> )
>
>
>
>
> On Saturday, April 22, 2017 at 5:25:41 PM UTC-4, Vardhan wrote:
>>
>> [ Pardon if this appear twice ]
>>
>> hi,
>>   this is my  toy 1st elm app.
>>i'm trying to keep the 'item' of the list as a seperet module.
>>
>> so i have in module Item:
>> -
>> type alias ItemModel= { ... }
>> type ItemMsg = .. | .. | ..
>> itemView : ItemModel -> Html ItemMsg
>> -
>>
>> And in the top level App  module:
>> -
>> type alias Model = { items: List ItemModel }
>> type Msg = ItemMsg ItemMsg | ...
>> view : Model -> Html Msg
>> view model =
>> div []
>> (List.map itemView model.items )
>> --
>>
>> Well, the problem is itemView can't be used , as it is 'Html ItemMsg', 
>> and not 'Html Msg'
>> However, the Item module doesn't have access to 'Msg' ( which is top 
>> level ),
>>
>> How should I use itemView in the view function ?
>>
>> -Regards
>> Vardhan
>>
>>
>>

-- 
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: Elm-reactor and static files (images & videos)

2017-04-23 Thread Ian Mackenzie
You should be able to use the debugger outside of elm-reactor if you 
compile with the --debug flag, for example

elm make --debug Main.elm --output main.js

You should then be able to use whatever you want to serve the HTML/JS and 
you'll still get the debug overlay.

On Saturday, 22 April 2017 18:33:29 UTC-4, Fernando Martinez wrote:
>
> Hello All! 
>   I'm new to Elm and trying to build a small 
> "playlist/carousel app"  (basically, a list of videos and images that 
> passes one after the other) to learn elm and try some ideas about ports. 
>
> I think I'm having a newie problem but I could not found a solution or 
> leads to a solution (or I couldn't undestand them :P), so I'm writing here.
>
> I'm using the elm-reactor to develop and like very much the debbugger and 
> the automatic building.
>
> So, my first question is:  is it posible to serve static files (images and 
> videos) from the elm-reactor?
> (From what I could observe, it returns an empty response. perhaps I'm 
> missing something?)
>
> As a work around, I tried using a common http server to serve the app and 
> was able to have hotreloading using a file system watching library to 
> automatically build and reload the browser. But I lost the debugger :(
>
> So, the second question, depending on how the first one goes. is: is there 
> any way to open the debugger outside of elm-reactor?
>
>
> Thanks you very much!
> Fernando.
>

-- 
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: Discovery: Model /= Database

2017-04-23 Thread Rehno Lindeque
Hi Kasey, I just wanted to mention that the wikipedia article 
Model-view-viewmodel 
 might 
be helpful reading. In some architectural patterns this is called the 
viewmodel rather than the model. (A couple of GUI oriented packages call 
this State, for the same reason I think).

At my workplace we tend to use the convention that models should be named 
after screen elements, e.g. PartEditor.Model whereas the response from the 
server would just be called Part (even though this record is closer to the 
database model).

-Editor, -Report, -Chart, -Table, -Matrix, -Page, -Viewer are of a couple 
of suffixes I'm tending to reach for when naming the modules that contain 
viewmodels.

On Saturday, April 22, 2017 at 7:18:41 PM UTC, Dustin Farris wrote:
>
> Interesting approach to just cache things in local storage and use the 
> Model as a view "state" only.
>
> I keep a nice store in my Model, but I think a key difference is that I'm 
> using websockets to update information automatically.  Each route (page) 
> has a list of channels that it cares about, and when data comes in on those 
> channels, the store is updated.
>
> WRT your issue on authenticated routes, my approach was to write a 
> function that checks the model for a Maybe String which is the JWT token. 
>  If it was Nothing, I'd send a Cmd to navigate to the login page.  It has 
> worked well so far.
>
>
> On Wednesday, April 19, 2017 at 7:28:06 PM UTC-4, Kasey Speakman wrote:
>>
>> I'm probably slow, but in recent months I've discovered that trying to 
>> use Elm's Model like a database or cache (as I have previously seen 
>> suggested) has turned out to be pretty painful for me. An example 
>> database-minded model where a section could display *either* a list of 
>> employees *or* a list of courses.
>>
>> type alias Model =
>> { employees : List Employee
>> , courses : List Course
>> , loadingError : Maybe Http.Error
>> , route : MyRoute -- employee or course
>> }
>>
>> The problem this runs into is having to worry about state management. I 
>> have to remember to "reset" or "turn off" things when they are not active. 
>> As my application grew, I had a lot of problems that boiled down to tedious 
>> state management details. My cached data didn't turn out to be all that 
>> useful because I usually had to reload it anyway, in case something changed.
>>
>> Instead, I have been moving toward the model only representing the 
>> current state of my UI. The big difference here is the model representing 
>> the current *visual* elements and their data. This leads more to using 
>> union types to represent parts of the UI. When you switch to a different 
>> case of the union type, the data from the previous case is *dropped on 
>> the floor*. This leaves nothing to remember to "reset". RemoteData is a 
>> good micro-example of this. If there was an error fetching the data, when 
>> the user requests the data again, you switch back to Loading, the error 
>> message is dropped on the floor. No forgetting to hide it.
>>
>> type RemoteData e a
>> = NotAsked
>> | Loading
>> | Failure e
>> | Success a
>>
>> If it is really important to cache the data, I prefer to keep that as a 
>> persistence concern, not on Model. It can be part of the process for 
>> retrieving the data to first check my chosen cache before making a request 
>> for fresh data. For instance, first check local storage before making an 
>> HTTP call. (Currently, this scenario is easier with Native modules for lack 
>> of Local Storage API or being able to wait on port subscriptions. But it's 
>> still doable.)
>>
>> So working towards a Model reflecting the visuals on the page has been an 
>> interesting challenge. I'm not claiming it's easier, but so far I've found 
>> it avoids a class of problems, and has led to some interesting discoveries 
>> in my own apps. One small example: I realized that my LoggedIn and 
>> NotLoggedIn routes should actually be separate "apps". Attempts to model 
>> this in a SPA fashion with the LoggedIn and NotLoggedIn routes as siblings 
>> always came up with the conundrum: how do I make it a compiler error for 
>> the model to be in LoggedIn mode but I receive a NotLoggedIn message, or 
>> vice versa? Even using TEA, I could not avoid this situation. Then I 
>> realized the only way to do that would be as separate apps. And that it was 
>> entirely possible to separate them. My "login page" turned out to be an 
>> entirely self-contained process: the user filling in info, obtaining a 
>> token, and saving it to local storage.
>>
>> I post this in the slim hope it is helpful to someone.
>>
>

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

Re: [elm-discuss] Why `let`?

2017-04-23 Thread Wojciech S. Czarnecki
Dnia 2017-04-23, o godz. 10:34:22
Tom Ayerst  napisał(a):

> "where" would be nice though...
> 
> foo a b =
>x + y
> where
> x = a
> y = b

Most people read from top to bottom not otherwise.

So let - in gives information in proper order:

Let -- using this
   saw, plane, hammer, nails, two boards, two planks
in  -- do that
   make bookshelf 

https://github.com/elm-lang/elm-compiler/issues/621
https://groups.google.com/forum/?fromgroups=#!topic/elm-discuss/KiKF6K9gBKU

:)

-- 
Wojciech S. Czarnecki
   ^oo^ OHIR-RIPE

-- 
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] Why `let`?

2017-04-23 Thread Tom Ayerst
"where" would be nice though...

foo a b =
   x + y
where
x = a
y = b

On 23 April 2017 at 10:17, Jan Tojnar  wrote:

> On Sunday, 23 April 2017 02:23:20 UTC+2, Witold Szczerba wrote:
>>
>> in real code things are much more complicated and confusing. I have
>> looked at the Todo.elm you prepared and (for me) it is much harder to
>> reason about.
>>
>
> I agree, it is especially confusing once we get to *destructing*:
>
> https://github.com/rtfeldman/elm-todomvc/blob/
> 8678c8bcaeb5cb4b3f87dbefb7a01b5fe492dbc7/Todo.elm#L44
>
> let – in allows quick visual distinction between definitions and the main
> statement.
>
> Cheers,
>
> Jan Tojnar
>
> --
> 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] Why `let`?

2017-04-23 Thread Jan Tojnar

On Sunday, 23 April 2017 02:23:20 UTC+2, Witold Szczerba wrote:
>
> in real code things are much more complicated and confusing. I have looked 
> at the Todo.elm you prepared and (for me) it is much harder to reason about.
>
 
I agree, it is especially confusing once we get to *destructing*:

https://github.com/rtfeldman/elm-todomvc/blob/8678c8bcaeb5cb4b3f87dbefb7a01b5fe492dbc7/Todo.elm#L44

let – in allows quick visual distinction between definitions and the main 
statement.

Cheers,

Jan Tojnar

-- 
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] Unexpected error when parsing what seems to be reasonable code (Bug in parser maybe?)

2017-04-23 Thread Peter Damoc
On Sat, Apr 22, 2017 at 11:50 AM, Dwayne Crooks 
wrote:

> However, if I update the let bindings as follows:
>
> let
>> defaultViewConfig = Rating.defaultViewConfig
>> ratingViewConfig =
>> { defaultViewConfig | readOnly = model.readOnly }
>> in
>> ...
>
>
> Then, there is no error. Am I missing something or is this a bug in the
> parser?
>
> This is related to an active topic around record update syntax.
In short, currently it is not allowed to directly update nested records. ({
Rating.defaultViewConfig | readOnly = model.readOnly } is invalid syntax)
The temporary solution is the one you stumbled upon: creating an alias for
the nested record.

Another solution would be to use custom functions:


updateReadOnly : { a | readOnly : Bool } -> Bool -> a
updateReadOnly rec readOnly =
{ rec | readOnly = readOnly }

and then use it like:

ratingViewConfig =
updateReadOnly Rating.defaultViewConfig True


-- 
There is NO FATE, we are the creators.
blog: http://damoc.ro/

-- 
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] Optimisation in Elm

2017-04-23 Thread Peter Damoc
Hi Ana,

1. Elm is fast because it use a very fast implementation of a virtual dom.
2. Elm compiler outputs JavaScript so all the low level memory concerns are
handled by the JavaScript VM, not by Elm
3. No, Cmds do not influence the purity of Elm. If a function returns Cmds,
it will always return the same Cmds. Executing those Cmds might lead to
different results BUT that's a different concern.


On Fri, Apr 21, 2017 at 3:10 PM, Ana Borovac 
wrote:

> Hi,
>
> I have some theoretical questions:
>
> - why is Elm so fast? is there some kind of tail-recursion behind or is
> this just the part that JavaScript does?
>


> - how is the storage mastered (garbage collection)?
> - does commands (Cmd) influence on pureness of Elm?
>
> Thank you so much for your help!
>
> Ana
>
> --
> 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.
>



-- 
There is NO FATE, we are the creators.
blog: http://damoc.ro/

-- 
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] Looking for a better way to use `andThen` chains (and other beginner questions)

2017-04-23 Thread Peter Damoc
On Sat, Apr 22, 2017 at 12:37 AM, Stefan Matthias Aust 
wrote:

>
> *My first question*: Is there any way to create a constrained type like
> "something that has a `no` field?
>
>
Right now, I have a lot of nearly identical code like this:
>
> findStar : StarNo -> Game -> Maybe Star
> findStar no game =
> List.head (List.filter (\star -> star.no == no) game.stars)
>
> findPlanet : PlanetNo -> Star -> Maybe Planet
> findPlanet no star =
> List.head (List.filter (\planet -> planet.no == no) star.planets)
>

You could use record pattern matching to create generic functions but in
the above case that won't work because the container name is different.

Here is how it would have looked if the container name would have been the
same (in this case `children`)

findChild : Int -> { b | no : Int, children : List { a | no : Int } } ->
Maybe { a | no : Int }
findChild no parent =
List.head (List.filter (\child -> child.no == no) parent.children)

I used Int for the `no` but you you can leave that also as a parameter
(e.g. `c`)


> *Second question*: Is there an easy way to replace an element in a list?
>
> You can create a generic helper like

swapIfSameNo : { a | no : Int } -> { a | no : Int } -> { a | no : Int }
swapIfSameNo a b =
if a.no == b.no then
a
else
b

and then the updates would be simpler

updatePlanetInStar : Star -> Planet -> Star
updatePlanetInStar star planet =
{ star | planets = List.map (swapIfSameNo planet) star.planets }


The same comments from above apply. If you have a generic name for the
container (children) you can make this a generic updateChild function that
would work on both.


> Sidenote: I'd love to use
>
> { game | stars[x].planets[y] = planet }
>
> and let the compiler deal with all that stupid boilerplate code. I realize
> that for this to work, I'd have to use `Array` (a rather unsupported type)
> instead of `List` but that would be fine. That way, I could alias my `no`
> to indices. By the way, do I really had to create my own `nth` function for
> Lists?
>

What make you think Array is unsupported?

Regarding indexes in arrays (or lists for that matter), it would be lovely
to be able to say things like that but it is unsafe. You might be using an
out of bounds index.
There are no solutions in contexts like these, only tradeoffs. Elm choses
to make things more verbose and more explicit in order to guarantee safety.
That's the tradeoff that Elm chose.



> *Third question*. When implementing `BuildDefense`, I need to convert the
> no's to records and this could fail. Instead of working with `Maybe`, I use
> a `Result Error a` type and `Error` is a sum type that contains all the
> errors that could occur. However, I get some really ugly chaining…
>
> buildDefense : TeamNo -> StarNo -> PlanetNo -> Int -> Game -> Result Error
> Game
> buildDefense teamNo starNo planetNo amount game =
> findTeamR teamNo game
> |> Result.andThen
> (\team ->
> findStarR starNo game
> |> Result.andThen
> (\star ->
> findPlanetR planetNo star
> |> Result.andThen
> (\planet ->
> if amount < 1 then
> Err InvalidAmount
> else if planet.owner /= team.no
> then
> Err NotYourPlanet
> else if planet.resources < amount
> * defenseCost then
> Err NotEnoughResources
> else
> planet
> |> addDefense amount
> |> subtractResources
> (amount * defenseCost)
> |> updatePlanet star
> |> updateStar game
> |> Ok
> )
> )
> )
>
> How am I supposed to write this "the Elm way"?
>

What I would try to do different is split buildDefense in two between
finding the proper planet and doing something with it

findPlanet : TeamNo -> StarNo -> PlanetNo -> Game -> Result Error ( Planet,
Star )
findPlanet teamNo starNo planetNo game =
findTeamR teamNo game
|> Result.andThen
(\team ->
findStarR starNo game
|> Result.andThen
(\star ->
findPlanetR planetNo star
|> Result.andThen
(\planet ->
if planet.owner /= team.no then