Put simply, `<-` and `->` both semantically mean 'bind at a later time'.
In the case of `->` during the creation of an anonymous function, it
means:
'replace (i.e. bind) all the matches at the pointy-side by the things on
the shaft-side inside the function body on the pointy-side with the actual
data that will be passed in when the function is called.'
In the case of 'for', it means:
'replace (i.e. bind) all the matches at the pointy-side by the things on
the shaft-side inside the comprehension body on the pointy-side with the
actual data that will be passed in during each iteration (that matched and
passed the filters).'
In the case of 'with', it means:
'replace (i.e. bind) all the matches at the pointy-side by the things on
the shaft-side inside the succeeding with-clauses when it will be
executed.' (don't mind the pun... ;D)
See a pattern here?
The reason there both is a `->` and a `<-`, is that it is more natural for
certain kind of bindings to be written right-to-left, and others
left-to-right.
The only reason that the match operator `=` works 'differently' inside
function heads than it works inside function bodies, is that inside a
function head, there is no notion of order-of-execution, while in function
bodies there is (left-to-right, top-to-bottom). To have this clear
order-of-execution inside a function body is a design choice (that both
Erlang and Elixir follow) that makes the language a whole lot easier to
understand for newcomers. Languages that are closer to the 'bare' lambda
calculus like Haskell do not have an order of execution and consequently do
not have this added restriction on `=` inside function bodies. But
programming without an order-of-execution is something that takes some
getting used to for people that come from the imperative realm. It also
makes some things very hard to express.
So: `<-` and `->` are in essence the same thing, and `<-` does basically
the same thing, regardless of using it inside `for` and `with`.
Also, `=` does not so much behave 'differently' outside of matches, as that
it is more *restricted*. This restriction is because of Elixir's
order-of-execution. This restriction also is a good thing, because it makes
it sure that what you write is unambiguous.
Just my two cents ;-)
~Wiebe-Marten/Qqwy
On Thursday, August 25, 2016 at 12:03:42 AM UTC+2, Gilbert wrote:
>
> Hi, I am in the process of learning Elixir, and let me first say I am
> astounded at how polished the language and build tools are. A grand
> applause is well deserved to everyone who has helped Elixir get this far!
>
> I have only a minor suggestion. It seems to be easy to do, so here it goes.
>
> The `with` clause is excellent. This works, for example:
>
> msg =
> with {:ok, data} <- read_line(socket),
> {:ok, command} <- KVServer.Command.parse(data)
> do
> KVServer.Command.run(command)
> end
>
> However, I'd like to write it in this style, in effort to reduce line
> width (and, IMO, increase readability):
>
> msg =
> with \
> {:ok, data} <- read_line(socket),
> {:ok, command} <- KVServer.Command.parse(data)
> do
> KVServer.Command.run(command)
> end
>
> But, as you can see, I need to add a backslash after the `with` keyword
> for it to compile.
>
> Is there a technical reason for the backslash requirement? Or could
> perhaps Elixir be updated to support this style of formatting, without the
> backslash? :)
>
> I am using Elixir 1.3.2
>
> Thanks for reading,
> Gilbert
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/621a164c-b6b4-4137-b0c0-aa61c088df59%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.