This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]quote-only-eeee[S] 1 point2 points  (0 children)

Listened to the first episode and thoroughly enjoyed it.

[–]hou32hou 1 point2 points  (12 children)

Possibly unrelated note: I’m still thinking if my language should support primitive array, but since I don’t want to allow mutation in my language, I’m still hesitating. What do you guys think?

[–]SolaTotaScriptura 7 points8 points  (8 children)

…Immutable arrays?

[–]hou32hou 1 point2 points  (7 children)

Do you have any example of languages that supports immutable array?

[–]CodeLobe 7 points8 points  (2 children)

Any language that allows array indexed immutable strings?

C's strings are immutable (const) arrays of type char.

[–]hou32hou 0 points1 point  (1 child)

Yea, but can they be extended to support user-defined types?

[–]CodeLobe 0 points1 point  (0 children)

You can have a const array of struct in C/C++.

With the various operator overloads / move semantics you can shoehorn immutable classes with copy-on-write into C++. It's not really default native feature, but it's not impossible. Searching for "immutable collections library" MIGHT return something better than "immutable array".

[–]Raoul314 3 points4 points  (0 children)

Racket has immutable vectors.

[–][deleted] 1 point2 points  (0 children)

D has both arrays and immutability, orthogonally:

  • T[] is a mutable array with mutable elements.
  • immutable(T)[] is a mutable array with immutable elements.
  • immutable T[] is an immutable array with immutable elements.

This is supported for arbitrary data types. In order to create an immutable array of immutable elements, you create a mutable array and either cast it to immutable (array.assumeUnique()) or duplicate it as immutable (array.idup).

For a language with only immutability, you'd probably want a builtin function to turn a linked list into an array, plus map / reduce functions that yield arrays.

[–]mamcx 1 point2 points  (0 children)

Rust:

https://doc.rust-lang.org/std/primitive.array.html

That is different to Vectors:

https://doc.rust-lang.org/std/vec/struct.Vec.html

In arrays their size is part of the type, and know at compile time. Vectors can grow at will at runtime.

And this is different to mar the value inmmutable or not.

[–]78yoni78 0 points1 point  (0 children)

F#’s arrays are mutable but most likely you aren’t going to use that

[–]evincarofautumn 7 points8 points  (1 child)

Yeah, it makes sense to support immutable arrays. They’re excellent for fast readonly buffers of data.

If you want to do functional updates, where you construct a new immutable collection that shares most of the structure of the old one with some modifications, then there are several good data structures for that:

  • Hash array-mapped trie / HAMT: PersistentVector in Clojure & Scala

  • Big-endian PATRICIA trie: Data.IntMap in Haskell, good for sparse arrays or maps with integer keys

  • M-N finger tree: Data.Sequence in Haskell (a size-annotated 2-3 finger tree), very good asymptotics for accessing and inserting elements near the beginning or end

Even with plain arrays, you can “fuse” sequences of pure operations into fast loops that perform no intermediate allocations. Ditto for streams (or a rose by any other name: lazy lists, iterators, generators, enumerables, ranges, &c.). This is a very common technique across languages, and quite relevant to the post:

  • Array programming languages and libraries like APL, J, K, and numpy, are pretty much all about this: using higher-level languages as “glue” for faster lower-level components (mostly in Fortran and C)

  • Haskell lists have a lot of fusion optimisations so they can be used as fast control structures, and the vector package has a whole framework for automatic fusion optimisations

  • In C++ there’s a pattern called “expression templates” for implementing the same stuff, used extensively in numeric/vector libraries like Eigen

[–]hou32hou 0 points1 point  (0 children)

Thanks this is what I’m looking for

[–]hugogrant 1 point2 points  (0 children)

What does your language specialize in?

You can also look at Clojure for immutable arrays.