News for hackers. Stuff that matters.
Re:Clojure – Fascinated, Disappointed, Astonished (2016) by bcrosby95 on Friday January 24, 2020 @02:27PM (#22141598)
It's missing my favorite concurrency construct: immutable, persistent data structures. I was initially interest in Clojure for things like STM, but found most of my concurrency problems are amenable to immutable data. You can certainly use this technique in other languages, but it tends to be higher effort.
Re:Clojure – Fascinated, Disappointed, Astonished (2016) by Scarbutt on Friday January 24, 2020 @03:15PM (#22142011)
Once javascript gets immutable types and better handling of objects, the gap between it and clojure will very small for most daily tasks.

https://github.com/tc39/proposal-record-tuple

https://github.com/tc39/proposal-object-iteration

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by adamkl on Friday January 24, 2020 @03:41PM (#22142226)
The issue that I find with JavaScript (which I use on a daily basis) is that it has become a "kitchen-sink" language. Every time it gets new additions to "close the gap" with some other language, it gains more ways to do the same things.

A big thing with Clojure is that it tries to provide a single, idiomatic way of doing things (though overuse of macros may cloud this picture at times). Generally, you can hop into any Clojure codebase and very quickly understand what's what.

Since JavaScript can do just about anything (functional, imperative, object-oriented, prototypical), there's no one "true" way of writing code, so while its possible use the "good parts" of JavaScript, everyone seems to have a different idea of what those are.

Flexibility can be a virtue, but I find myself preferring Clojure due to the constraints it provides.

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by kace91 on Friday January 24, 2020 @07:23PM (#22143627)
As you say, there is no objective "good parts" of js, what exists is basically a large language that can act as a framework for you to create a language you like by leaving only what you want in the subset allowed by your linters.

That's arguably a lot of work, but the payoff is that you get to use your new hand tailored "language" anywhere js can run, which is not the case if you ignore js and just pick a language you like from the get go instead.

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by adamkl on Friday January 24, 2020 @08:06PM (#22143933)
If you are flying solo, then sure, I can see the appeal, but for most(?) of us, that hand-tailored language needs to be learned by everyone you work with, and anyone who might need to work on your codebase in the future. And it’s unlikely that those future coders will feel the same about your hand-tailored language as you do.

(editing to mention the fact that Clojure targets both servers (via JVM/CLR) and browsers (via ClojureScript) so you lose none of the reach you mention with JavaScript)

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by hinkley on Friday January 24, 2020 @04:41PM (#22142743)
Does anything illustrate this more clearly than the picture of 1e 'Javascript, the Good Parts' sat next to the O'Reilly Javascript book?
Re:Clojure – Fascinated, Disappointed, Astonished (2016) by zachrose on Saturday January 25, 2020 @12:15AM (#22144854)
That image was from a decade ago. The O'Reilly book must be three or four times thicker by now.
Re:Clojure – Fascinated, Disappointed, Astonished (2016) by yogthos on Friday January 24, 2020 @04:10PM (#22142494)
That is absolutely not the case. There's a huge difference between having immutable constructs available in a language and immutability being the default.

Clojure is built around the idea of immutability from ground up. It makes it natural to work with immutable data, and it guides you towards doing the right thing. An example of this would be transients, which allow you to do local mutation within a function, but the compiler will give you an error if you try to leak mutable state. All of this greatly reduces mental overhead for the developer because vast majority of code is referentially transparent. You can reason about functions in isolation without having to know anything about the rest of the application.

Meanwhile, simply bolting on immutable constructs on top of JavaScript puts the burden of using them correctly squarely on the developer. It's still perfectly possible to stick a mutable reference into your immutable record, and then pass it around and modify it. The language does nothing to help you there. Furthermore, you have to consider the culture and the ecosystem around the language. Pretty much all Js libraries are written in imperative style, and passing mutable data is the accepted practice.

And immutability is just one aspect, there are plenty of other differences that are very important for many daily tasks. One major difference is that Clojure is a much smaller and consistent language. Clojure code tends to follow a few accepted patterns that the community settled on. Js is a giant language that's grown organically and acquired tons of quirks and gotchas over the years. It's a minefield compared to Clojure.

Another day to day benefit of using Clojure is its tooling. You get reliable hot loading, which is pretty much impossible with Js, you have sane library management, minification, code pruning to function level, and code splitting out of the box. These features are especially important when shipping code across the network to the browser.

You also get a REPL where you can connect your editor to your running application, and interact with it directly from there. Any time you write a function, you're able to run it immediately and see what it does. There's no waiting for your code to compile, or the page to reload, or having to rebuild the state. It's an incredibly tight feedback loop.

In short, there's no comparison between ClojureScript and Js regardless of what constructs get bolted onto Js in the future.

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by agumonkey on Friday January 24, 2020 @04:22PM (#22142594)
that's how I felt using immutable libs in python, it did help but the language still has open doors and I never really feel like juggling with potential side effect
Re:Clojure – Fascinated, Disappointed, Astonished (2016) by fnordsensei on Friday January 24, 2020 @03:30PM (#22142148)
It’s not only about having immutable constructs available, but having immutability as default, and pervasive throughout the ecosystem.
Re:Clojure – Fascinated, Disappointed, Astonished (2016) by slifin on Friday January 24, 2020 @05:14PM (#22142959)
ClojureScript is a superset of JavaScript, something as simple as writing a loop would very quickly illustrate there is a huge gap between the languages
Re:Clojure – Fascinated, Disappointed, Astonished (2016) by blueberrytea on Friday January 24, 2020 @05:26PM (#22143016)
How so? Not disagreeing with the languages being different, but loops are very similar
Re:Clojure – Fascinated, Disappointed, Astonished (2016) by kubanczyk on Friday January 24, 2020 @03:07PM (#22141943)
No. Immutable structures still require locking.

If you are spending 100 ms calculating the new version of the immutable structure, you want to make sure that the nearby thread isn't making their own version. You don't want any of the two results silently overwritten. You want the nearby thread be sequential - to wait for you and base on your result.

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by dpratt71 on Friday January 24, 2020 @03:28PM (#22142129)
This is not how persistent data structures are used in practice. There is no "the" new version of a persistent structure. The best analogy I can think of is to compare it to a VCS (e.g. git). There is no need to lock any existing commit in order to create a new commit (which together with prior commits, represents a new version of the code).
Re:Clojure – Fascinated, Disappointed, Astonished (2016) by majormajor on Friday January 24, 2020 @03:50PM (#22142321)
I'm not familiar with Clojure, but you can hit conflicts in the Git world, though, which seem to be what the parent is concerned about. Two of us could be creating some new data based on the last data we had, at time T, and then the other person submits theirs at time T+5, and I submit mine at time T+10. In that case, my change hasn't taken theirs into account.
Re:Clojure – Fascinated, Disappointed, Astonished (2016) by escherize on Friday January 24, 2020 @04:09PM (#22142477)
If you can "submit" your change back to the original datastructure then the original datastructure is not immutable, right? Here's a nice explaination about how the persistant immutable datastructures work: https://hypirion.com/musings/understanding-persistent-vector...
Re:Clojure – Fascinated, Disappointed, Astonished (2016) by setr on Friday January 24, 2020 @05:05PM (#22142899)
I believe the question is that, if two threads take the same immutable vector, and both make a change to it independently, they'll end up with two new vectors (eg two branches in git); a vector that reflects thread1's change, and a second vector that reflects thread2's change. So now you have a conflict, which requires resolution; git has a human intervene.

eg

x = [1,2,3]

Thread1 -> x + [4] => [1,2,3,4]

Thread2 -> x + [5] => [1,2,3,5]

But you were expecting [1,2,3,4,5]

Reality was that you wanted an order to your events, normally enforced by locking, which the immutable vector doesn't seem to help you with; they were both able to update independently, but you actually wanted them to update dependently.

If you try to use immutable datastructures to avoid locking, then how is conflict resolution handled?

I think the answer would be that it doesn't help you avoid locking; either you lock & share a single reference to the latest version of your immutable vector, to enforce ordered events, or you define a resolution strategy separately. The immutability aspect just stops you from not having a resolution strategy -- which would always be incorrect

And if I understand correctly, the ideal scenario for immutable datastructures in concurrent scenarios is when you can define such a merge strategy (and safely give threads their own copy of the datastructure to muddle with, without actually having to copy the entire datastructure)

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by dpratt71 on Friday January 24, 2020 @06:04PM (#22143221)
You could, as per your example, use locking as part of a resolution/merge strategy to combine the results of two separate computations running on two separate threads. Or you could use some strategy that does not involve locking. Either way, it does not support the original claim I disputed that "Immutable structures still require locking".
Re:Clojure – Fascinated, Disappointed, Astonished (2016) by setr on Friday January 24, 2020 @06:56PM (#22143514)
>Either way, it does not support the original claim I disputed that "Immutable structures still require locking".

It does, if you believe serialization by locking is the main strategy to handle serialization (in which case, mutable or immutable, you still need to lock), and so... you still need locking. Serialization being the main scenario GP gave.

Your original answer didn't resolve the problem either -- fine, you didn't need to lock when adding elements to your immutable structure, but you still haven't reached serialization; you've just pushed the problem back another step.

The answer that I believe GP would need to correct his understanding, (and much more importantly, the answer that I'm interested in :-) is what serialization strategies does immutable datastructures enable, if not locking?

The other correction GP seems to require is whether serialization is actually that important in general, and whether functional programmers tend to experience otherwise... But I don't care about that answer :-)

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by Scarbutt on Friday January 24, 2020 @07:37PM (#22143725)
the answer that I'm interested in :-) is what serialization strategies does immutable datastructures enable, if not locking?

Depending on your performance goals, Compare-and-Set with retries a la clojure's atom reference construct.

https://clojure.org/reference/atoms

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by dpratt71 on Friday January 24, 2020 @08:11PM (#22143961)
Oh goodness :) OP has conceded the point, but you're still down to argue on the basis of what a person may or may not believe is the main strategy to handle serialization. I give up. You win, I guess.

Regards your other question(s), I will just add that I answered many similar questions for myself (as well as disabusing myself of a lot of misconceptions) by undertaking to get a basic understanding of Haskell.

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by jfim on Friday January 24, 2020 @10:28PM (#22144441)
You're assuming that x+[5] mutates the list instead of returning a new list.

If you're planning to have multiple threads append items to a list, the immutable way to do it is to have each thread return a list of items to append to the main list, then fold those items into the list.

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by reitzensteinm on Friday January 24, 2020 @11:37PM (#22144716)
No, parent post is definitely not assuming that.
Re:Clojure – Fascinated, Disappointed, Astonished (2016) by dpratt71 on Friday January 24, 2020 @05:15PM (#22142966)
In brief, these sorts of conflicts simply do not arise in a fully persistent data structure. You may have a situation where you have a persistent data structure together with one or more mutable references, each to some version of the data structure. Yes, a modification to one of these mutable references would need to be synchronized, but they are separate from the persistent data structure itself.

Again using git as an example, there is the persistent data structure, aka the commit graph, as well as mutable references to commits, aka branches. A change to what commit a branch references needs to be synchronized.

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by kubanczyk on Friday January 24, 2020 @05:55PM (#22143169)
I concede. If you have algorithm to do the `git merge` equivalent without human help, I guess no locking or STM is needed. Although that's very costly in implementation.

It's great to have git as a mental model in this discussion, really useful.

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by dpratt71 on Friday January 24, 2020 @06:22PM (#22143322)
Whether the "merge" implementation is costly or complicated very much depends on exactly what it has to do. The git example is pretty much a worst case example in this regard.

An easier example could be that you have a tree structure that represents a mathematical expression. The evaluation of every node could proceed on its own lightweight thread. The merge strategy would be to simply perform the appropriate operation on the results produced by the threads evaluating the child nodes.

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by kubanczyk on Saturday January 25, 2020 @04:10AM (#22145493)
I don't get math expressions example at all. One thread modifies one value, so there is no (mutability) problem to solve.

You have customers' orders to buy items. One last item remains at your store. You accept one order and update HEAD. You accept another order in parallel and follow to merge. "Merge" here means that you need to return money to the customer and send out an apology e-mail.

More cumbersome than locking, isn't it? But possible, yes.

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by dpratt71 on Saturday January 25, 2020 @08:36AM (#22146172)
Yeah, you're right, I muddied the waters a bit with that example. I was just trying to think of examples of persistent data structures being used in a concurrent context, but you had in mind a situation where multiple agents could be updating the same data structure independently.

Your example is much better. I also was thinking of maintaining an account balance with a log (vs. synchronising updates to a stored amount), but it's not much different from your example.

And of course this "eventually consist" strategy is generally how things happen "at scale", persistent data structures or not.

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by bjoli on Saturday January 25, 2020 @03:14AM (#22145356)
That would be problematic using non-immutable datastructures as well as you might end up with incorrect order or even nonsensical data if you don't use locking.

Having used immutable data structures and concurrency in non-clojure languages, I mostly resort to something like concurrentML for concurrency. Message passing lets you solve situations like that in more elegant ways.

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by reitzensteinm on Friday January 24, 2020 @04:46PM (#22142779)
Clojure generally uses CAS semantics, although it also has transactions that can update multiple refs atomically. So nothing is ever "silently overwritten" and there's no locking required, it's all optimistic concurrency.

However, to agree with part of your point, an atom being updated with CAS can only ever progress linearly; if you bang 8 threads in a loop trying to update it, you might as well use a lock, because 7/8 of the work is going to be thrown out.

They should only be used for cases where on average one thread or less is working on updating it; anything more, and it needs to be split, the problem rethought, or some mechanism for not throwing out wasted work implemented.

It's not a silver bullet.

Re:Clojure – Fascinated, Disappointed, Astonished (2016) by Scarbutt on Friday January 24, 2020 @03:12PM (#22141971)
If he's talking about multithreading he probably wanted to say immutable data structures in combination with clojure's atom (CAS semantics).
Re:Clojure – Fascinated, Disappointed, Astonished (2016) by samatman on Friday January 24, 2020 @04:10PM (#22142486)
Please read the first paragraph of this page and see if your objection still stands:

https://clojure.org/about/concurrent_programming