exceptional icon indicating copy to clipboard operation
exceptional copied to clipboard

Idea: `pipe_safe/2` for cleaner piping.

Open zachdaniel opened this issue 7 years ago • 0 comments

For this I'll use a similar example to the one you used for Make Safe in the readme.

def max_of_row(nested_array, x, y) do
  toothless_fetch = safe(&Enum.fetch!/2)
  toothless_max = safe(&Enum.max/1)
  
  array
  ~> toothless_fetch(index)
  ~> toothless_max()
end

I think it would be great for readability if I could just say:

def max_of_row(nested_array, x) do
  array
  ~> pipe_safe(&Enum.fetch!(&1, x))
  ~> pipe_safe(&Enum.max/1)
end

If safe/2 didn't exist, then we would have been able to adapt the existing function to serve that purpose, but we would have to introduce a new name (or a new operator) to do it from where it is now.

As an operator (which I'm not necessarily advocating for, just including it for completeness' sake), that might look like

def max_of_row(nested_array, x) do
  array
  ~~> Enum.fetch!(x)
  ~~> Enum.max()
end

zachdaniel avatar Nov 10 '18 04:11 zachdaniel