--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/elixir-lang-core/34bd1126-03aa-4859-a23b-5c69bf351d3dn%40googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/elixir-lang-core/CAFXvW-79V6YwDNzKjgctvY5xwoFbF7bbs7Md1TJO7nQK6Ux0ww%40mail.gmail.com.
To view this discussion visit https://groups.google.com/d/msgid/elixir-lang-core/0CADB481-F911-4F5B-8761-C07577704840%40binarynoggin.com.
Most of alternatives described here have been addressed in the Erlang EEP for strict comprehensions.
I think of ‘for’ when I want to filter and combine permutations. If I want to match I generally use Enum.map and when the fun doesn’t match you get an error.
Replacing the module with an Enum (or any stdlib) call would work for most datatypes, but not for bitstrings.
Also, not a very satisfying solution, but strict filtering can be achieved today using this workaround:
for tuple <- tuples, {x, y} = tuple, do: …
In the Erlang EEP, the main rationale for rejecting destructuring in filter was that it was quite awkward looking: . This doesn’t quite apply in Elixir.
This is also quite similar to destructuring in the comprehension body. In the
Erlang EEP, the main reason for rejecting this was that you can’t re-use the
variable name you use to match on each item has a whole (here tuple, and in
the EEP, Usr) in following comprehensions. This obviously doesn’t apply in the
context of Elixir as well.
However, the EEP also notes that the intent of strict generation is not conveyed by destructuring later. If I received something like this in a PR, my first thought would be that it was bad style, and request for destructuring within the match until I thought it through.
In Erlang, with https://www.erlang.org/eeps/eep-0070 we have strict comprehensions now with the syntax <:-.. I don’t know if that syntax would be appropriate for Elixir, but if not I am sure we could think of some other syntax for it.
Like Amos mentioned, <:- can be difficult to distinguish from <-. Generally,
adding new syntax is a last resort since that adds complexity (not only for the
compiler, but more importantly for users). Erlang had to introduce new syntax
since its comprehension didn’t support being passed options. This is why I like
Bruce’s suggestion:
Maybe an option like strict: true, or mode: strict | match, or mode: match | **destrcuture would work?
Of these, I like strict: true the best, since we will not have more than 2
“modes”.
An option would change the entire comprehension and not a single match which I think would be less than ideal.
Isn’t changing the behaviour of the iteration the goal? I see don’t how passing options in any different from switching to Enum?
– bp
To view this discussion visit https://groups.google.com/d/msgid/elixir-lang-core/CAMEXYWciYOpizzk%3DQqZ7wNu0HZdMwPhBtS76m81BHsFsxJt4tA%40mail.gmail.com.