Skip to content

Instantly share code, notes, and snippets.

@lorn
Created March 23, 2016 16:18
Show Gist options
  • Save lorn/3e85f5918357124bca6d to your computer and use it in GitHub Desktop.
Save lorn/3e85f5918357124bca6d to your computer and use it in GitHub Desktop.
; CIDER 0.10.0snapshot (package: 20150824.244) (Java 1.8.0_60, Clojure 1.7.0, nREPL 0.2.10)
user> (StringBuilder "Sam")
RuntimeException Expecting var, but StringBuilder is mapped to class java.lang.StringBuilder clojure.lang.Util.runtimeException (Util.java:221)
user> (StringBuilder. "Sam")
#object[java.lang.StringBuilder 0x3711d177 "Sam"]
user> ["mixed" 4 "content"]
[""]
user> ["mixed" 4]
["mixed" 4]
user>
user>
user> (.get ["This" "is" "a" "vector"] 2)
"a"
user> #{"this" "is" "a" "set"}
( = )
user>
user>
user> (= #{"this" "is" "a" "set"} #{"a" "set" "is" "this"} )
true
user> (.equals #{"this" "is" "a" "set"} #{"a" "set" "is" "this"})
true
user> \c
user> \space
\space
user> (throw (Exception "AAARRGH"))
CompilerException java.lang.RuntimeException: Expecting var, but Exception is mapped to class java.lang.Exception, compiling:(/tmp/form-init879491987502932725.clj:1:8)
user> *e
#error {
:cause "Expecting var, but Exception is mapped to class java.lang.Exception"
:via
[{:type clojure.lang.Compiler$CompilerException
:message "java.lang.RuntimeException: Expecting var, but Exception is mapped to class java.lang.Exception, compiling:(/tmp/form-init879491987502932725.clj:1:8)"
:at [clojure.lang.Compiler analyzeSeq "Compiler.java" 6730]}
{:type java.lang.RuntimeException
:message "Expecting var, but Exception is mapped to class java.lang.Exception"
:at [clojure.lang.Util runtimeException "Util.java" 221]}]
:trace
[[clojure.lang.Util runtimeException "Util.java" 221]
[clojure.lang.Compiler lookupVar "Compiler.java" 7092]
[clojure.lang.Compiler isMacro "Compiler.java" 6571]
[clojure.lang.Compiler macroexpand1 "Compiler.java" 6626]
[clojure.lang.Compiler analyzeSeq "Compiler.java" 6709]
[clojure.lang.Compiler analyze "Compiler.java" 6524]
[clojure.lang.Compiler analyze "Compiler.java" 6485]
[clojure.lang.Compiler$ThrowExpr$Parser parse "Compiler.java" 2409]
[clojure.lang.Compiler analyzeSeq "Compiler.java" 6723]
[clojure.lang.Compiler analyze "Compiler.java" 6524]
[clojure.lang.Compiler analyze "Compiler.java" 6485]
[clojure.lang.Compiler$BodyExpr$Parser parse "Compiler.java" 5861]
[clojure.lang.Compiler$FnMethod parse "Compiler.java" 5296]
[clojure.lang.Compiler$FnExpr parse "Compiler.java" 3925]
[clojure.lang.Compiler analyzeSeq "Compiler.java" 6721]
[clojure.lang.Compiler analyze "Compiler.java" 6524]
[clojure.lang.Compiler eval "Compiler.java" 6779]
[clojure.lang.Compiler eval "Compiler.java" 6745]
[clojure.core$eval invoke "core.clj" 3081]
[clojure.main$repl$read_eval_print__7099$fn__7102 invoke "main.clj" 240]
[clojure.main$repl$read_eval_print__7099 invoke "main.clj" 240]
[clojure.main$repl$fn__7108 invoke "main.clj" 258]
[clojure.main$repl doInvoke "main.clj" 258]
[clojure.lang.RestFn invoke "RestFn.java" 1523]
[clojure.tools.nrepl.middleware.interruptible_eval$evaluate$fn__623 invoke "interruptible_eval.clj" 58]
[clojure.lang.AFn applyToHelper "AFn.java" 152]
[clojure.lang.AFn applyTo "AFn.java" 144]
[clojure.core$apply invoke "core.clj" 630]
[clojure.core$with_bindings_STAR_ doInvoke "core.clj" 1868]
[clojure.lang.RestFn invoke "RestFn.java" 425]
[clojure.tools.nrepl.middleware.interruptible_eval$evaluate invoke "interruptible_eval.clj" 56]
[clojure.tools.nrepl.middleware.interruptible_eval$interruptible_eval$fn__665$fn__668 invoke "interruptible_eval.clj" 191]
[clojure.tools.nrepl.middleware.interruptible_eval$run_next$fn__660 invoke "interruptible_eval.clj" 159]
[clojure.lang.AFn run "AFn.java" 22]
[java.util.concurrent.ThreadPoolExecutor runWorker "ThreadPoolExecutor.java" 1142]
[java.util.concurrent.ThreadPoolExecutor$Worker run "ThreadPoolExecutor.java" 617]
[java.lang.Thread run "Thread.java" 745]]}
user> (def k (java.util.ArrayList. [1 2]))
#'user/k
user> (class k)
java.util.ArrayList
user> (k)
ClassCastException java.util.ArrayList cannot be cast to clojure.lang.IFn user/eval8191 (form-init879491987502932725.clj:1)
user> (k 2)
ClassCastException java.util.ArrayList cannot be cast to clojure.lang.IFn user/eval8533 (form-init879491987502932725.clj:1)
user> (def a 1)
#'user/a
user> a
1
user> k
[1 2]
user> ;; set cannot have duplicate values
user> #{a a}
IllegalArgumentException Duplicate key: a clojure.lang.PersistentHashSet.createWithCheck (PersistentHashSet.java:68)
user> {"This" "is" "a" "map"}
{"This" "is", "a" "map"}
user> ;; map need to have a even number of elements
user> {"This" "is" "not"}
RuntimeException Map literal must contain an even number of forms clojure.lang.Util.runtimeException (Util.java:221)
user>
user>
user> (def l [1 3 4 8 2])
#'user/l
user> (sort l)
(1 2 3 4 8)
user> (class k)
java.util.ArrayList
user> (def m (java.util.HashMap.))
#'user/m
user> (.put m k 5)
nil
user> m
{[1 2] 5}
user> (.get m k)
5
user> (get m k)
5
user> (.set k 0 7)
1
user> k
[7 2]
user> m
{[7 2] 5}
user> (get m k)
nil
user> m
{[7 2] 5}
user> (def v [1 2])
#'user/v
user> (def m (hash-map v 5))
#'user/m
user> m
{[1 2] 5}
user> (conj v 7)
[1 2 7]
user> (get m v)
5
user> v
[1 2]
user> (conj v 3)
[1 2 3]
user> (list 1 2 3)
(1 2 3)
user> (def l (list 1 2))
#'user/l
user> l
(1 2)
user> v
[1 2]
user> (conj v 3)
[1 2 3]
user> (conj l 3)
(3 1 2)
user> *1
(3 1 2)
user> *3
[1 2 3]
user> ;; *n is a shortcut for the last 3 variables used on repl
user> (class *1)
clojure.lang.PersistentVector
user> (class *3)
clojure.lang.PersistentList
user> '(1 2 3)
(1 2 3)
user> (class *1)
clojure.lang.PersistentList
user> (class *3)
java.lang.Class
user> (class *2)
java.lang.Class
user> (class '(1 2 3))
clojure.lang.PersistentList
user> m
{[1 2] 5}
user> (assoc m 7 8)
{7 8, [1 2] 5}
user> (require 'clojure.repl)
nil
user> (clojure.repl/doc assoc)
-------------------------
clojure.core/assoc
([map key val] [map key val & kvs])
assoc[iate]. When applied to a map, returns a new map of the
same (hashed/sorted) type, that contains the mapping of key(s) to
val(s). When applied to a vector, returns a new vector that
contains val at index. Note - index must be <= (count vector).
nil
user> (def m1 "Our cool map")
#'user/m1
user> (defn foo "Our 1st function" [] "bar")
#'user/foo
user> (foo)
"bar"
user> (cloure.repl/doc m1)
ClassNotFoundException cloure.repl java.net.URLClassLoader.findClass (URLClassLoader.java:381)
user> (clojure.repl/doc m1)
-------------------------
user/m1
nil
nil
user> (clojure.repl/doc foo)
-------------------------
user/foo
([])
Our 1st function
nil
user> ;; anonumous function
user> (fn [x] (str "Hello" x))
#object[user$eval8590$fn__8591 0x224e3070 "user$eval8590$fn__8591@224e3070"]
user> ;; a true function
user> m
{[1 2] 5}
user> get (m [1 2])
#object[clojure.core$get 0x462690a "clojure.core$get@462690a"]
5
user> (get m [1 2])
5
user> ;; function? its a true/false function
user> (contains? m [1 2])
true
user> (contains? m 5)
false
user> ;; diferenct btw contains? from clojure and (.contains) from java
user> v
[1 2]
user> (contains? v 1)
true
user> (contains? v 2)
false
user> (.contains v 1)
true
user> (.contains v 2)
true
user> ;; contains?, clojure version, receive the key of the vector, and the vector dont have 3 items
user> ;; with set its different
user> (def set_example #{1 2})
#'user/set_example
user> (contains? set_example 1)
true
user> (contains? set_example 2)
true
user> { 1 2 3}
RuntimeException Map literal must contain an even number of forms clojure.lang.Util.runtimeException (Util.java:221)
user> (def my_map {:age 30, :name "lorn"})
#'user/my_map
user> my_map
{:age 30, :name "lorn"}
user> (get my_map :name)
"lorn"
user> (get my_map name)
nil
user> (def new_map {"foo" 32})
#'user/new_map
user> new_map
{"foo" 32}
user> ;; function example
user> (defn foo [information] )
#'user/foo
user> (def foo [information] )
CompilerException java.lang.RuntimeException: Unable to resolve symbol: information in this context, compiling:(/tmp/form-init879491987502932725.clj:1:1)
user>
user>
user>
user> (get {:a 1} :a)
1
user> (get {:a 1} :b)
nil
user> (get {:a 1} :b "not found")
"not found"
user> (get ["one" "two"] 0 "not found")
"one"
user> (get ["one" "two"] 0 "not found")
"one"
user> (get ["one" "two"] 1 "not found")
"two"
user> (get ["one" "two"] -3 "not found")
"not found"
user> (nth ["one" "two"] 1 "not found")
"two"
user> (nth ["one" "two"] 2 "not found")
"not found"
user> (nth ["one" "two"] 2)
IndexOutOfBoundsException clojure.lang.PersistentVector.arrayFor (PersistentVector.java:153)
user> (assoc {:a 1} :b 2)
{:a 1, :b 2}
user> (assoc ["a" "b"] 0 "zero")
["zero" "b"]
user> (dissoc {:a 1 :b 2} :a)
{:b 2}
user> (pop [1 2 3])
[1 2]
user> (conj [1 2 3] 4)
[1 2 3 4]
user> (conj {:a 1} :b 2)
IllegalArgumentException Don't know how to create ISeq from: clojure.lang.Keyword clojure.lang.RT.seqFrom (RT.java:528)
user> (conj {:a 1} [:b 2])
{:a 1, :b 2}
user> (find {:a 1 :b 2} :a)
[:a 1]
user> (find {:a 1 :b 2} 1)
nil
user> (peek [1 2 3])
3
user> (reduce (fn [acc v] (+ acc v)) 0 [1 2 3] )
6
user> (reduce + 0 [1 2 3])
6
user>
(reduce (fn [acc entry]
(if (= 1 v)
(conj acc k)
acc
)
)
[] {:a 1 :b 2 :c 1})
[]
user> (reduce (fn [acc k v]
(if (= 1 v)
(conj acc k)
acc
)
)
[] {:a 1 :b 2 :c 1})
ArityException Wrong number of args (2) passed to: user/eval10029/fn--10030 clojure.lang.AFn.throwArity (AFn.java:429)
user> (reduce conj #{} [1 1 2 2 3 3])
#{1 3 2}
user> (reduce conj #{} [1 1 2 2 3 3])
#{1 3 2}
user> (reduce (fn [acc x]))
ArityException Wrong number of args (1) passed to: core/reduce clojure.lang.AFn.throwArity (AFn.java:429)
user> (reduce (fn [acc x]
(conj acc (f x))
))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: f in this context, compiling:(/tmp/form-init879491987502932725.clj:2:27)
user> (reduce (fn [acc k v]
(if (= 1 v)
(conj acc k
acc))
[] {:a 1 :b 2 :c 1}))
ArityException Wrong number of args (1) passed to: core/reduce clojure.lang.AFn.throwArity (AFn.java:429)
user> (reduce (fn [acc x]
(conj acc [ x x ]))
[] [1 2 3])
[[1 1] [2 2] [3 3]]
user> (reduce (fn [acc x]
(conj acc x x))
[] [1 2 3])
[1 1 2 2 3 3]
user> ;; reduce
user> ;; + is the combination function
user> ;; acc v
user> ;; 3 [1 2 3 4 5]
user> ;; acc x
user> ;; 3 1
user> ;; 4 2
user> ;; 6 3
user> ;; 9 4
user> ;; 13 5
user> ;; 18
user>
user> (clojure.repl/doc reduce)
-------------------------
clojure.core/reduce
([f coll] [f val coll])
f should be a function of 2 arguments. If val is not supplied,
returns the result of applying f to the first 2 items in coll, then
applying f to that result and the 3rd item, etc. If coll contains no
items, f must accept no arguments as well, and reduce returns the
result of calling f with no arguments. If coll has only 1 item, it
is returned and f is not called. If val is supplied, returns the
result of applying f to val and the first item in coll, then
applying f to that result and the 2nd item, etc. If coll contains no
items, returns val and f is not called.
nil
user> (reduce (fn [acc value] (inc acc)) 0 [1 2 3 4 5])
5
user> (reduce (fn [acc value-is-ignored] (inc acc)) 0 [1 2 3 4 5])
5
user> ;; _ underscore is used when a parameter is not necessary
user> (reduce (fn [acc _] (inc acc)) 0 [1 2 3 4 5])
5
user>
user> (defn f [acc val]
)
#'user/f
user> (defn f [acc val]
stuff)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: stuff in this context, compiling:(/tmp/form-init879491987502932725.clj:1:1)
user> (defn f1 [acc val]
stuff)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: stuff in this context, compiling:(/tmp/form-init879491987502932725.clj:1:1)
user> (defn f [_ val]
val)
#'user/f
user> f
#object[user$f 0x11b0ccee "user$f@11b0ccee"]
user> f(0,1)
#object[user$f 0x11b0ccee "user$f@11b0ccee"]ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn user/eval10283 (form-init879491987502932725.clj:1)
user> f([0 1])
#object[user$f 0x11b0ccee "user$f@11b0ccee"]ArityException Wrong number of args (0) passed to: PersistentVector clojure.lang.AFn.throwArity (AFn.java:429)
user> (f [0 1])
ArityException Wrong number of args (1) passed to: user/f clojure.lang.AFn.throwArity (AFn.java:429)
user> (reduce f nil [1 2 3 4 5 "cheese"])
"cheese"
user> ; f nil 1 = 1
user> ; f 1 2 = 2
user> ; f 2 3 = 3
user> ; f 3 4 = 4
user> ; f 4 5 = 5
user> ; f 5 "cheese" = cheese
user> ; cheese
user>
user> (f 0 1)
1
user> (defn my-map
[f]
(reduce (fn [acc x]
(conj acc (f x)))
[] l))
#'user/my-map
user> (my-map inc)
[2 3]
user> l
(1 2)
user> (my-map (fn [x] (+ x 2)))
[3 4]
user> (my-map dec)
[0 1]
user> (defn my-mapcat
[f coll]
(reduce (fn [acc x]
(let [result (f x)]
(my-into acc result)))
[] coll))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: my-into in this context, compiling:(/tmp/form-init879491987502932725.clj:5:21)
user> (defn my-into)
IllegalArgumentException Parameter declaration missing clojure.core/assert-valid-fdecl (core.clj:7174)
user> (defn my-into
[target coll]
(reduce conj target coll))
#'user/my-into
user> (defn my-mapcat
[f coll]
(reduce (fn [acc x]
(let [result (f x)]
(my-into acc result)))
[] coll))
#'user/my-mapcat
user> (my-mapcat l)
ArityException Wrong number of args (1) passed to: user/my-mapcat clojure.lang.AFn.throwArity (AFn.java:429)
user> (dup l)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: dup in this context, compiling:(/tmp/form-init879491987502932725.clj:1:1)
user> (my-mapcat dup l)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: dup in this context, compiling:(/tmp/form-init879491987502932725.clj:1:1)
user> (defn (fn [x] [x x]))
IllegalArgumentException First argument to defn must be a symbol clojure.core/defn--4156 (core.clj:281)
user> (defn dup (fn [x] [x x]))
IllegalArgumentException Parameter declaration "fn" should be a vector clojure.core/assert-valid-fdecl (core.clj:7190)
user> (defn dup ([x] [x x]))
#'user/dup
user> (my-mapcat dup l)
[1 1 2 2]
user> (let [result (inc 5)]
result )
6
user> (let [result (vector 5)]
(my-into [0 result]))
ArityException Wrong number of args (1) passed to: user/my-into clojure.lang.AFn.throwArity (AFn.java:429)
user> (defn my-mapcat2
[f coll]
(reduce (fn [acc x]
(let [result (f x)]
(into acc result)))
[] coll))
#'user/my-mapcat2
user> (my-mapcat2 dup l)
[1 1 2 2]
user>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment