Clojure vs. D in creating immutable lists that are almost the same.
w0rp via Digitalmars-d
digitalmars-d at puremagic.com
Sat Feb 27 15:19:51 PST 2016
On Saturday, 27 February 2016 at 22:31:28 UTC, Brother Bill wrote:
> Clojure supports immutable lists that allow adding and removing
> elements, and yet still have excellent performance.
>
> For D language, what are the recommended techniques to use
> functional programming, without massive copying of data and
> garbage collection, so that it remains immutable.
>
> That is, how to create one-off changes to an immutable data
> structure, while keeping the original immutable, as well as the
> one-off change, and maintain good performance.
>
> Thank you
I think this is a property of linked lists which could possibly
be advantageous. However, I would keep in mind that memory layout
is very important when it comes to execution speed, and that
slices of memory are unbeatable in that regard. That's worth
stating first.
I think for linked lists, you can always create a new node which
points to another node. So you start with element a as immutable,
then you take a head element b and point to a, so you get b : a,
then c : b : a, etc. So you can create larger and large immutable
linked lists because you never actually change a list, you just
produce a new list with an element pointing the head of a
previous list.
I'm not sure if Phobos has something suitable for this, but you
could always implement your own singly linked list in such a
manner pretty easily. I would be tempted just to use slices
instead, though. Linked lists are rarely better.
More information about the Digitalmars-d
mailing list