Skip to content

Instantly share code, notes, and snippets.

@aamedina
Created May 12, 2014 21:30
Show Gist options
  • Select an option

  • Save aamedina/328c127f2e6e8effd602 to your computer and use it in GitHub Desktop.

Select an option

Save aamedina/328c127f2e6e8effd602 to your computer and use it in GitHub Desktop.
(ns clojure.core)
(declare cons)
(defprotocol ISeq
(first [coll])
(rest [coll])
(next [coll]))
(defprotocol Seqable
(seq [coll]))
(defprotocol IPersistentCollection
(conj [coll val]))
(defprotocol IMeta
(meta [obj])
(with-meta [obj meta]))
(defprotocol Show
(show [obj]))
(deftype Cons [_first _rest _meta]
IPersistentCollection
(conj [coll val] (Cons. val coll))
ISeq
(first [coll] _first)
(rest [coll] (or _rest ()))
(next [coll] (or _rest nil))
Seqable
(seq [coll] (cons _first _rest))
IMeta
(meta [obj] _meta)
(with-meta [obj meta] (Cons. _first _rest meta))
Show
(show [obj] (eval `(str "(" ~_first ~@_rest ")"))))
(defn cons
[x xs]
(Cons. x xs))
(deftype Symbol [_ns _name _meta]
IMeta
(meta [obj] _meta)
(with-meta [obj meta] (Symbol. _ns _name meta))
Show
(show [sym]
(if _ns
(str _ns "/" _name)
(str _name))))
(deftype Keyword [_ns _name]
Show
(show [kw]
(if _ns
(str ":" _ns "/" _name)
(str ":" _name))))
(deftype Char [ch]
Show
(show [c] (str \\ ch)))
(deftype String [chars]
Show
(show [s] (str \" chars \")))
(defn str
([] "")
([x] (show x))
([x & ys]))
(defprotocol Num
(+ [] [x] [x y] [x y & more])
(- [] [x] [x y] [x y & more])
(* [] [x] [x y] [x y & more])
(/ [] [x] [x y] [x y & more]))
(defprotocol Real
(rationalize [num]))
(defprotocol Integral
(quot [num div])
(rem [num div])
(mod [num div])
(int [x]))
(deftype BigInt []
Num
(+ [] [x] [x y] [x y & more])
(- [] [x] [x y] [x y & more])
(* [] [x] [x y] [x y & more])
(/ [] [x] [x y] [x y & more]))
(deftype Ratio [numerator denominator]
Num
(+ [] [x] [x y] [x y & more])
(- [] [x] [x y] [x y & more])
(* [] [x] [x y] [x y & more])
(/ [] [x] [x y] [x y & more]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment