Created
May 7, 2014 09:07
-
-
Save d0rc/7cbf3c5956c2118bd25f to your computer and use it in GitHub Desktop.
pipes pipes pipes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule PipesX do | |
| defmacro pipe_with(fun, pipes) do | |
| IO.puts "Will reduce over: #{inspect Macro.unpipe(pipes)}" | |
| Enum.reduce Macro.unpipe(pipes), &(reduce_with &1, &2, fun) | |
| end | |
| defp reduce_with( {segment, pos}, acc, outer ) do | |
| IO.puts "reduce_with: Got #{inspect segment} at #{inspect pos}" | |
| quote do | |
| inner = fn(x) -> | |
| res = unquote Macro.pipe((quote do: x), segment, pos) | |
| IO.puts "(inner) Got #{inspect x}, res: #{inspect res}" | |
| res | |
| end | |
| case unquote(acc) do | |
| {ac, pos} -> | |
| res = unquote(outer).(ac, inner) | |
| IO.puts "(outer) Got #{inspect ac}, #{inspect pos}, res: #{inspect res}" | |
| res | |
| ac -> | |
| IO.puts "Got #{inspect ac}" | |
| unquote(outer).(ac, inner) | |
| end | |
| end | |
| end | |
| end | |
| defmodule Simple do | |
| require PipesX | |
| def inc(x), do: x + 1 | |
| def double(x), do: x * 2 | |
| def with_pipes_map do | |
| PipesX.pipe_with fn(acc, f) -> Enum.map(acc, f) end, | |
| ([ 1, 2, 3] |> inc |> double) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment