Clojure Keywords

  1. first
    return first element in list or vector
  2. rest
    return all elements except the first
  3. cons
    • change in a list
    • Returns a new seq where x is the first element and seq is the rest.
    • (cons 1 '(2 3 4 5 6))
    • ;;=> (1 2 3 4 5 6)
  4. conj
    • ;; notice that conjoining to a vector is done at the end
    • (conj [1 2 3] 4)
    • ;;=> [1 2 3 4]

    • ;; notice conjoining to a list is done at the beginning
    • (conj '(1 2 3) 4)
    • ;;=> (4 1 2 3)
  5. list
    '( )
  6. map
    { }
  7. vector
    [ ]
  8. peek - list
    return the first element in a list
  9. pop - list
    removes first element
  10. peek - vector
    returns last element
  11. pop - vector
    removes last element
  12. big difference between keywords with list and vector
    • lists work with the first elements
    • vectors work with the last elements
  13. subvec
    • return the end element but not the start
    • (subvec ("a" "big" "red" "apple") 1 3)
    • returns=>   "red" "apple"
    • vector only
  14. set
    • remove repeating
    • returns distinct elements
  15. subvec with only one number
    • returns elements after the start to the end
    • user=> (subvec [12 3 4 5 6 7] 2)
    • [4 5 6 7]
  16. #( )
    set
  17. dissoc
    • map
    • dissoc[iate].
    • removes the given element returning a new map
  18. keys vs. vals
    • 10 "Ten"
    • key = 10
    • val = "Ten"
  19. %1 %2 %3
    use for multiple elements to be called
  20. assoc
    • assoc[iate]
    • changes the selected element value
    • user=> (assoc [1 2 3] 0 10)
    • [10 2 3]
  21. false?
    Returns true if x is the value false, false otherwise.
  22. nil?
    Returns true if x is nil, false otherwise.
  23. filter
    • (filter even? (range 10))
    • ;;=> (0 2 4 6 8)
  24. range
    • (range 5)
    • = 0 1 2 3 4
  25. drop
    • Returns a lazy sequence of all but the first n items in coll.
    • (drop 2 [1 2 3 4])
    • ;;=> (3 4)
  26. take
    • Returns a lazy sequence of the first n items in coll, or all items if there are fewer than n.
    • (take 3 '(1 2 3 4 5 6))
    • ;;=> (1 2 3)
  27. repeat
    • user=> (take 5 (repeat "x"))
    • ("x" "x" "x" "x" "x")
  28. identity
    • keyword
    • (fn[n] n)
  29. for
    • (def digits (seq [1 2 3]))
    • (for [x1 digits x2 digits] (* x1 x2))
    • ;;=> (1 2 3 2 4 6 3 6 9)
  30. compliment
    opposite
  31. symbol
    • 'a
    • "a"
  32. let
    bind to value
  33. comp
    • composition
    • can bind a word to 2 or more values
    • last element gets applied first
    • ((comp str +) 8 8 8) ;;=> "24"
  34. dec
    -1
  35. destructuring
    pull out 1 or more values from a structure
  36. { }
    map
  37. atom
    • Creates and returns an Atom with an initial value of x
    • user=> (def my-atom (atom 0))
  38. swap!
    • swaps the value of atom to be
    • (def counter (atom 0))
    • (swap! counter inc)
    • ;;=> 1
  39. immutable
    unchanging over time or unable to be changed.
  40. do
    • used to evaluate multiple expressions in order
    • => (do
    • (println "LOG: Computing...")
    • (+ 1 1))
    • LOG: Computing...
    • 2
  41. compare-and-set!
    • sets the value of atom to newval if and only if the
    • current value of the atom is identical to oldval
  42. partition
    stops if partition cannot be filled in contrast to the range

    • (partition 4 (range 22))
    • ;;=> ((0 1 2 3) (4 5 6 7) (8 9 10 11) (12 13 14 15) (16 17 18 19))
  43. partition-all
    Returns a lazy sequence of lists like partition, but may include partitions with fewer than n items at the end.

    (partition 4 (range 22));;=> ((0 1 2 3) (4 5 6 7) (8 9 10 11) (12 13 14 15) (16 17 18 19) (20 21))
Author
djon
ID
293035
Card Set
Clojure Keywords
Description
clojure keywords
Updated