Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active December 15, 2015 17:31
Show Gist options
  • Save henrik/041a6ff48f49a7b162f6 to your computer and use it in GitHub Desktop.
Save henrik/041a6ff48f49a7b162f6 to your computer and use it in GitHub Desktop.
A convenient script for Elixir golfing.
# A convenient script for Elixir golfing.
# Provide your code and some test cases. Then run this file, e.g. "elixir caddy.exs".
# Outputs your character length and test results.
# Text input, text output and return values are all handled.
#
# By Henrik Nyh (http://henrik.nyh.se) under the MIT license.
ExUnit.start
defmodule Caddy.Example do
use ExUnit.Case
import ExUnit.CaptureIO
# Put your code between the triple quotes. Newlines and initial/final whitespace will be stripped.
@code """
IO.puts String.reverse String.upcase IO.gets("");
:my_return_value
"""
# Modify these test cases. Further reading:
# - http://elixir-lang.org/docs/v1.0/ex_unit/ExUnit.CaptureIO.html
# - http://thepugautomatic.com/2015/09/testing-callbacks-in-elixir/
test "example of text input, text output, and a return value" do
input = "hello world"
output = capture_io [input: input], fn ->
{value, _binding} = Code.eval_string(clean_code)
send self, {:value, value}
end
assert_received {:value, :my_return_value}
assert output == "DLROW OLLEH\n"
end
# You probably don't need to touch this.
setup_all do
IO.puts ""
IO.puts ["Your code is a mere ", highlight(String.length(clean_code)), " chars:"]
IO.puts ""
IO.puts highlight(clean_code)
IO.puts ""
:ok
end
defp highlight(text) do
[IO.ANSI.yellow, to_string(text), IO.ANSI.reset]
end
defp clean_code do
@code |> String.strip |> String.replace("\n", "")
end
end
Your code is a mere 65 chars:

IO.puts String.reverse String.upcase IO.gets("");:my_return_value

.

Finished in 0.5 seconds (0.5s on load, 0.01s on tests)
1 test, 0 failures

Randomized with seed 456445
@henrik
Copy link
Author

henrik commented Dec 15, 2015

Renamed golf.exs to caddy.exs as suggested by @martinsvalin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment