7 ms·
Immutable.rs: immutable data structures for Rust
- muizelaar 9y agohttp://smallcultfollowing.com/babysteps/blog/2018/02/01/in-rust-ordinary-vectors-are-values/ http://smallcultfollowing.com/babysteps/blog/2018/02/01/in-r... is an interesting look at how Rust's ownership system lets you treat the standard collections as values.
- slaydemons 9y agoGood read, if you are the author I am not sure if feedly is lying that you have rss but despite saying you do I can not subscribe.
- steveklabnik 9y agoThe author doesn’t post to HN that I’m aware of, I’ll ping him. Thanks!
- eridius 9y agoBTW, while it's true the feed URL listed in the <head> tag of that site is incorrect, you can in fact subscribe using http://smallcultfollowing.com/babysteps/atom.xml http://smallcultfollowing.com/babysteps/atom.xml
- bjoli 9y agoI you are into immutable and c++ you should really have a look at Immer by Arximboldi (who is a user here on HN). Really amazing stuff: https://github.com/arximboldi/immer https://github.com/arximboldi/immer
- m0meni 9y agoThe other immer[0] of note is a JS library that gives you immutability with a mutable API i.e. you mutate your code as normal, but all changes are proxied to a copy, which is then returned as a result of your mutations. [0]: https://github.com/mweststrate/immer https://github.com/mweststrate/immer
- cryptonector 9y agoSounds a lot like jq's jv API: https://github.com/stedolan/jq/wiki/C-API:-jv https://github.com/stedolan/jq/wiki/C-API:-jv
- widdershins 9y agoYeah I've been using this to make a interpreted Clojure-style lisp with immutable data structures. Great library.
- arximboldi 9y agoThanks for mentioning Immer! I am studying Immutable-RS right now and it seems interesting. The code is much more straight-forward than Immer's thanks to Rust expresivity when it comes to sum types, etc. Sadly, on the other hand, some of the tricks that Immer implemented, in particular the ones that flattens leaf nodes, can not be implemented in safe Rust, and I my guess is that Immer is at the moment more efficient both in run time and space usage. I would love to see some benchmarks comparing both though!
- winstonewert 9y agoWhat kind of uses cases do people have for this? Since rust already pretty tightly controls mutability, my normal reasons for using immutable data structures don't apply there.
- vvanders 9y agoThat was one of the interesting side-effects of digging into Rust that I wasn't expecting. I find that I get the same level of "no mutation surprises" I commonly see in Elm/functional code but with the benefit of breaking out mutation when I need performance in a way that I don't normally shoot myself in the foot with. Having written C++ for quite a while it's really a refreshing feeling. Heck, I wish I had similar constrains available to me in Java for parts of an architecture that I know is going to be gnarly.
- Groxx 9y ago>Why Immutable Data Structures >... Rust, being what it is, does a good job of discouraging this kind of behaviour, and keeping it strictly controlled when it's necessary, but the standard library doesn't provide collection data structures which are optimised for immutable operations. This means, for instance, that if you want to add an item to a Vec without modifying it in place, you first need to clone the whole thing before making your change. >Data structures exist which are designed to be able to make these copies much cheaper, usually by sharing structure between them, which, because this structure is also immutable, is both cheap and safe. ... Seems pretty straightforward. You'll still have some benefits to immutable-data coding-patterns because it requires only read access on a value, not write, all the way up to the moment you want to swap values (if ever). Making that faster is a worthwhile goal.
- taeric 9y agoSeems misguided, though. Specifically, this has the implicit claim that the shared structures will be faster than the alternative. The whole "is both cheap and safe." However, Rust uses other mechanisms to provide the safety, such that it may be "cheaper," but only compared to copying the full data structure. It will not be cheaper than just modifying in place. The question, then, is how often is that strategy truly idiomatic and a net win in Rust?
- the_mitsuhiko 9y agoThe ergonomics of using immutable structures with Rust are really good. You wouldn't know you have an immutable structure in front of you (other than that maybe there are more Arcs than you would expect).
- cryptonector 9y agoThis could really use some examples. I hope this is as easy to use as, say, the jv functions in libjq [0]. [0] https://github.com/stedolan/jq/blob/master/src/jv.h https://github.com/stedolan/jq/blob/master/src/jv.h
- elihu 9y ago> Most of these data structures support in-place copy-on-write mutation, which means that if you're the sole user of a data structure, you can update it in place with a huge performance benefit (about an order of magnitude faster than immutable operations, almost as fast as std::collections's mutable data structures). > > Thanks to Arc's reference counting, we are able to determine whether a node in a data structure is being shared with other data structures, or whether it's safe to mutate it in place. When it's shared, we'll automatically make a copy of the node before modifying it, thus preserving the usual guarantees you get from using an immutable data structure. That's a really interesting approach. I could see that being really useful in a lot of cases, and it lets you get good single-threaded performance on code that would also continue to work in a multi-threaded case.
- masklinn 9y agoFWIW Clojure already has that concept as "transients" but it requires explicit invocation (creation of the transient then conversion back to persistent). I think Swift also commonly uses CoW but does so for regular collections not persistent ones.
- millstone 9y agoWhat is the advantage of persistent collections over value-type collections?
- karavelov 9y agoStructural sharing. It means you don't have to copy the whole data structure but just the path to the root.
- millstone 9y agoI don't know - that sounds more like an implementation detail available to both. Obviously if you modify the first element of a value-type list, it's not going to copy the rest. Persistent data structures are really about the interface: functions that would be traditionally mutating instead return new versions. Is there an advantage of that interface over mutable value-type collections? One advantage is that it works better with e.g. folds. But overall I think it's more awkward.
- noahdesu 9y agoI'm trying to figure out what the basis is for the implementation of the sorted map. For example, in related project https://github.com/orium/rpds https://github.com/orium/rpds they explicitly note that they are basing their sorted map on Okasaki red-black tree.
- tom_mellior 9y agoordmap.rs notes that it's based on B-trees. Doesn't that help?
- nurettin 9y agoThis looks more like a view library where you have an iterable and views that let you access them in convenient ways and copy as little as possible than a data structure library where data is created on the fly within the data structure as needed.
- floatboth 9y agoYay, it's no longer LGPL'd: https://github.com/bodil/im-rs/commit/32f58e914c84a6e965eea8e732a81cc3cd160fbb https://github.com/bodil/im-rs/commit/32f58e914c84a6e965eea8... :)