So now, either I write a ports for commonmark.js, or I write it as a native 
>> module. I asked about it here 
>> <https://groups.google.com/forum/#!topic/elm-discuss/Kd53qnKY-io> with 
>> no answers. 
>>
>
> I think if you write it as ports or native, you'll still need to map the 
> AST between javascript and Elm. As a native module that could be done with 
> a Decoder or by constructing the Elm AST in native code with Javascript. 
> Perhaps Decoders are not so bad?
>

In the case of using commonmark.js, I didn't use a decoder. Taking a page 
out of the elm-markdown playbook, I call directly into VirtualDom's js calls 
<https://github.com/evancz/elm-markdown/blob/3.0.1/src/Native/Markdown.js#L12>. 
My guess is that Evan did it as a hack. And since it's not a published API, 
I don't expect it to stick around. Reaching into a private API is usually 
bad news. I chose not to write decoders in this case, because I'm using 
commonmark.js already, so it's not like I could share the code as a lib. 
And it's admittedly a shortcut.

In the case of parsing SQL, I did end up using a decoder. I sent the sql 
string into a port, had it parsed and returned as JSON representing an AST, 
that fed back into a subscription port that I then decoded into union types 
<https://groups.google.com/forum/#!topic/elm-discuss/RqKfzeM35f0>.
 

> I don't think a parser is a side-effect. A parser is a pure function from 
> String -> Ast, with no side effects.
>

Technically, no. That's why I was asking about a native module API, since I 
just copied elm-markdown.
 

> The thing about ports for something like this is it feels a bit unnatural 
> to invoke the port resulting in a Cmd. Then you'll need a subscription port 
> to get the result back in, and have that pass it to your update function 
> from where you can take it and place it in the model. That doesn't feel 
> like a good way to call a function : String -> Ast.
>

Well, since there's no official documentation on a native module API, ports 
is what I can rely on. But from a semantics point of view, since I'm 
calling out to something that might have a side effect, it makes sense. 
It's just the mechanics is a bit awkward. I've had the pleasure of swimming 
in his design decisions in Elm so far, and he's built up enough cred with 
me that I think he'll figure something out.

I was just looking for some guidance as to what to implement in pure elm, 
and what to leverage native modules for. Even in other languages where new 
libraries are implemented completely in that language (like ruby, per the 
original post on native modules a year ago), there are libraries that call 
out. The main one that comes to mind is the Linear Algebra Package (LAPACK) 
originally written in Fortran, but most languages that use it now are 
usually just wrappers around it. I just wondered where Elm community drew 
the line.

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.

Reply via email to