5 ms·
Concurrency aside, why is Elixir preferred over Ruby when it doesn't even have a native array implementation? No, lists and tuples are no substitute nor are map
by cutler 10y ago
Concurrency aside, why is Elixir preferred over Ruby when it doesn't even have a native array implementation? No, lists and tuples are no substitute nor are maps with numeric keys as Jose has suggested. If you want an array in Elixir your only option is Erlang's implementation which ain't pretty - http://erlang.org/doc/man/array.html http://erlang.org/doc/man/array.html. When I raised this issue in the mailing list and on IRC the response was invariably a definsive "I've never needed arrays", "We use tuples for most things in Elixir" or "Array performance characteristics are difficult to optimise in a functional language such as Elixir". I just find this disappointing.
- cutler 10y agoSorry if seems a bit off-topic but I raised this issue because there have been a lot of posts about Rails devs switching to Elixir/Phoenix.
- jswny 10y agoI've just started learning Elixir out of interest so I can't quite answer your question. However, I'd like to ask: what have you found to be difficult to do using the Elixir types such as tuples, list, keyword lists, and maps that would otherwise be easy to do using arrays? I'm asking out of genuine curiosity because I've specifically noticed a lack of arrays when learning Elixir and I'm quite used to using them in other languages.
- cutler 10y agoElixir has no native array syntax nor any array functions in the stlib so arrays are basically second class citizens rather than completely absent. Ask not why I need them but why anyone might need them if the Elixir is supposed to be a general purpose language. Why does Clojure need them or Ruby or Python (lists)? If I want to process data as a large indexed array Elixir isn't going to help me and some Elixir devs have conceded that it may not be suitable for large scale data processing. We all know Elixir/Erlang excels at distributed concurrency but the question here is whether it is deficient in other areas, making it too much of a niche language.
- rdtsc 10y ago> Why does Clojure need them or Ruby or Python (lists)? So you want a list it seems > l=[1,2,3] > [h | t] = l [1, 2, 3] > h 1 > t [2, 3] > If I want to process data as a large indexed array Elixir isn't going to help me But neither will Python. You'd want a large indexed array you probably want numpy. See now you are revealing a bit more about your usecase that's helpful. So you have large amount of data and want to query it and process it. In that case you might want to check out ETS. The nice benefit, you have have cool matching or query list comprehensions of it. Can also access it concurrently from multiple processes: http://elixir-lang.org/getting-started/mix-otp/ets.html http://elixir-lang.org/getting-started/mix-otp/ets.html This is the Erlang side of it: http://learnyousomeerlang.com/ets http://learnyousomeerlang.com/ets > but the question here is whether it is deficient in other areas, making it too much of a niche language. Of course it is deficient. C is deficient. Java, C++ is they all. Every language is deficient in some way. I haven't found a Perfect one yet. Still looking... Maybe Rust, who knows, still struggling to learn it.
- desdiv 10y ago>But neither will Python. You'd want a large indexed array you probably want numpy. Python comes with array.array¹ natively. ¹ https://docs.python.org/3/library/array.html#array.array https://docs.python.org/3/library/array.html#array.array
- rdtsc 10y agoOriginal poster, specifically mentioned lists in Python, i.e. implying they wanted something equivalent to that. (Why does Clojure need them or Ruby or Python (lists)).
- bratsche 10y ago> Elixir has no native array syntax nor any array functions in the stlib There is no native array syntax, but there are array functions in the stdlib: http://erlang.org/doc/man/array.html http://erlang.org/doc/man/array.html
- tedmiston 10y agoPython lacks a native array too but its list type seems to do just fine. What is it you want to do that you that requires an array?
- cutler 10y agoPython's lists are lists in name only when compared with lists in functional languages. Whilst Python may offer additional array/vector implementations its lists are equivalent to arrays or vectors in other languages such as Ruby.
- aprdm 10y agoincorrect. python has array.array which behaves just like in C. You even give the type of the data.
- rdtsc 10y agoWhy do you want an array? You mean you want a memory area with adjacent cells layed out, to talk to C perhaps? Perhaps might want a list, a binary, or a sorted set, a tuple, a map and so on. You have to tell a bit more about your use case. I used Erlang for many years, and in the last one full time. Not one time have I needed an "array". It seems you picked one odd feature no-one uses and are upset about it, I thik it is just a small misunderstand, if you share a bit more about what you are trying to do, you'll get more help. > I just find this disappointing Did you explain your use case to them. If you just said "I need an array" I see their response as perfectly rational. And from what I've seen, that is probably the friendliest and most approachable community, especially for newcomers.
- cutler 10y agoI would need an array for processing any large data set as an indexed collection. Why is that considered an "odd feature no-one uses" when it's a fundamental data type in most mainstream languages?
- KallDrexx 10y agoHow are you planning on accessing it though? If it's iterated over sequentially then a list works perfectly. If it's random access then a map works.
- cutler 10y agoWhat if I want to push/pop it and also access it randomly by index? PHP gets a lot of stick for making arrays serve double purpose but the Elixir advocates of using maps for array purposes are basically arguing for the same thing, no?
- nine_k 10y agoDoes absence of arrays look a problem for you, while general immutability of most everything and message-based approach look totally normal and easy to grasp? I suspect that to use Elixir, you need to study a new way of doing things first. Then you'll see arrays in a different light.
- cutler 10y agoI have studied Elixir and other functional languages such as Clojure where vectors are 1 of the 3 basic collection types so the "new way of doing things" isn't an issue for me. Elixir took a lot of inspiration from Clojure so the 2 languages are not that different.
- nine_k 10y agoExpectedly, per Clojure documentation, "Vectors support access to items by index in log32N hops". This means that a Clojure vector is essentially a tree-based map; a "true" array (as in C or Java) would have O(1) time for access by index. I suspect that Elixir's maps are not different in this regard, as are Erlang's arrays, or maps in Haskell or OCaml. This is the canonical way to make an immutable map (see picture here: http://programmers.stackexchange.com/questions/195718/functional-programming-right-ideas-about-concurrency-and-state/195730#195730 http://programmers.stackexchange.com/questions/195718/functi...).
- GrinningFool 10y agoThis isn't a defensive response, just a genuinely curious one. For certain types of programming - generally in lower-level languages - arrays can be essential for performance. But then my assumption is that if you needed that kind of performance, you'd be using those languages. So outside of performance, what's the functional difference between an actual array implementation - and a thing that looks, walks, and quacks like a duck?
- cutler 10y agoRuby, Python and Javascript all have native array support but can hardly be considered lower-level languages.
- asonge 10y agoJust to be clear, Javascript's arrays will, depending on the implementation, dynamically switch between sparse and compact implementations. A sparse javascript array literally behaves like a javascript object or a map with an integer index. Ruby and Python's arrays will grow as long as the allocator can alloc more memory onto the end, but has to copy the entire array if there's memory fragmentation...it's not quite the same as a C array. Because of the immutable data, your best hopes are actually vectors or maps. Btw, the erlang library you mention implements arrays on top of a tree of 10-ary(-ish) tuples. It'd need to be measured, but I'd be willing to bet that the native maps are faster at access, insert, and delete. The maps are implemented in C, and worst case for all operations would be O(n log n) or similar.
- querulous 10y agoif you need constant time access by index to a sequence of fixed size elements you can use binaries. if you want constant time access to a sequence of fixed size you can use tuples. if you want both you can get close with maps using integer keys or with trees (which i believe are used by clojure behind the scenes for arrays)
- cutler 10y agoIf it comes to that I can just use Erlang's implementation but my question is why the omission in Elixir when arrays are supported in Erlang? With Elixir's excellent macro support surely an Elixir wrapper around Erlang's arrays wouldn't be so difficult?
- acangiano 10y agoElixir wrappers that don't significantly enhance what Erlang is already exposing are discouraged within the Elixir community. As I type this, I realize that this might enrage you even more than the lack of arrays ever could. :)
- tbrooks 10y agoIn all your replies, you haven't given concrete example of "I need arrays because tuples or lists can't do X". Complaining in the abstract isn't helpful. If Elixir had arrays, why would that be better than using other enumerables like lists or tuples?
- cutler 10y agoIt would be better because lists are not accessible by index and tuples are only designed for small amounts of data.
- frigo_1337 10y ago...but why do you want to access it by index? Since you refuse to be more specific, it sounds like you're just not willing to learn the language and the way problems are solved with it. Expecting mutable C-type arrays in elixir/erlang is like expecting classes and lambdas in an assembly language. It's just not a good fit for what the language was designed to do.
- alehander42 10y agonot the OP, but I've had a side project in Elixir and often I wanted to browse/play with some of the last saved models in repl. When I get the collection using `ecto`'s `Repo.all..` I can't just `bets[4]` or `bets[-2]`, I have to do weird gymnastics like `hd(tl(bets |> Enum.reverse))`
- smss79 10y agohttp://elixir-lang.org/docs/stable/elixir/Enum.html#at/3 http://elixir-lang.org/docs/stable/elixir/Enum.html#at/3
- alehander42 10y agothank you!