[elm-discuss] Re: How to map over a list of entities that are different types?

2016-05-16 Thread Wil C
So, I came up with a solution that seems to work. As reference (and for 
others), I did reference work on Entity-Component Systems mentioned in this 
group before.

https://gist.github.com/TheSeamau5/8d7fac9f1937f0bab317
https://gist.github.com/TheSeamau5/ec9695232ae919185f4d

And I found Chris Granger's post quite 
helpful: www.chris-granger.com/2012/12/11/anatomy-of-a-knockout/

I decided to model a generic entity as:

// entity.elm

type alias ID = Int

type alias Model = {
id : ID
  , components : List Component
  }

With the components as a type:

// entity.elm
type Component =
Spatial Component.Spatial.Model
  | Corporeal Component.Corporeal.Model
  | Label Component.Label.Model
  | Viewable ComponentView
  | Gravitate Component.Gravitate.Model

type alias ComponentView = {
func : Model -> Form
  }

And the Spatial component model would contain fields that had to do with 
spatial orientation of the entity, modeled as:

// components/spatial.elm
type alias Model = {
mass : Float
  , forces : List Vec.Vec
  , pos : Vec.Vec-- in units
  , vel : Vec.Vec-- in units / centiseconds
  , acc : Vec.Vec-- in units / centiseconds ** 2
  , heading : Float
  }

So in declaring an instance entity, such as a cat, I modeled it as:

// entity/cat.elm
init : Entity.ID -> Entity.Model
init id = {
id = id
  , components = [
  Entity.spatial 50 (100, 0)
, Entity.corporeal (45, 45) Color.orange
, Entity.viewable view
, Entity.gravitate Component.Gravitate.ToEarth
]
  }

where view() was another method with the signature view : Model -> Form

What is Entity.spatial? Well, it's just:

//entity.elm
spatial : Float -> Vec -> Component
spatial mass pos =
  Spatial <| Component.Spatial.init mass po

So entity.elm has some functions that delegate to the specific component 
being created, and just tacks on the type in front.

How do we get at a particular component type in a list of components?

getSpatial : Model -> Maybe Component.Spatial.Model
getSpatial model =
  List.filterMap (\component ->
case component of
  Spatial spaceModel ->
Just spaceModel
  _ ->
Nothing
  ) model.components
  |> List.head

Unfortunately, this needed to be repeated for all other components. I don't 
know of a way to pass in a type and use it in the branches of a case 
expression.

But sometimes, you don't just want to get the component, you want to 
replace it. Learning the lesson from functors, I added a filterMap for 
entity, treating it as a container.

filterMapSpatial : (Component.Spatial.Model -> Component.Spatial.Model) -> 
Model -> Model
filterMapSpatial func entity =
  { entity |
components = List.map (\component ->
case component of
  Spatial space ->
Spatial (func space)
  _ ->
component
  ) entity.components
  }

So when I update and step through the model with a dt tick, I do:

//main.elm
step : Float -> Model -> Model
step dt model =
  model
  |> boundFloor
  |> gravity dt
  |> newtonian dt
  |> clearForces

where each one of those functions changes the model somehow. In newtonian, 
it maps over all entities and calls newtonian on each entity.

// main.elm
newtonian : Float -> Model -> Model
newtonian dt model =
  map (Entity.newtonian dt) model

it calculates the new position based on the velocity. It calculates 
velocity based on the total acceleration. And it calculate the total 
acceleration based on all the forces acting on it. So even though all the 
entities have different components, I'm able to call newtonian on each 
entity, and filterMapSpatial will act on a spatial component, if it can 
find one, and update it.

