[elm-discuss] Re: Elm dynamically loading a decoder based on previous field

2016-12-02 Thread Leroy Campbell
I've posted a full example here:

https://gist.github.com/artisonian/4f62ee4572a788d5d801c997e5a560ce

If it'd be helpful, I can work on a writeup to explain my design choices.

On Friday, December 2, 2016 at 8:11:45 PM UTC-5, Nima Birgani wrote:
>
> Hi all,
>
> I'm trying to decode an relatively large JSON to run my app in elm. here 
> is the how my json looks like:
>
> {
>   "pageTitle" : "users",
>   "primaryWidget" : "user"
>   "primaryWidgetData" : {
> "permissions" : ["read", "write"]
> "data": [
>   {
> "name": "name 1"
> "email" : "someem...@sd.cs"
> "id": "dsd33-wdsds"
>   },
>   {
> "name": "name 2"
> "email" : "anot...@sd.cs"
> "id": "-wdsds"
>   }
> ]
>   } 
> }
>
> alternatively my json can be like:
>
> {
>   "pageTitle" : "accounts",
>   "primaryWidget" : "account-manager"
>   "primaryWidgetData" : {
> "accounts": [
>   {
> "accountId": "id 1"
> "accountName" : "acc name"
> "owner": {
>   "name": "name 2"
>   "email" : "anot...@sd.cs"
>   "id": "-wdsds"
> }
>   },
>   {
> "accountId": "id 2"
> "accountName" : "acc name 2"
> "owner": {
>   "name": "name 1"
>   "email" : "s...@sd.cs"
>   "id": "3343-wdsds"
> }
>   }
> ]
>   }
> }
>
> when I'm decoding the JSON i would like to load  the relevant decoder 
> based on the "primaryWidget" and I'm using pipeline decoder:
>
> manifestDecoder : Decoder Manifest
> manifestDecoder =
> Json.decode Manifest
> |> required "pageTitle" string
> |> required "primaryWidget" (string |> andThen 
> primaryWidgetDecoder)
> |> required "primaryWidgetDat"  now I want to pass the relevant 
> loader here but I have no Idea how. tried to use andThen but no luck 
>
>
> Many thanks for reading and helping.
>
> Cheers,
>
>
>

-- 
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: How to write function that updates arbitrary element of a record

2016-10-23 Thread Leroy Campbell
I think I have something close to what you're looking for, but my solution 
makes me wonder, *why store text in your model rather than the parsed 
float/int values?* It seems like you want the parsed values for a 
calculation, not to format a paragraph of text.

update : Msg -> Model -> Model
update msg model =
  case msg of
ChangeDripText dripText ->
  { model
| dripText = valueOrDefault model.dripText String.toFloat dripText
  }

ChangeHoursText simulationHoursText ->
  { model
| simulationHoursText = valueOrDefault model.simulationHoursText 
String.toInt simulationHoursText
  }

ChangeMinutesText simulationMinutesText ->
  { model
| simulationMinutesText = valueOrDefault 
model.simulationMinutesText String.toInt simulationMinutesText
  }


valueOrDefault : a -> (a -> Result x b) -> a -> a
valueOrDefault default test value =
  test value
|> Result.map (always value)
|> Result.withDefault default

On Wednesday, October 19, 2016 at 10:49:03 AM UTC-4, Brian Marick wrote:
>
> I have a model that looks like this:
>
> type alias Model =
>   { ...
>   , dripText : String
>   , simulationHoursText : String
>   , simulationMinutesText : String
>   ...
>   }
>
> Those strings each correspond to a text field containing floating point 
> numbers. The whole thing looks like this:
>
>
> The text fields ensure that the values are all valid when characters are 
> typed, using `onInput` messages.  I would very much like to write something 
> like:
>
> — Ignore the fact that this `update` doesn’t return
> — a `Cmd Msg`. That’s coming.
> update : Msg -> Model -> Model
> update msg model =
>   case msg of
> ChangedDripText string ->
>   updateWhen isValidFloatString dripText string
> ChangedHoursText string ->
>   updateWhen isValidIntString simulationHoursText string
> ChangedMinutesText string ->
>   updateWhen isValidIntString simulationMinutesText string   
>
> … because that shows what’s different about each case. (Especially useful 
> is highlighting which fields accept integers and which floats.) 
>
> However, I think - based on 
> https://lexi-lambda.github.io/blog/2015/11/06/functionally-updating-record-types-in-elm/
>  
> - there’s no way to do that. Which has me writing this copypasta: 
>
> updateNextSpeed model nextString =
>   if isValidFloatString nextString then
> {model | dripText = nextString }
>   else
> model
>   
> updateHours model nextString =
>   if isValidIntString nextString then
> {model | simulationHoursText = nextString } 
>   else
> model
>
> updateMinutes model nextString = 
>   if isValidIntString nextString then
> {model | simulationMinutesText = nextString }
>   else
> model
>   
>
> update : Msg -> Model -> Model
> update msg model =
>   case msg of
> ChangedDripText string ->
>   updateNextSpeed model string
> ChangedHoursText string ->
>   updateHours model string
> ChangedMinutesText string ->
>   updateMinutes model string
>
> Is there a better way to handle this?
>

-- 
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] Outgoing port event ordering

2016-10-17 Thread Leroy Campbell
Ahh...I see. Assuming you have some sort of autosave logic, how about two 
messages: Save and SaveAndQuit? It seems reasonable to me to split the 
actions this way...Save could be scheduled using Time.every and SaveAndQuit 
could be both an explicit user interaction and an incoming port message 
triggered by window.onbeforeunload().

On Monday, October 17, 2016 at 2:26:20 PM UTC-4, David Andrews wrote:
>
> The problem initially arose when I had two ports, one of which wrote to 
> local storage and the other of which closed the window.  Nothing was ever 
> written to local storage because the window closed first.  The reason I 
> went with two ports is because those actions are so conceptually different, 
> and if it were actually just for local storage I would have used a single 
> port.  If it were not for the fact that JavaScript processing ceases after 
> calling window.close, the current port implementation would have worked for 
> me.
>
> On Oct 17, 2016 2:03 PM, "Leroy Campbell" <artis...@gmail.com 
> > wrote:
>
>> From what I can tell, port communication uses Cmd because interop with 
>> JavaScript isn't necessarily a request-response communication pattern 
>> (instead, port are pubsub).
>>
>> But I do have a question: *Is the underlying problem a need to 
>> coordinate access to a shared resource in JavaScript? *I ask because 
>> you mentioned localStorage in your initial message. I imagine you'd instead 
>> want to leave the coordination in Elm to take advantage of Elm's 
>> concurrency model (immutable data + message-passing) and have a single port 
>> to talk to JavaScript.
>>
>> On Monday, October 17, 2016 at 3:03:14 AM UTC-4, David Andrews wrote:
>>>
>>> In another discussion, I was pointed to 
>>> http://faq.elm-community.org/17.html#what-is-the-difference-between-cmd-and-task,
>>>  
>>> which sheds some light on the issue, but also raises a few questions.
>>>
>>> Specifically:
>>>
>>>1. The article mentions that APIs generally expose Task in favor of 
>>>Cmd. Why is the port API a -> Cmd msg instead of a -> Task Never () 
>>>or something like that?
>>>2. Is there a recommended way to pass data to ports in order? I've 
>>>come up with the workaround of sending over only one port per update and 
>>>using Cmd.Extra.message to trigger additional updates immediately, 
>>>but I don't think it's very clean.
>>>
>>>
>>> On Monday, October 17, 2016 at 2:39:01 AM UTC-4, Peter Damoc wrote:
>>>>
>>>> On Mon, Oct 17, 2016 at 8:02 AM, Janis Voigtländer <
>>>> janis.voi...@gmail.com> wrote:
>>>>
>>>>> Peter, the problem in David’s case is that the actions he wants to 
>>>>> order execution of are port data sending, and there is no “something 
>>>>> lower 
>>>>> level, like Tasks” for that. The only API available for port data 
>>>>> sending is Cmd-based.
>>>>>
>>>> Ooops... my bad. I should have looked more carefully. 
>>>>
>>>>
>>>>
>>>> -- 
>>>> 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/cSzJT2-g8Ss/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to 
>> elm-discuss...@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] Outgoing port event ordering

2016-10-17 Thread Leroy Campbell
>From what I can tell, port communication uses Cmd because interop with 
JavaScript isn't necessarily a request-response communication pattern 
(instead, port are pubsub).

But I do have a question: *Is the underlying problem a need to coordinate 
access to a shared resource in JavaScript? *I ask because you mentioned 
localStorage in your initial message. I imagine you'd instead want to leave 
the coordination in Elm to take advantage of Elm's concurrency model 
(immutable data + message-passing) and have a single port to talk to 
JavaScript.

On Monday, October 17, 2016 at 3:03:14 AM UTC-4, David Andrews wrote:
>
> In another discussion, I was pointed to 
> http://faq.elm-community.org/17.html#what-is-the-difference-between-cmd-and-task,
>  
> which sheds some light on the issue, but also raises a few questions.
>
> Specifically:
>
>1. The article mentions that APIs generally expose Task in favor of Cmd. 
>Why is the port API a -> Cmd msg instead of a -> Task Never () or 
>something like that?
>2. Is there a recommended way to pass data to ports in order? I've 
>come up with the workaround of sending over only one port per update and 
>using Cmd.Extra.message to trigger additional updates immediately, but 
>I don't think it's very clean.
>
>
> On Monday, October 17, 2016 at 2:39:01 AM UTC-4, Peter Damoc wrote:
>>
>> On Mon, Oct 17, 2016 at 8:02 AM, Janis Voigtländer <
>> janis.voi...@gmail.com> wrote:
>>
>>> Peter, the problem in David’s case is that the actions he wants to order 
>>> execution of are port data sending, and there is no “something lower level, 
>>> like Tasks” for that. The only API available for port data sending is 
>>> Cmd-based.
>>>
>> Ooops... my bad. I should have looked more carefully. 
>>
>>
>>
>> -- 
>> 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] Difficulty learning Elm

2016-09-27 Thread Leroy Campbell
Rather than decode to nested Dict types, you could create types that represent 
your domain better (assuming the underlying JSON is well-structured). You can 
also use `andThen` to decode based on some field in your objects.

-- 
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] Private state: A contrived example

2016-09-09 Thread Leroy Campbell
Mark,

I don't understand why so much concern about private state. The reason 
components (in JS frameworks) need private state is to protect them from 
accidental mutation by callers. With immutable data, I don't see a need to hide 
data.

I've had to maintain several 10K+ JavaScript codebases. The number one problem 
I've had is mutable state spread throughout those apps. Unit tests don't help 
here; sure components work fine in isolation, but no amount of disciple saves 
you from bugs where a reference is leaked from a nested data structure then 
mutated in a closure somewhere else.

Do you have a real case for components in the way you propose? To me, it 
doesn't seem like you've tried to build anything substantial with Elm to 
understand Evan and Richard's point of view on the matter.

-- 
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] What concepts and models did you had to learn in order to be able to write good Elm code?

2016-08-11 Thread Leroy Campbell
I'd say learning how to make illegal states unrepresentable.

https://vimeo.com/162036084

-- 
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] Re: port module for JS interop.. without main/view ?

2016-07-28 Thread Leroy Campbell
Just to clarify, inbound messages to Elm are batched (enqueued) and then 
executed on nextTick.

And to echo Luke, observables would make more sense. A Promise-based 
interface would mean supporting only request-response semantics. 
Observables not only allow for request-response, but also publish-subscribe 
semantics.

For example, suppose you have an Elm worker that processes stock tickers. 
You send an inbound message to subscribe to one or more ticker symbols. An 
outgoing port would continue to stream updates until you send a message to 
unsubscribe from a particular ticker symbol (or all). I'm not sure how 
you'd pull that off with Promises.


On Wednesday, July 27, 2016 at 9:44:27 PM UTC-4, Yonatan Kogan wrote:
>
> For us, we have multiple inbound ports and some outbound ports (inbound to 
> elm from js, outbound to js from elm) so it's more useful.
>
> The send from Elm to JS is synchronous (newly in 0.17 I believe?) but 
> going from JS to Elm is not, as it seems like Elm waits until nextTick to 
> pick up the message (understandably). We still want to be able to say 
> "after elm has processed this" though. The analogy would be "after this 
> Ajax request has returned," so a Promise interface seems to make sense 
> (again, I know nothing about Observables).
>
> I don't think this is stable, but it does seem like the following is 
> effectively the same:
>
> ```js
> elmApp.ports.portName.send(message);
> setTimeout(function() {
>   // use newly updated data
> });
> ```
>
> Also, writing that code highlights for me that our need is somewhat 
> specific to elm updating a redux/flux store and we then want to take action 
> on the updated store.
>
> On Wed, Jul 27, 2016 at 12:36 PM, Jörg Winter  > wrote:
>
>> Just to clarify, I talk about calling Elm from JS.
>>
>> --
>> 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.
>>
>
>
>
> -- 
> Yonatan Kogan
>
>

-- 
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: How do I organize "inheritance" between my data types?

2016-07-18 Thread Leroy Campbell
It may help to watch this talk on modeling from a functional programming 
perspective (features F#, but applicable to Elm since they both are 
ML-based):

https://vimeo.com/162036084

On Sunday, July 17, 2016 at 7:06:32 PM UTC-4, Leonardo Sá wrote:
>
> Short question:
>
> What is the best way to create relationships between my types so it is 
> easy to access data present in both types?
>
> Long question:
>
> Suppose I have the following types:
>
> type alias Person =
>   { name : String
>   , address : String
>   , personType : PersonType 
>   }
>
> type alias Employee =
>   { department : String }
>
> type alias Customer =
>   { itemsPurchased : Int }
>
> type PersonType = EmployeeType Employee | CustomerType Customer
>
> Then I think it's not straight forward to write a function that retrieves 
> me the department and name for an employee:
>
> nameAndDepartment : Person -> (String, String)
>
> It seems to me this function would be a Maybe (String, String) and return 
> Nothing if the Person is not an Employee. But in that case, I am relying on 
> the runtime to type check things for me, which tells me there is probably a 
> better way to structure this.
>
>

-- 
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: Port Error

2016-07-18 Thread Leroy Campbell
I recreated your example without issue, so I wonder if there is another 
problem. Here's a fork of your gist:

https://gist.github.com/artisonian/11e93321cd7fd108142115269cbeafe7

On Monday, July 18, 2016 at 4:40:06 AM UTC-4, Zachary Kessin wrote:
>
> I am having a strange problem with ports, I am trying to send a list of 
> data through the port (see gist) and I it is giving me a runtime error. 
>
> Actually it seems to have nothing to do with the data, as I am trying to 
> send a string through a different port in the same file and getting the 
> same error. 
>
> Some ports in other files work ok
>
> https://gist.github.com/zkessin/e5b61e80f2d280e5496f60a8e42f0c79 
> 
>
> -- 
> Zach Kessin
> Twitter: @zkessin 
> Skype: zachkessin
> ᐧ
>

-- 
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: Union type list

2016-05-22 Thread Leroy Campbell
I assume if it were possible to have lists with mixed types, you'd lose 
some algebraic guarantees. How would you use such a list? There's probably 
another way to accomplish the same thing while retaining type safety.

On Friday, May 20, 2016 at 1:41:38 PM UTC-4, John Orford wrote:
>
> Why isn't something like
>
> List (A | B)
>
> possible?
>

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