News for hackers. Stuff that matters.
Re:Functional data structures in JavaScript with Mori by swannodette on Monday November 11, 2013 @05:06PM (#6714408)
One nifty thing not covered here is that Mori also has an extremely efficient O(1) thaw to get a mutable copy of an immutable value and a O(1) freeze to get a persistent value from a mutable data structure (transient).

The performance of a transient vector is shockingly close to using a JavaScript array under Google V8 for adding new elements:

http://github.com/swannodette/mori#efficient-freezethaw

Re:Functional data structures in JavaScript with Mori by rdtsc on Monday November 11, 2013 @05:38PM (#6714604)
Of course it is obligatory to mention the classic: Okasaki's "Purely Functional Datastructures" book.

http://www.amazon.com/Purely-Functional-Structures-Chris-Oka...

Also check out this link for additional data structures worked on since then or simply not included in the book:

http://cstheory.stackexchange.com/questions/1539/whats-new-i...

Re:Functional data structures in JavaScript with Mori by swannodette on Monday November 11, 2013 @05:44PM (#6714638)
A minor nit, most of the data structures provided through Mori via ClojureScript are not based heavily on Okasaki's work, rather they are based on Bagwell's paper on mutable Ideal Hash Trees http://lampwww.epfl.ch/papers/idealhashtrees.pdf and Rich Hickey's efficient immutable implementations in Java for Clojure
Re:Functional data structures in JavaScript with Mori by Arnor on Monday November 11, 2013 @05:43PM (#6714634)
Is this more required reading? Has anyone else's homework load doubled since college?
Re:Functional data structures in JavaScript with Mori by tieTYT on Monday November 11, 2013 @04:41PM (#6714262)
This is great. I never understood why underscore/lo-dash doesn't have a `conj` or a `concat`.

JavaScript's `push` mutates data which you'd think you'd want to avoid if you're using a functional library like underscore/lo-dash.

`concat` makes sense to leave out when you consider that arrays already have a `concat` function. But if that's the reasoning, why did underscore/lo-dash include `size` and `max` and all those other things that already exist?

Re:Functional data structures in JavaScript with Mori by jkrems on Tuesday November 12, 2013 @01:11AM (#6716154)
One thing I never understood about - awful generalization - "real" functional languages is their fascination with terrible naming. What I mean is things like `car`, `cdr`, `conj` - words that are impossible to understand if you don't already know what they mean.
Re:Functional data structures in JavaScript with Mori by tikhonj on Tuesday November 12, 2013 @06:53AM (#6717209)
That might just be an age thing: older languages seem particularly bad at this. Lisp has its cars and its cdrs, but C is even worse with ioctl, atoi, fputc, strxfrm and so on.

Assembly is worse yet, and mixing C with assembly is basically a path to Lovecraftian madness: I really hate the way intrinsics look. Here's a real snippet from StackOverflow:

    a_i = __mm_load_ps(&A[n*i+k]);
    b_i = __mm_load_ps(&B[n*k+j]);
    c_i = __mm_load_ps(&C[n*i+j]);

    __m128d tmp1 = __mm_mul_ps(a_i,b_i);
    __m128d tmp2 = __mm_hadd_ps(tmp1,tmp1);
    __m128d tmp3 = __mm_add_ps(tmp2,tmp3);
    __mm_store_ps(&C[n*i+j], tmp3)
You can also go too far in the other direction: see Java and especially Objective C.
Re:Functional data structures in JavaScript with Mori by zachrose on Tuesday November 12, 2013 @01:35AM (#6716229)
It's the same with C-like syntaxes, or OOP, or *nix stream commands, or the English language, or the phonetic alphabet. It's all a thick pile of history that has been agreed upon piece by piece.
Re:Functional data structures in JavaScript with Mori by camus2 on Tuesday November 12, 2013 @04:11AM (#6716654)
I guess there was a time where one had to care about the length of the source code.

that's why some came up with these abbreviations.

Re:Functional data structures in JavaScript with Mori by lmm on Tuesday November 12, 2013 @05:07AM (#6716854)
That's only lisp and particularly faithful derivatives (unfortunately including clojure). Haskell uses more natural names - head, tail, last.
Re:Functional data structures in JavaScript with Mori by mjn on Tuesday November 12, 2013 @07:16AM (#6717304)
Even most Lisps have first/rest as synonyms of car/cdr (dating back to some very old Lisps, and mandated by the Common Lisp standard). They just don't seem to have culturally caught on as idiomatic.
Re:Functional data structures in JavaScript with Mori by augustl on Monday November 11, 2013 @05:36PM (#6714592)
I like that the article calls then "functional data structures". Every time I call them by their real name (persistent data structures), _at least_ one person will get confused and think I'm talking about serialization to disk.
Re:Functional data structures in JavaScript with Mori by pmahoney on Monday November 11, 2013 @06:24PM (#6714818)
Is is possible to construct a persistent data structure that is not referentially transparent? I suspect the answer is yes, depending on how you define side effects (example: a persistent array that logs operations).

Which would mean "functional" is a more general adjective than "persistent" since it includes both persistence and referential transparency.

Wikipedia[1] claims "There also exist persistent data structures which use destructible operations, making them impossible to implement efficiently in purely functional languages"

[1] https://en.wikipedia.org/wiki/Persistent_data_structure

Re:Functional data structures in JavaScript with Mori by augustl on Tuesday November 12, 2013 @09:33AM (#6717991)
"Persistent" is but a bullet point on Clojure's own explanation of its data strucures, so I suppose we as a community should follow in the official docs' footsteps :)

http://clojure.org/data_structures

Re:Functional data structures in JavaScript with Mori by theothershoe on Monday November 11, 2013 @09:46PM (#6715538)
I consistently see the same problem. That is exactly why I use the word "functional".
Re:Functional data structures in JavaScript with Mori by TheZenPsycho on Monday November 11, 2013 @10:37PM (#6715703)
what's wrong with "immutable" ?
Re:Functional data structures in JavaScript with Mori by augustl on Tuesday November 12, 2013 @01:10AM (#6716151)
Immutable does not imply persistence/structural sharing, a key aspect of these data structures.
Re:Functional data structures in JavaScript with Mori by agumonkey on Monday November 11, 2013 @07:52PM (#6715162)
IIUC they pulled out clojure's clojurescript projected core structures so you can use it ~pojo ?

Recenlty I saw https://github.com/harukizaemon/hamster , similarly immutable, structure sharing, persistent data structures

It's becoming mainstream ...

Re:Functional data structures in JavaScript with Mori by ds_ on Tuesday November 12, 2013 @05:41AM (#6716982)
Is there a current build anywhere or do I have to build it myself?
Re:Functional data structures in JavaScript with Mori by theothershoe on Tuesday November 12, 2013 @07:20AM (#6717318)
Install the npm package, mori. You can require it in a NodeJS project, or grab the file mori.js from the installed package and drop it in a web browser.