Skip to content

Instantly share code, notes, and snippets.

View fogus's full-sized avatar
💭
attempting to learn how to better learn

Fogus fogus

💭
attempting to learn how to better learn
View GitHub Profile
@fogus
fogus / gvec.clj
Created August 27, 2010 01:43 — forked from richhickey/gvec.clj
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
(set! *warn-on-reflection* true)
(defn foo [s] (prn s))
(provide-contracts
[foo "Only allow foo" [s] [#"foo"]])
(foo "foo")
; "foo"
(foo "bar")
; java.lang.AssertionError: Assert failed: (clojure.core/re-matches #"foo" s)
(defn cleave
[& args]
((apply juxt (butlast args))
(last args)))
(defn cleave2
[x & fns]
((apply juxt fns) x))
(defn symbol
"Returns a Symbol with the given namespace and name."
{:tag clojure.lang.Symbol
:added "1.0"
:changed "1.3"
:static true}
([sym] (if (symbol? sym) name (clojure.lang.Symbol/intern sym)))
([ns sym] (clojure.lang.Symbol/intern (name ns) (name sym))))
(doseq [[fn-name fn-doc] '[[testname "sample doc"] [anothername "detailed documentation"]]]
(eval `(defn ~fn-name ~fn-doc [a#] a#)))
(testname :foo)
;=> :foo
(defmacro gen-em [body]
`(do
~@(for [[fn-name# fn-doc#] body]
(list 'defn fn-name# fn-doc# ['a] 'a))))
(gen-em [[foo "sample doc"] [bar "detailed documentation"]])
(foo :foo)
;=> :foo
(defn sqr [n]
"Squares a number"
(* n n))
(sqr 5)
;=> 25
(alter-var-root
(var sqr) ; var to alter
(fn [f] ; fn to apply to the var's value
(defn foo [] 42)
(foo)
(defn mk-foo []
foo)
(defn mk-bar []
(mk-foo))
(defmulti foo type)
(defmethod foo String [_] (println "a string"))
(defmethod foo ::cereal [_] (println "some cereal"))
(foo "blah")
; a string
(foo {:a 1})
; java.lang.IllegalArgumentException: No method in multimethod 'foo' for dispatch value: class clojure.lang.PersistentArrayMap