Clojure Programming/Examples/API Examples
From Wikibooks, the open-content textbooks collection
This page gives example usage for Clojure API function calls. For a more general introduction to Clojure by example please see http://en.wikibooks.org/wiki/Clojure_Programming/By_Example
[edit] TO DO:CLASSIFY
[edit] ->
This is the "threading" operator.
(macroexpand-1 '(-> true (if "clojure")))
=>(if true "clojure")
this:
user=> (-> "abc" .toUpperCase ,, (.replace ,, "B" "-"))
"A-C"
is equivalent to:
user=> (.replace (.toUpperCase "abc") "B" "-")
"A-C"
NOTE: you do not need to insert ',,', since clojure treats them like whitespace, but marking the forms second position this way enhances readability.
However -> works on everything
user=> (-> true (if ,, inc dec) (map ,, [1 2 3]))
(2 3 4)
or expanded
(map (if true inc dec) [1 2 3])
So one can also use macros and normal functions in ->, ie. non-methods.
Here is an example that should make every devoted Programmer fall in love with lisp, if he compares it with equivalent Java code. Reading the name of the first entry in a zip file:
user=> (import '(java.net URL) '(java.util.zip ZipInputStream))
nil
user=> (-> "http://clojure.googlecode.com/files/clojure_20081217.zip" URL. .openStream ZipInputStream. .getNextEntry bean :name)
"clojure/"
[edit] cond
(defn fib [n]
(cond
(== 0 n) 0
(== 1 n) 1
(< 1 n) (+
(fib (- n 1))
(fib (- n 2)))))
[edit] condp
(let [a 5]
(condp = a
1 "hi"
5 "fun"))
-> "fun"
condp throws an java.lang.IllegalArgumentException if no clauses are matched in the above example. condp also allows to add a single default expression after the clauses whose value will be returned if no clauses matches.
(let [a 7]
(condp = a
1 "hi"
5 "fun"
"no match"))
-> "no match"
[edit] str
user=> (str [1 2])
"[1 2]"
user=> (str 1 2)
"12"
[edit] Variable Definition
This page provides examples for the following functions.
[edit] Sequence Building
This page provides examples for the following functions.
[edit] MultiMethod
This page defines the following functions
[edit] Reference Tools
This page defines the following functions.
[edit] do Macros
This page provides examples for the following functions.
[edit] Hash-map tools
This page provides examples for the following functions.
[edit] Sequence Operators
This page provides examples for the following functions.
[edit] Predicate functions
This page provides examples for the following functions.
[edit] Recursion Tools
This page defines the following functions.
[edit] Function Tools
This page provides examples for the following functions.
[edit] Mapping Operators
This page provides examples for the following functions.
[edit] Java Interaction
This page provides examples for the following functions.
[edit] Namespaces
[edit] ns
(ns test.test
(:refer-clojure :exclude [+ -]))
(defn +
[a b]
33)
(+ 1 2) ;= 33
[edit] require
(require '[clojure.zip :as zip])
(require ['clojure.contrib.sql :as 'sql])
[edit] Math Functions
[edit] rem
user=> (rem 5 2)
1
[edit] Structure Tools
This page provides examples for the following functions.
[edit] test
user=> (defn
#^{:test (fn []
(assert (= 4 (myadd 2 2))))}
myadd [a b]
(+ a b))
#'user/myadd
user=> (test #'myadd)
:ok
[edit] Sets
[edit] union
user=> (clojure.set/union #{1 2 3} #{1 4 7})
#{1 2 3 4 7}
[edit] Zip Utility
[edit] zippers
user=> (-> (zip/vector-zip [[1 2] 3 [[4 5] 7 8]])
zip/down
zip/right
zip/right
zip/down
zip/down
zip/right
(zip/edit inc)
zip/root)
[[1 2] 3 [[4 6] 7 8]]
A zipper is a way to modify a tree in a functional way- To do this conveniently, there needs to be a way for you to "drill down" a branch in the tree and perform a local edit, without having to tediously rebuild the upper areas of the tree again. In this example, the vector-zip function converts nested arrays into a zipper. Then we "drill down" the zipper with "down" and "right". Once we've reached the node we want to edit, we do so with "edit", in this case, incrementing the number. Then we can call "root" to convert the zipper back into nested arrays. Note that we were able to efficiently make a pinpoint change in the tree in a very elegant manner.
[edit] zipmap
user=> (let [ks [1 3 4]] (zipmap ks (map inc ks)))
{4 5, 3 4, 1 2}