# Clojure
WARNING
This page is a draft containing my personal notes on Clojure. I extracted them from the time I was comparing languages.
# Introduction
# FAQ
# How to create a function?
(defn test "test function" [arg] (print arg))
Or by using a lambda function.
(def test "test function" (fn [x] (print arg)))
# How to create datastructure?
(defstruct structname :ref1 :ref2 :refn)
# How to use datastructure?
(struct structname "ref1_value" "ref2_value" "refn_value")
# How to use a map datastructure?
(map (fn [x] (+ 1 x)) [1 2 3])
# How to use reduce?
(reduce (fn [x y] (+ x y)) [1 2 3 4])
# How to use list comprehension?
- https://clojuredocs.org/clojure.core/for (opens new window)
- https://clojuredocs.org/clojure.core/doseq (opens new window)
- http://stackoverflow.com/questions/23796941/how-to-do-list-comprehension-in-clojure (opens new window)
# How to define types?
# How to create agents?
(agent [:state])
(def myagent [:state])
- http://clojure.org/reference/agents (opens new window)
- https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/agent (opens new window)
- https://clojuredocs.org/clojure.core/agent (opens new window)
- https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Agent.java (opens new window)
- http://lethain.com/a-couple-of-clojure-agent-examples/ (opens new window)
- http://java.ociweb.com/mark/clojure/article.html#Agents (opens new window)
- http://blog.japila.pl/2011/01/clojure-concurrency-with-agents-no-more-explicit-threads/ (opens new window)
- http://www.thattommyhall.com/2014/02/24/concurrency-and-parallelism-in-clojure/ (opens new window)
- http://clojure.wladyka.eu/posts-output/2016-04-06-share-state.html (opens new window)
# How to call an agent?
(send myagent action)
Another solution is to use send-off function.
(send-off myagent action)
# What can I do if my agent is dead?
(restart-agent myagent [:state])
# How to use pattern matching?
core.match module is required. You can use by using adding it in the
requirement.
[org.clojure/core.match "0.3.0-alpha4"]
Next, use require.
(require '[clojure.core.match :refer [match]])
- https://github.com/clojure/core.match
- https://github.com/clojure/core.match/wiki/Understanding-the-algorithm
# How to use deps.edn file?
- https://clojure.org/reference/deps_and_cli (opens new window)
- https://tomekw.com/clojure-deps-edn-a-basic-guide/ (opens new window)
# References and Resources
- https://clojure.github.io/clojure/
- http://clojure-doc.org/
- https://clojuredocs.org/
- http://clojure.org/api/api
- http://github.com/clojure/clojure/
- http://clojure.org/api/cheatsheet