// entity.elm
newtonian : Float -> Model -> Model
newtonian dt entity =
  filterMapSpatial (\space ->
let
  acc = Component.Spatial.totalAcc space
  space2 = Component.Spatial.vel (space.vel |+ acc .* (dt / 10)) space
in
  Component.Spatial.pos (space2.pos |+ ((space.vel |+ space2.vel) .* 
(0.5 * (dt / 10 space2
  ) entity

And in that way, I can call newtonian on every entity, regardless of what 
components it has, because filterMapSpatial looks for a spatial component 
in an entity's component list, and only acts to replace it if it can find 
one. 

And that's how I got around overloading and polymorphism. The full code 
(work in progress, as of this posting) is here 
.
 
The downside is that every time you add a component, you have to add 
accessor and filterMap functions for each component.

Hope that helps someone.

Wil

On Tuesday, May 10, 2016 at 11:56:44 PM UTC-7, Wil C wrote:
>
> Thanks so far for the help that elm community has given lately. Really 
> appreciated. :)
>
> I've been in the middle of writing a simple game in Elm. However, I'm 
> running into a roadblock that makes me feel unproductive in elm. Here's the 
> problem:
>
> In a game, I got it in my head 

Re: [elm-discuss] This may be a problem of thinking, instead of implementing

2016-05-16 Thread Peter Damoc
Part of Elm philosophy is to get away from this kind of side-effects.

Also, I don't think Elm has enough OOP infrastructure to implement what you
want here.


On Tue, May 17, 2016 at 5:28 AM, Nandiin Bao  wrote:

> Yes, exactly. And I want an elegant implementation.
>
> 在 2016年5月16日星期一 UTC+8下午2:04:58,Peter Damoc写道:
>>
>> Hi Nandiin,
>>
>> It is not clear for me what are you actually trying to accomplish here.
>> I understand that you want the synchronization but it is not clear what
>> would a satisfying solution would be.
>>
>> I somehow think you might want side-effects where by side-effects I mean
>> have a piece of state (the shared state) that can be given to
>> sub-components and have the sub-components mutate it.
>> In other words, have a sub-component  that directly changes something
>> outside of it.
>> This way, when one component mutates the global state, the change is
>> present in the other components that rely on that state.
>> Is this what you are ultimately trying to accomplish?
>>
>>
>>
>>
>> On Mon, May 16, 2016 at 5:37 AM, Nandiin Bao  wrote:
>>
>>> I'm struggling with "Model sharing(synchronizing) problem" posted at
>>> here  and
>>> Peter raised some solution on it, but those needs either explicitly coding
>>> synchronization codes or some extra-knowledge of sub module. And then I
>>> found some posts (and replies on those posts) describing similar problems:
>>>
>>> Dealing with state duplication within the model (The Elm Architecture)
>>> 
>>>
>>>
>>> Communicating with a parent component to relay something happened in a
>>> sub component
>>> 
>>>
>>> Evan's answer on second post
>>>  says
>>> that we can return extra data from sub module's update function for
>>> super module to notice something is happening. It really works for noticing
>>> super modules but the synchronization code is still needed.
>>>
>>> Finally, a thought that the original modeling (there is something that
>>> should be shared or synchronized) may run away from FRP thinking occurred
>>> to me. Is this true ? and what's the correct way of thinking ?
>>>
>>> --
>>> 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...@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.
>



-- 
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] How to render at a lower fps in 0.17?

2016-05-16 Thread Wil C
I've made the switch to 0.17, and I've also followed along with the mario 
example . 
I can see how to get the delta, but it's locked in at around 60 fps with a 
subscription like "AnimationFrame.diffs Tick". How do I subscribe to 
AnimationFrame.diff with a lower framerate? It doesn't looks like I can use 
Time.every.

Wil 

-- 
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] Unable to access DataTransfer from Drag and Drop event - Elm 0.17

2016-05-16 Thread Ronn Ross
Still no luck, but I'm getting closer. The problem is I'm not sure how to build 
the decoder for the drop event. Dragging and dropping is working great I just 
need to get the file information from the drop event. In Js you can get the 
files array from the event using

event.data transfer.files

Within the files array there is an object for each file containing name, 
fileSize, etc.

I can't figure out how to decode the object being returned, nor can i find a 
way to see the object to start debugging. Here is my function.


assetArea : Html Msg
assetArea =
  div [ class "asset-area"
, onWithOptions "dragenter" (Options True True) (Json.succeed 
(DropEvent "dragenter"))
, onWithOptions "dragover" (Options True True) (Json.succeed (DropEvent 
"dragover"))
, onWithOptions "drop" (Options True True) (Json.map (\x -> log 
"whatever" x) (Json.succeed (DropEvent "drop")))
]
[ text "Drop here" ]

Here is an example in Js. 

https://jsfiddle.net/0GiS0/4ZYq3/

I've read the Json.decoder docs, but still having trouble. Can someone point me 
in the right direction?

-- 
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] How to not render an Html Node (from React to Elm)

2016-05-16 Thread Joaquín Oltra
Using style or a class name maybe? Is that hidden the attribute hidden on 
the div or something else?

For now I've created a helper 

 
for that specific case resulting 

 
in a nice little dsl 


gif url =
  when url <|
div [ class "Gif" ]
  [ input [ type' "text", readonly True, value url ] []
  , img [ src url ] []
  ]

error err =
  when err <| div [ class "Error" ] [ text err ]


On Monday, May 16, 2016 at 8:22:55 PM UTC+2, Fedor Nezhivoi wrote:
>
> One possible solution may be to hide element based on condition:
>
> view model =
>   div
> [ class "App" ]
> [ div [ class "Error", hidden (String.isEmpty model.error) ] [ text 
> model.error ]
> , div [ class "Gif", hidden (String.isEmpty model.gif) ]
>   [ input [ type' "text", readonly True, value model.gif ] []
>   , img [ src model.gif ] []
>   ]
> ]
>
> 2016-05-16 23:22 GMT+06:00 Joaquín Oltra  >:
>
>> Hi!
>>
>> I'm porting a simple React webapp to Elm, and there is a pattern for not 
>> rendering a component that I'm not sure how to do cleanly with elm.
>>
>> The pattern is:
>>
>> data ?  : null
>>
>> In a render method, like in here: 
>> https://github.com/joakin/gimme-gif/blob/415ff83611ab64ba10eb4f0cddccc12d1192815d/browser/ui/app.js#L13-L15
>>
>> My translation to Elm has been doing something like:
>>
>>   if String.isEmpty model.error then
>> text ""
>>   else
>> div [ class "Error" ] [ text model.error ]
>>
>> But it seems verbose and weird, so I figure there are probably better 
>> ways to accomplish the same.
>>
>> Any ideas?
>>
>> Here's some more code with the pattern a couple of times:
>>
>> view model =
>>   div
>> [ class "App" ]
>> [ -- Other nodes here...
>>   if String.isEmpty model.error then
>> text ""
>>   else
>> div [ class "Error" ] [ text model.error ]
>> , if String.isEmpty model.gif then
>> text ""
>>   else
>> div [ class "Gif" ]
>>   [ input [ type' "text", readonly True, value model.gif ] []
>>   , img [ src model.gif ] []
>>   ]
>> ]
>>
>>
>>
>>
>> ps: I'm keeping the state of the Error, Gif, etc separate because they 
>> are independent, there may be none to show, just the error, no error but a 
>> gif, and maybe both error and a gif.
>>
>> -- 
>> 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...@googlegroups.com .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> -- 
> Best regards,
> Fedor Nezhivoi.
>

-- 
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] Html.toElement in Elm version 0.17?

2016-05-16 Thread Peter Damoc
you can easily use Html for layout and create your own little layout
language with functions.

For example, you can use this playground for visually thinking about the
layout:
https://demos.scotch.io/visual-guide-to-css3-flexbox-flexbox-playground/demos/
and a little helper module like this one:
https://gist.github.com/pdamoc/c07eee6b7ab761853d59d8db01d89b71

to allow you to express your layout like

greyCenterStretch =
   div [ design <| justifyCenter <| alignItemsStretch <| [("background",
"grey")] ]

bottomDiv = div [design <| alignSelfEnd []]

view model =
greyCenterStretch
[ bottomDiv
[ ... ]
, ...
]





On Mon, May 16, 2016 at 10:29 AM, Tobias Hermann  wrote:

> Ok, thank you.
> I liked to align input boxes etc. with the functions from
> Graphics.Element, because it isolated me from all the HTML/div/CSS thinking.
> But of course I understand. Also thanks for the remark about
> Graphics.Input. I was already wondering where it went. ;-)
>
>
> On Sunday, May 15, 2016 at 11:32:53 PM UTC+2, Janis Voigtländer wrote:
>>
>> No, this conversion is not conceptually possible anymore. Html can have
>> event listeners attached, but post-0.16 Elements are weaker than 0.16
>> Elements in that they don't support events anymore (hence also no
>> Graphics.Input module anymore). Thus, the conversion from Html to Element
>> had to go.
>>
>> (Conceptually, it should still be possible to convert values of type Html
>> Never to Element, but it's not implemented.)
>>
>> Am Sonntag, 15. Mai 2016 schrieb Tobias Hermann :
>>
>>> Hi,
>>>
>>> I used to utilize Html.toElement to embed divs in flow down lists etc.
>>> from Graphics.Element.
>>> Html.toElement is gone with the version update. Is there still
>>> something available for this conversion?
>>>
>>> Tobias
>>>
>>> --
>>> 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.
>



-- 
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] Html.toElement in Elm version 0.17?

