11 ms·
Elixir 1.3.1 released
- firasd 10y agoGood stuff. I've started using Elixir and Phoenix in a new project (I wanted concurrency for making a lot of HTTP calls) and while functional programming does take getting used to (you can not write something like a counter that increments itself in a loop) it's been relatively easy to pick up. Compared to learning Node.js, it's been a more relaxed and productive experience (although maybe I'm not making a fair comparison because I've implemented way more things in Node that I have yet to try in Elixir.)
- jdimov10 10y agoElixir counter in a loop: defmodule LoopCounter do def go, do: go(0) defp go(n) do IO.puts("n is #{n}") :timer.sleep(1000) go(n + 1) end end Or you can use processes and message passing to maintain state. More examples at: http://dantswain.herokuapp.com/blog/2015/01/06/storing-state-in-elixir-with-processes/ http://dantswain.herokuapp.com/blog/2015/01/06/storing-state...
- firasd 10y agoInteresting. I was trying to do something like: tasks = for i <- urls do Task.start(fn -> :timer.sleep(5000 * the_counter) # more stuff end) end So each task would run 5 seconds after the previous one, etc. I definitely need a deeper understanding of the whole environment and language though. Will check out the article.
- tomku 10y agoHere's a couple ways of doing that, packaged as a runnable .exs script that you can mess around with: https://gist.github.com/tomku/21eab22e98950f31a072a2ac63900b16 https://gist.github.com/tomku/21eab22e98950f31a072a2ac63900b... The key ingredient is Enum.with_index, which pairs your enumerable with the indices that you'd have in an iterative 'for' loop in another language. I've also changed from Task.start to Task.async so that I can collect the tasks and wait for them at the end of the script. Edit: The recursive approach jdimov10 explained is really useful too, you'll see that pattern of using a recursive function's arguments to hold state all over the functional programming world.
- firasd 10y agoThanks for the code sample! Will give it a try.
- unvs 10y agoNot exactly what you want, but I've found this snippet useful from time to time: for {item, counter} <- Enum.with_index([:a, :b, :c]) do IO.puts("#{counter} -> #{item}") end 0 -> a 1 -> b 2 -> c
- pdimitar 10y agoThat's actually a perfect example of looping over data structures. Thank you. (Yes, not what the parent poster asked but I still found it useful.)
- shadeless 10y agoOr if you like pipelines as much as I do, you can write it as: [:a, :b, :c] |> Enum.with_index |> Enum.each(fn({item, counter}) -> IO.puts("#{counter} -> #{item}") end)
- sanderjd 10y agoThis is interesting. I'm a huge fan of pipelines like that and I think they're unmatched for being able to compose operations well, but I prefer the non-pipelines version of what is basically just an imperative loop.
- innocentoldguy 10y agoIf all you want is a counter to loop through something a given number of times, you could do something like this: defmodule Counter do def loop(num) do Enum.each(0..num, fn (i) -> IO.puts(i) end) end end If you really want to write your own recursive function (Enum already does that for you, but it's always good to try different ways, so you can learn the language better), you could do something like this: defmodule Counter do def loop(num), do: _loop(0, num) defp _loop(idx, num) when idx == num, do: IO.puts idx defp _loop(idx, num) do IO.puts idx _loop(idx + 1, num) end end The thing that is nice about Elixir is that it does tail-optimization on its recursive functions, as long as the recursive call is the very last thing in the function. This means you won't have 10 copies of the _loop function in memory waiting to unwind.
- jswny 10y agoSeems like this is mostly a bug fixing release. Solid stuff nonetheless. I recently learned Elixir and even though I came from an object oriented and imperative language background, I've fallen in love with the language. Switching back to others like JavaScript really leave me missing so many of the functional features, especially pattern matching. I think I've been spoiled.
- deleted 10y ago[deleted]
- rpazyaquian 10y agoSo, I know that Elixir is powerful and good at functional programming. The one thing I can't quite understand is why we would want to program everything as a composition of individual programs and applications, with supervisors and application trees and message passing and the like. It seems like a lot of overhead to accomplish something. Making everything asynchronous and detached makes things more complicated, not less. I hear a lot about OTP and the "let it crash" mantra, but I just don't quite understand what's so great about it. Maybe it's just due to my problem domain (web development), but it doesn't seem like as big a draw to Elixir and Erlang as pattern matching and FP are.
- tim333 10y agoThe main argument why seems to be for reliability so it can run for months without going down.
- rpazyaquian 10y agoI understand the need for reliability, i.e. "come back up if you get bad data so it doesn't just die on you", but this seems like something that dates back from when any kind of raised exception would bring the entire system down. Don't we have modern frameworks and systems now that can tolerate individual failures instead of completely dying just because someone tried to POST some bad data? Plus, having reliability and coming back up if it goes down doesn't solve the root problem, which is that there's a bug in the code, or some sort of unhandled edge case. 99.9999999% uptime doesn't help if your code doesn't work right.
- dottedmag 10y agoImagine there is a bug in the code or unhandled edge case, only exhibited on 0.01% transactions. Elixir/OTP handler dies on this edge case, supervisor spawns another worker, 99.99% of user transactions continue to be served. Programmer wakes up next morning, comes to work, reads the exception log, fixes this edge case, optionally hot-upgrades the code in production if it's easier than doing a full restart, problem solved. Other frameworks have to carefully implement the same semantics (that is, prevention of propagation of bad state/cascading failures). Elixir/OTP/Erlang come with it by default, and coding style encourages it.
- dingleberry 10y agohaving to put 'end' leaves bad taste, which is why i stopped learning ruby. i learned lisp instead and now i'm learning lfe (lisp flavored erlang) do i miss something by not learning elixir?
- SEMW 10y ago> do i miss something by not learning elixir? Verses LFE? Not really, at the end of the day they're both ways of making programs for the Erlang VM that aim to improve on Erlang. You can use any of erlang, elixir, and LFE libraries from any of the three[0]. See Jose's comment at https://groups.google.com/d/msg/lisp-flavoured-erlang/ensAkzcEDQQ/I7e1MJgjulQJ https://groups.google.com/d/msg/lisp-flavoured-erlang/ensAkz... . [0] though beware string types -- LFE string functions generally take charlists, same as Erlang; Elixir ones take utf-8 binaries. So you lose a bit of unicode-just-worksness compared to Elixir, but it isn't really a problem, you just need to be aware of the mismatch and be prepared to handle it
- tim333 10y agoElixir seems to have more in the way of books and videos if you like that.
- anonymousguy 10y agoWe have been looking for an Elixir code beautifier. If you are aware of one please let us know: https://github.com/Glavin001/atom-beautify/issues/545 https://github.com/Glavin001/atom-beautify/issues/545
- innocentoldguy 10y agoDo you mean something like Python's PEP8 plugin, or JSHint?
- sugarpile 10y agoNot sure if it's what you want but Credo (https://github.com/rrrene/credo/ https://github.com/rrrene/credo/) exists
- nilkn 10y agoCan someone explain why I should use Elixir instead of Erlang? I haven't used either, but from a very high level it seems that Elixir has a "magic" syntax like Ruby, where as much as possible is hidden from the user, whereas Erlang has a much clearer and more concrete syntax. Even though Erlang's syntax is not standard or traditional, as someone who's used neither neither language, I find it much easier to read and understand exactly what's happening.
- bratsche 10y agoI don't think anything is hidden, it's just a different syntax. The only thing that's really changed is that in some of the Elixir modules they've changed the order of parameters to make them consistent. I use Elixir and I like it, but I wouldn't tell you to use Elixir over Erlang. There's no competition, what's good for one language is good for the other. So use whatever makes more sense to you and hopefully all of us in this Erlang community win.
- mijoharas 10y agoI've only dabbled with both, but the reasons often given is that (apart from surface syntax) the advantages of Elixir over erlang is a powerful macro system, a more cohesive standard library (with some nice things like the pipeline operator), and some very nice tooling (mix is great). With all that being said, erlang is a great language as well, so just pick whatever you like the most. Here is a short article that outlines some advantages. http://theerlangelist.com/article/why_elixir http://theerlangelist.com/article/why_elixir
- learc83 10y agoRuby doesn't really have "magic" syntax anymore than any other dynamic language does. Too much "magic" is a frequent criticism of the Rails framework, but I've never heard that directed at Ruby.
- illicium 10y agoPhoenix is filled with macro magic, and just like Rails is the overwhelming reason to use Ruby, is pretty much the main reason why people pick up Elixir.
- vfclists 10y agoWhy are redditors and ycombinators so annoying? A new version has been released and instead of discussing what is important and notable about this new release,you all go off on a tangent discussing the pros and cons of Elixir. Why don't you guys take the discussion elsewhere so that interested readers can focus on what is new and relevant about this release? The subject of the post is about a new release,not about people showing of their knowledge and opinions about computer programming and languages etc. It just makes this forums intolerable especially those which are about new releases new products etc.
- pdimitar 10y agoYou have a very adequate and concise changelog linked for this purpose. IMO people discussing here aren't "showing off their knowledge". Posting a version upgrade serves as a visibility reminder and people start inquiring about the features of the language/framework. Eventually a technology gains enough critical mass so that more people start using it. Absolutely nothing wrong about that.
- themgt 10y agoWhat makes Elixir a sort of "perfect storm" is the combination of a battle-tested, corporate funded, philosophically correct language model and VM (Erlang/BEAM/OTP) combined with a syntax (Elixir) that's both beautiful and comprehensible to an average programmer. Even if you loved Erlang, I'd argue the language is just too esoteric and jarring to most programmers to ever gain serious traction. I remember reading about Erlang's magic back in ~2007 (maybe [1]) and giving it a brief shot but deciding there was no way I wanted to look at that kind of code 8 hours a day. But coming from writing fairly FP-style Ruby/CoffeeScript/ES6-7, Elixir is feels only a step or two more down that path - in many ways actually conceptually simpler - and with enormous benefits. [1] https://pragprog.com/articles/erlang https://pragprog.com/articles/erlang
- macintux 10y agoReplace "beautiful" with "familiar" please. I find Elixir uncomfortable because I left behind that syntax and have no desire to return to it.
- innocentoldguy 10y agoI think "beautiful" is subjective. I personally like Elixir's syntax a lot. It is concise, easy to read, and familiar. It also doesn't have all those unnecessary curly braces and parenthesis, which is nice. That's what makes it beautiful to me. I also like Elm's syntax, which I use for doing front end work on my Elixir projects. Both make me happy, and that's important when I have to stare at both of them all day, every day. If something else makes you happy, then that's what you should do all day, I suppose.