This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn cleave | |
[& args] | |
((apply juxt (butlast args)) | |
(last args))) | |
(defn cleave2 | |
[x & fns] | |
((apply juxt fns) x)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(doseq [[fn-name fn-doc] '[[testname "sample doc"] [anothername "detailed documentation"]]] | |
(eval `(defn ~fn-name ~fn-doc [a#] a#))) | |
(testname :foo) | |
;=> :foo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn foo [] 42) | |
(foo) | |
(defn mk-foo [] | |
foo) | |
(defn mk-bar [] | |
(mk-foo)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |