Skip to content

Instantly share code, notes, and snippets.

@killme2008
Created October 23, 2015 04:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killme2008/d8345301536980d1b64a to your computer and use it in GitHub Desktop.
Save killme2008/d8345301536980d1b64a to your computer and use it in GitHub Desktop.
Elixir custom sigis example
defmodule MySigils do
def sigil_i(string, []), do: String.to_integer(string)
def sigil_i(string, [?n]), do: -String.to_integer(string)
end
@killme2008
Copy link
Author

(defn i [s]
  (let [s (str s)
        n? (.endsWith s "n")
        s (if n?
            (.substring s 0 (-> s (count) (dec)))
            s)
        x (Integer/valueOf s)]
    (if n?
      (- x)
      x)))

;;add reader
(alter-var-root #'default-data-readers assoc 'i i)

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