Skip to content

Instantly share code, notes, and snippets.

@ryuone
Created September 25, 2013 13:35
Show Gist options
  • Save ryuone/6699681 to your computer and use it in GitHub Desktop.
Save ryuone/6699681 to your computer and use it in GitHub Desktop.
ExUnit.start
# defprotocol Comparable do
# @only [BitString]
# def be(left,right)
# def le(left,right)
# def left <= right
# end
# defimpl Comparable, for: BitString do
# def be(left,right) do
# binary_to_integer(left) >= binary_to_integer(right)
# end
# def le(left,right) do
# binary_to_integer(left) <= binary_to_integer(right)
# end
# def left <= right do
# Kernel.<= binary_to_integer(left), binary_to_integer(right)
# end
# end
defmodule Comparable do
def is_both_integer(left, right) do
(Regex.match?(%r/[0-9]*/, left) and Regex.match?(%r/^[0-9]*$/, right))
end
defmacro left == right do
quote do
result = unquote(__MODULE__).is_both_integer unquote(left), unquote(right)
:io.format "result : ~p~n", [result]
case result do
true ->
unleft = unquote(left)
unright = unquote(right)
# :io.format "unleft : ~p~n", [unleft]
# :io.format "unright : ~p~n", [unright]
unleft = binary_to_integer(unleft)
unright = binary_to_integer(unright)
:io.format "unleft : ~p~n", [unleft]
:io.format "unright : ~p~n", [unright]
# result = Kernel.==(binary_to_integer(unquote(left)), binary_to_integer(unquote(right)))
# :io.format "result : ~p~n", [result]
# result
true
false ->
# Kernel.==(unquote(left), unquote(right))
true
end
end
end
defmacro __using__(_opts) do
quote do
import Kernel, except: [==: 2]
import unquote(__MODULE__), only: [==: 2]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment