Thanks for all the thoughtful responses.  Also, apologies for the ambiguities 
and
omissions in my original note.  As so often happens, some of the things I had in
mind didn't make it into my email.  (sigh)

In this note, I'm only considering the case of named functions that are 
explicitly
handed other named functions as arguments, via function capture.  So, for 
example,
we don't have to worry about dealing with variables which are bound to a 
function.

# Inferring arity of captured functions

When a captured function (&bar) is being used as an argument to another function
(foo), it may be possible to infer bar's arity.  In the case of library 
functions,
this information should be available from the function's typespec.  For example,
https://hexdocs.pm/elixir/Enum.html#group_by/3 tells us that key_fun and 
value_fun
both have arity 1:

  group_by(enumerable, key_fun, value_fun \\ fn x -> x end)

  group_by(t(), (element() -> any()), (element() -> any())) :: map()

So, we should be able to write something like this:

  list = ~w{ant buffalo cat dingo}

  list |> Enum.group_by(&String.length)
  # %{3 => ["ant", "cat"], 5 => ["dingo"], 7 => ["buffalo"]}

  list |> Enum.group_by(&String.length, &String.first)
  # %{3 => ["a", "c"], 5 => ["d"], 7 => ["b"]}

To clarify my motivation, I'm not trying to save the effort of typing the arity
information.  Rather, I'm trying to cut down on the amount of clutter on the 
page
and (perhaps) the effort of reading it.  I also want to get the "/1" syntax out
of the way to allow for the following notion.

# Adding arguments to captured functions

Many named functions take multiple arguments, so they can't be used in function
captures.  Allowing arguments could extend their reach and reduce the need for
special-purpose lambdas.  Here is some proposed syntax:

  list = [
    { :status, 2, "This is a minor problem." },
    { :status, 1, "This is a major problem." }
  ]

  list |> Enum.sort_by(&elem(1))

which could replace complected horrors such as:

  list |> Enum.sort_by(fn {_, x, _} -> x end)
  list |> Enum.sort_by(fn x -> elem(x, 1) end)

https://hexdocs.pm/elixir/Enum.html#sort_by/3 tells us that its mapper function
needs to have arity 1: "(element() -> mapped_element)".  Although we're using
elem/2, we're also handing it an argument, so the arity math comes out even...

-r




-- 
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-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/C9FE81F0-131D-43FA-AEC0-BD0E7A56A141%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to