2016-05-16 Thread Tobias Hermann
Ok, thank you.
I liked to align input boxes etc. with the functions from Graphics.Element, 
because it isolated me from all the HTML/div/CSS thinking.
But of course I understand. Also thanks for the remark about 
Graphics.Input. I was already wondering where it went. ;-)


On Sunday, May 15, 2016 at 11:32:53 PM UTC+2, Janis Voigtländer wrote:
>
> No, this conversion is not conceptually possible anymore. Html can have 
> event listeners attached, but post-0.16 Elements are weaker than 0.16 
> Elements in that they don't support events anymore (hence also no 
> Graphics.Input module anymore). Thus, the conversion from Html to Element 
> had to go.
>
> (Conceptually, it should still be possible to convert values of type Html 
> Never to Element, but it's not implemented.)
>
> Am Sonntag, 15. Mai 2016 schrieb Tobias Hermann :
>
>> Hi,
>>
>> I used to utilize Html.toElement to embed divs in flow down lists etc. 
>> from Graphics.Element.
>> Html.toElement is gone with the version update. Is there still something 
>> available for this conversion?
>>
>> Tobias
>>
>> -- 
>> 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] This may be a problem of thinking, instead of implementing

2016-05-16 Thread Peter Damoc
Hi Nandiin,

It is not clear for me what are you actually trying to accomplish here.
I understand that you want the synchronization but it is not clear what
would a satisfying solution would be.

I somehow think you might want side-effects where by side-effects I mean
have a piece of state (the shared state) that can be given to
sub-components and have the sub-components mutate it.
In other words, have a sub-component  that directly changes something
outside of it.
This way, when one component mutates the global state, the change is
present in the other components that rely on that state.
Is this what you are ultimately trying to accomplish?




On Mon, May 16, 2016 at 5:37 AM, Nandiin Bao  wrote:

> I'm struggling with "Model sharing(synchronizing) problem" posted at here
>  and
> Peter raised some solution on it, but those needs either explicitly coding
> synchronization codes or some extra-knowledge of sub module. And then I
> found some posts (and replies on those posts) describing similar problems:
>
> Dealing with state duplication within the model (The Elm Architecture)
> 
>
>
> Communicating with a parent component to relay something happened in a sub
> component
> 
>
> Evan's answer on second post
>  says
> that we can return extra data from sub module's update function for super
> module to notice something is happening. It really works for noticing super
> modules but the synchronization code is still needed.
>
> Finally, a thought that the original modeling (there is something that
> should be shared or synchronized) may run away from FRP thinking occurred
> to me. Is this true ? and what's the correct way of thinking ?
>
> --
> 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.