From 1beb2d0359c7fd947a75d87132a141d212dd5992 Mon Sep 17 00:00:00 2001 From: Brooklyn Zelenka Date: Mon, 12 Sep 2016 15:52:31 +0900 Subject: [PATCH] Update README with <~> operator --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 209beb3..1c6dc3b 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,35 @@ operator `<|>` is done "the math way" (right-to-left). Versions on lists also available. +```elixir +# Regular Composition + +sum_plus_one = fn x -> x + 1 end <|> &Enum.sum/1 +sum_plus_one.([1,2,3]) +#=> 7 + +add_one = &(&1 + 1) +piped = [1,2,3] |> Enum.sum |> add_one.() +composed = [1,2,3] |> ((add_one <|> &Enum.sum/1)).() +piped == composed +#=> true + +sum_plus_one = (&Enum.sum/1) <~> fn x -> x + 1 end +sum_plus_one.([1,2,3]) +#=> 7 + +# Reverse Composition (same direction as pipe) +x200 = (&(&1 * 2)) <~> (&(&1 * 10)) <~> (&(&1 * 10)) +x200.(5) +#=> 1000 + +add_one = &(&1 + 1) +piped = [1,2,3] |> Enum.sum |> add_one.() +composed = [1,2,3] |> ((&Enum.sum/1) <~> add_one).() +piped == composed +#=> true +``` + ## Common Combinators A number of basic, general functions, including `id`, `flip`, `const`, `pred`, `succ`, `fix`, and `self_apply`.