Last active
December 4, 2020 05:41
-
-
Save henrik/00e3712d97c79325e25a to your computer and use it in GitHub Desktop.
Silly #elixir-lang macro experiment for Ruby-like method chaining. Don't use this for serious purposes :)
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 Chain do | |
defmacro chain(ex) do | |
parse(ex) | |
end | |
defp parse({:., _, [first_arg, function_name]}, more_args) do | |
args = [first_arg|more_args] |> Enum.map(&parse/1) | |
apply(String, function_name, args) | |
end | |
defp parse({ex, _, args}) do | |
parse(ex, args) | |
end | |
defp parse(anything), do: anything | |
end | |
ExUnit.start | |
defmodule ChainTest do | |
use ExUnit.Case | |
import Chain | |
test "one function" do | |
assert (chain " hello ".strip) == "hello" | |
end | |
test "multiple functions" do | |
assert (chain " hello ".strip.reverse.upcase) == "OLLEH" | |
end | |
test "argument" do | |
assert (chain "XXhelloXX".strip(?X)) == "hello" | |
end | |
test "multiple arguments" do | |
assert (chain "hello".ljust(10, ?Y)) == "helloYYYYY" | |
end | |
test "chained arguments" do | |
assert (chain "XXhelloXX".strip(?X).ljust(10, ?Y)) == "helloYYYYY" | |
end | |
test "nested arguments" do | |
assert (chain "hello".replace("xEx".downcase.strip(?x), "x".upcase)) == "hXllo" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that this is very much a toy. It doesn't handle all input sensibly. It e.g. doesn't handle arguments being variables or non-string-chain expressions like
:random.uniform
. Which is good, because it also executes the string functions at compile time, so any changing arguments would be stuck at one value.