Think of strings in JavaScript. Those are already immutable:
var fooString = "foo";
var secondFooString = fooString;
secondFooString; // => "foo"
fooString = "bar";
secondFooString; // => "foo"
We set the variable fooString to point to a different string, but the original, underlying string hasn't changed. In JavaScript, we can think of a string as a value.
This is not the case with arrays in JavaScript:
var firstArray = [1, 2, 3];
var secondArray = firstArray;
firstArray[0] = 100;
firstArray; // => [100, 2, 3]
secondArray; // => also [100, 2, 3]
Because we can change the underlying contents of the array, an array in JavaScript isn't a value. It's a place: a reference to a location in memory. The underlying value could be changed at any time.
But, using Mori, collections are values, just like strings:
var firstVec = m.vector(1, 2, 3);
var secondVec = firstVec;
firstVec = m.assoc(firstVec, 0, 100);
firstVec; // => [100, 2, 3]
secondVec; // => still [1, 2, 3]
Instead of modifying firstVec in place, mori.assoc creates a new vector that is identical to firstVec except for the change we want. We then assign the result to firstVec. secondVec is unchanged. We are unable to go in and change the underlying values because a vector is a value, not a place.
The most obvious way to build this would be to deep-copy the entire collection when it's changed, but that would of course be way too slow and wasteful — imagine copying a one-million-long array just to change one element. Clojure, ClojureScript and Mori minimize unnecessary copying using a very thoughtfully designed data structure you can read about here: http://hypirion.com/musings/understanding-persistent-vector-... The short story is that, surprisingly, you get "effectively O(1)" copying when you use assoc.
Did you ever look at Clojure? If not, why not? If so, what didn't you like?
While Clojure surely has a bigger learning curve than Go, it's much simpler and more approachable than Scala. I've learned it recently and am an absolute convert. It seems perfect for your use case and you could even skip writing the prototype in Rails because you'll be just as productive in Clojure.
Note that I'm not trying to convince you to change; you obviously found something that works for you. But I am curious if there were obstacles to using Clojure (missing libraries? poor tutorials?) and if so, how that could be fixed.
Thanks! I will give it a shot :) The main reason I chose Go was for the learning curve for my fellow devs. But if clojure is only slightly higher in complexity, I would definitely give it a shot..thanks :)
A Lisp family language is hardly more approachable than Scala. I found Java -> Scala pretty smooth, but can't make head or tail of Clojure code as it looks completely different to languages I've used before.
Interesting perspective, thanks. I suspect it depends on whether you're more used to langs like Ruby, Python, JavaScript (which are more like Clojure) or Java (which is more like Scala). Coming from Python and JavaScript, and with minimal Java experience, I find Clojure more approachable... but then, Java-style OO is utterly vexing to me.
However, I'm surprised that you single out a 40-hour work week as though it's something exceptional. Isn't that the standard? I would never consider joining a company that expected me to work weekends. I hope that's not as common as you imply.
As a fellow Clojurist, I agree with the substance of what you're saying here but wish you could express it in a more friendly manner. Both your comments essentially say "you're wrong" without educating or adding value. I don't feel that reflects well on the Clojure community, and I'd like us to do better.
> Immutable arrays, where the entire array must be copied with the change of one element, are an example of that.
Only in a naive implementation. Clojure, for example, has a persistent vector that only requires O(log32 n) copying, which grows so slowly as to be effectively O(1).
What is a fan? Someone who gave your music a thumbs up? Or something willing to pay for the music you produced because they desire it so much? I have a feeling if you have hundreds of thousands of the latter you'd be in good shape.