Proposal: Addition of `either` and `both` operators

42 views
Skip to first unread message

Daniel Angell

unread,
Mar 2, 2018, 12:55:06 AM3/2/18
to elixir-lang-core
As an alternative to:

if foo(my_baz) and bar(my_baz) do
  # ...
end

it would be great to have `either` and `both` operators as sugar

if my_baz both foo and bar do
  # ...
end

if my_baz either foo or bar do
  # ...
end

These operators could be equivalent to the following function definitions

def both(_, true, true),      do: true
def both(_, false, _),        do: false
def both(_, _, false),        do: false
def both(value, true, right), do: right.(value)
def both(value, left, true),  do: left.(value)
def both(value, left, right) do
  left.(value) and right.(value)
end

def either(_, true, _),          do: true
def either(_, _, true),          do: true
def either(_, false, false),     do: false
def either(value, left, false),  do: left.(value)
def either(value, false, right), do: right.(value)
def either(value, left, right) do
  left.(value) or right.(value)
end

However, the support for boolean operands might not end up being the most readable.

Louis Pilfold

unread,
Mar 2, 2018, 4:46:10 AM3/2/18
to elixir-lang-core

Hi

What would this add to the language that we don't already have? It seems to overlap with &&, ||, and, or, Enum.any, and Enum.all. I would rather we have fewer ways in the standard library to write the same thing, rather than more, so new people coming to this language have less special cases, syntaxes and rules to learn.

This could be implemented today as a macro quite easily. Perhaps make a hex package for this as then people can use this if they wish and we don't have to modify the standard library and wait for another Elixir released.

Cheers,
Louis


--
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 on the web visit https://groups.google.com/d/msgid/elixir-lang-core/0bae2cc5-a466-4f70-b069-878f594d45ef%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages