An often confusing topic to new Elixir programmers is comprehensions. In this blog post, we will start from the most basic ideas and examples, and build on that to illustrate some cool patterns of advanced comprehensions widely useful across all sorts of applications.
I was going to open a new thread after reading this article from the ElixirRadar newsletter. Thank you! It was a great read.
In my programs I usually tend to use
reduce functions over comprehensions. I wanted to ask about general examples of when you people tend to use comprehensions over those functions.
Is there an idiomatic preference over when to use one over the other?
The main reason to use a comprehension, is its increased flexibility.
When this flexibility is not necessary, using map/filter/reduce etc… is more idiomatic.
One thing that for-comprehensions can not do, is be pipelined, so in pipelines, the other functions are more frequently used. If you want to perform a slightly more complex manipulation of your data, though, it is good form to extract it to its own (private) function, in which case you can use whatever you want in its internals, of course.
Three things that only for-comprehensions provide are:
- Iterating over multiple collections at once. (all combinations of elements are then iterated over)
- In one go store the result of the comprehension in another thing implementing the Collectable protocol. (The Enum functions are all built on top of
reduceand therefore always build an intermediate list representation.)
- Have boolean properties to exclude certain fields from the thing you are constructing. With the Enum functions, you usually need to use multiple steps after one-another (e.g. fist filter, then map) to do this.
Apparently, MS smelled the coffee and put out P-lang for IoT:
Looking at the syntax, reminds me of Erlang/Elixir…
The IoT fight is on with Gosling lured to AWS: https://siliconangle.com/blog/2017/05/23/james-gosling-father-java-joins-amazon-web-services/
New post on the ElixirLuthier:
“While lots of attention is being given to web-based project in Elixir/Phoenix, I thought it might be fun to show how Elixir can be used for IoT projects and low-level hardware control. In this post, I will demonstrate how easy it is to stand up a GenServer that uses a device connected to the i2c bus that senses temperature and humidity (using the SHT31-D device).”
One of the biggest selling points of Elixir is the means it gives you to write fault tolerant applications via its concurrency model. Processes can broadcast their failure to dependant processes which can take appropriate action.
You decide how processes should respond to failure based on your use case. There is no single solution.
In our latest blog post, Elixir consultant Thomas Hutchinson gives you an example in which not handling failure led to, you guessed it, more failure.
Wrote a short blog post on using the Floki HTML parsing library to parse XML documents here. The library that the blog post is called SecLatestRssFeedParser and is also available on Hex. Would love any feedback on the post!
Great! I’ve been using Friendly for a couple of my projects. Its a XML parser that uses Floki and I find it ok, but I’d like a better way. I’ve used Quinn in the past, but I’ve found it pretty lacking
I’m gonna read it later and come back to you
Very useful. BTW, any Topic Maps people using Elixir here?
I’ve been also using Quinn, until today I found that it crashes when the XML contains comments. Just tried Friendly and it works like a charm for my use case (sitemaps parsing). Thanks!
About a month ago I was in-between jobs - I had two weeks to rest up, recharge and get ready for my new job. So I thought, I should use those two weeks to learn something new…
I just wrote a blog post on my past month of learning Elixir and some Machine Learning.
Great article, a fun read, would like more code examples, like perhaps a post on how you designed your toy robot example and why you chose to do certain things the way you did; it is great to see how someone makes decisions about something.
Wow, this thread has gotten long. Anyway, I was listening to one of my podcasts today (new one by the former hosts of giant robots), and one of them mentioned tying Elixir, running into something when saving multiple records with Ecto, and maybe writing up his findings. Turns out he did:
I’ve been using comprehensions in Python a lot and it is my favorite method for many common tasks. I am happy that these are implemented in Elixir too, the implementation is maybe even better featured than Python’s.
I’ve written a new blog post showcasing an application that is vulnerable to DoS by exhausting the BEAM’s atom limit:
The first link under “Data Structures” doesn’t work anymore.