8 ms·
Clojure’s Approach to Identity and State (2008)
- feniv 7y agoRich Hickey has a talk on this (The Value of Values) here: https://youtu.be/-6BsiVyC1kM https://youtu.be/-6BsiVyC1kM
- thomk 7y agoThank you, this talk was paradigm shifting AND familiar for me at the same time.
- z3t4 7y agoWould be helpful with practical examples in code. As a self thought programmer I dont know what all concepts are called, but when I see code I can usually recognise them. The actor model as described in the article becomes less painful when you have an abstraction layer. The question might be are you going for horizontal scaling or vertical scaling, although you are best off implementing the simplest solution in order to avoid premature optimization (and overengineering).
- microcolonel 7y agoRegarding values: you can construct the same datastructure in any place, and compare it meaningfully with a datastructure from a completely different source (and you can do so efficiently). This is accomplished, as far as I know, by representing almost everything as persistent hash trees (with some implementation voodoo and shortcuts). Beyond that, you can actually just read the Clojure runtime code. It's a bit messy but there's not really that much there.
- louthy 7y agoPersistent hash tries [1] I have an efficient C# implementation here [2] [1] https://michael.steindorfer.name/publications/phd-thesis-efficient-immutable-collections.pdf https://michael.steindorfer.name/publications/phd-thesis-eff... [2] https://github.com/louthy/language-ext/blob/master/LanguageExt.Core/DataTypes/TrieMap/TrieMap.cs https://github.com/louthy/language-ext/blob/master/LanguageE...
- microcolonel 7y agoI love seeing these datastructures show up in more languages. They completely change the set programs you could feasibly find time to write. Thanks for sharing your C# one, I'll remember that if I ever need to use Unity again. The claims about CHAMP are very impressive, in my experience, Clojure's datastructures perform great for what they do, and they claim CHAMP tends to be many times faster. :- ) > Compressed Hash Array Map Trie Q: “What datastructure would you like?” A: “Yes.”
- johnday 7y agoAgreed. I went in hoping to see some code snippets.
- lvh 7y agoI think Rich Hickey's (the creator of Clojure) talk, The Value of Values, to be a great 'splainer: https://youtu.be/-6BsiVyC1kM https://youtu.be/-6BsiVyC1kM
- microcolonel 7y agoThis has been extremely useful to me while writing a (somewhat optimizing) compiler for spreadsheets. I can do subtree deduplication just by `assoc`ing into a map.
- neonate 7y agoI want to hear more about your somewhat optimizing compiler for spreadsheets!
- kesava 7y ago+1
- microcolonel 7y agoIt's proprietary for the time being; but in short, it is more straightforward than I thought it would be. We are working with LibreOffice Calc ODS sheets, which are pretty terrible as a format (since the references are not normalized in the formulas, they can't repeat them even when they behave identically, and they duplicate most of the XML namespaces in the attributes). We parse and normalize the references from A1 to R1C1 form, and then deduplicate the formulas (by text) and extract all of the immediates (and mark some of them as input, so that they can be varied at runtime). Then we pass the deduplicated formulas through instaparse (which is spectacular) with a relatively simple grammar, and propagate some of the constants. I then extract the references from the AST, while at the same time replacing SUMIF/MINIFS/MAXIFS/AVERAGEIF and similar with simple addition/min/max of known cells, where the tests are known at compile time. Then those ASTs are complied to functions (ignoring our cross-function optimizations). Then it's just down to generating a complete DAG of dependencies, and using that to sort the assignments (cells) topologically. The sheet can be evaluated naiively at that point by injecting the references into each subsequent assignment/cell and storing the result in a map (ranges injected as a seq over a range). There's a lot more to it, and it's getting better all the time, but that's the gist of it. Many real spreadsheets are not well-behaved, and they have dependency patterns which are more difficult to handle (i.e. ranges that refer to the current cell, or future cells, dynamically). The compiled output is getting more and more static, and will probably be reduced to some form of ssa, possibly even well-formed enough to be popped casually into LLVM. It would be some help if the ODS format were improved, it takes several seconds just to parse the hundreds of megabytes of XML in our amazing spreadsheet, and a lot of it is redundant.
- etbebl 7y agoThis is interesting. I've tried Clojure, and heard about the idea of avoiding mutable data and using pure functions plenty of times, but imperative/OOP have still always made the most sense to me. When reading this though, something clicked because I've encountered the problem of getting a stable state to read/write without blocking other operations, and dealt with it in C++ in a similar way to Clojure without realizing it at the time. I have this little lightly-tested library: https://github.com/tne-lab/rw-synchronizer https://github.com/tne-lab/rw-synchronizer. I'm not using it much currently but have played with it a lot while building extensions to Open Ephys. The idea being that as a reader, you get a "snapshot" of the last thing that was written, but it's really just one of several copies, and subsequent writes can happen on the other copies. So you never really modify the current data, just push newer versions of it. The cool thing is, if you know how many simultaneous readers you'll need ahead of time, all the allocation can be done upfront, so then if you have a real-time loop or something, all it needs to do is exchange pointers. If I ever get around to it, the next thing I would do is allow any writer to also read the latest value, so it can use a transformation to create a new one. Maybe even do it automatically with copy-on-write semantics? On the other hand, I'm probably reinventing the wheel here...
- agumonkey 7y agoIIUC Rich Hickey probably did just that too, writing ad-hoc version of clojure semantics in cpp before making his own language enforcing this.
- 6thaccount2 7y agoI recall he was big into SBCL, but most IT organizations wanted all code to run on the JVM or CLR. So he had to make a Lisp to run on the JVM and Armed Bear Common Lisp apparently wasn't exactly what he wanted. I want to learn Clojure, but there are definitely some road blocks. I don't have the time for Emacs, it'd be a pain to get a Cursive license (although the cost is extremely reasonable I'd have to do paperwork at work), and I don't know the JVM or Java well.
- tosh 7y ago
- stuhood 7y agoImmutability is very helpful, and the connection between values and identity is illuminating. But there have been very important developments in programming languages since this post/page was written: notably, the introduction of "borrow checking" (exemplified by Rust's implementation). Borrow checking has a very significant positive effect on the sustainability of imperative code, which makes the claim that "imperative programming is founded on an unsustainable premise" feel dated. It is worth taking the time to understand what borrow checking enables. For example: borrow checking allows even mutable datastructures to be treated as values with structural equality. It does this by guaranteeing that unless you have exclusive access to something, it may not be mutated. A good explanation of the benefits of ownership and borrow checking: http://squidarth.com/rc/rust/2018/05/31/rust-borrowing-and-ownership.html http://squidarth.com/rc/rust/2018/05/31/rust-borrowing-and-o...
- stingraycharles 7y agoBorrow checking and immutability solve two different problems. Immutability is about the absence of ownership and state, while borrow checking is a way to manage ownership. One does not replace the other, they coexist solving different problems.
- louthy 7y agoExactly, it's like the integer 5, nobody owns the number 5, nothing borrows the number 5, it just exists as a value that anything can use (and trust to always be the same). When immutable values (whether it's the number 5 or a record, or whatever) are used with a pure function then the returned value can be essentially a direct replacement for the function invocation, making the operation essentially take zero time (i.e. there aren't intermediate mutation states, locks, or any other coordination). This is the point that Rich Hickey suggests in a number of his talks [1] - which are always excellent IMHO. [1] Are We There Yet - https://www.youtube.com/watch?v=E4RarTAZ2AY https://www.youtube.com/watch?v=E4RarTAZ2AY
- stuhood 7y ago> When immutable values (whether it's the number 5 or a record, or whatever) are used with a pure function then the returned value can be essentially a direct replacement for the function invocation, making the operation essentially take zero time (i.e. there aren't intermediate mutation states, locks, or any other coordination). The same can be said of any variable in Rust that is passed to a function via immutable reference. Borrow checking guarantees (at compile time, with no runtime overhead) that even if a user passes something that supports mutation (such as a BTree or an array) to a function, that that function may not mutate unless it has permission to. This means that if you have an immutable reference to something, it can be treated as a value. For example: a Vec or BTreeMap (structures that supports mutation) can safely and easily be used as Map keys in Rust, because borrow checking guarantees that once the Map has taken ownership of the structure, it "is a value": ie, is immutable.
- deleted 7y ago[deleted]
- JUBISCLEUDO 7y agoYuuughk
- keymone 7y agoJust learning about immutable/functional approach makes you better developer even in imperative languages. “Share nothing” (in a mutable way) is beautifully simple solution to so many concurrency problems.
- pdub1 7y agoI've tried Clojure. I prefer a programming language that allows me to pick and choose which paradigms I want to follow-- whether OOP or FP, mutable or immutable, etc. I don't need Clojure to do that for me. Personally, I am trying to figure out why a closed source language is producing such activism-- trying to increase the popularity importance of the language... despite the fact that it's a privately owned language-- not really "open source"-- everything flows through one man & his company, which come first & above, regarding the language's development. Rich Hickey: [Paraphrasing] "Open source isn't about you. I created this, it's mine, and I'll change it when and how I choose." Clojure Community: "Hey, let's try to get more people into Clojure! Let's increase this community!"
- dpkp 7y agoI can understand your frustration about Rich's development process. But clojure is most definitely not closed source. The source is right here: https://github.com/clojure/clojure https://github.com/clojure/clojure and the license that allows you to copy, modify, and redistribute that source is here: https://opensource.org/licenses/eclipse-1.0.php https://opensource.org/licenses/eclipse-1.0.php Rich has a fairly strict development approach and wants to personally review and approve all changes to the core. There are complaints about that process, and that's fair. But as far as I have seen, most large, successful projects have similar personalities leading them (Stallman, Linus, Larry Wall, Guido...). Finally, I should add -- if what you are looking for is software freedom... then you should absolutely consider using a Lisp like clojure. Lisp's give you the power to control your language through macros and non-core libraries. Unlike other languages, you do not need a core development team to make language changes for you. Perhaps this is why clojure is so powerful... because the core process issues you have heard about are not actually that important, and in fact the language itself enables substantially more software freedom than perhaps you are giving it credit for.