Mafinar Rashid Khan
Product Manager
Panacea Systems Limited
@mafinar
“Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days, even if you never actually use Lisp itself a lot.”
“Lisp has jokingly been called "the most intelligent way to misuse a computer". I think that description is a great compliment because it transmits the full flavor of liberation: it has assisted a number of our most gifted fellow humans in thinking previously impossible thoughts.”
- Count Lestat, Interview with the Vampire
Basically forgetting about assignments and those little boxes holding values, you play pillow passing with values, values that never change.
(def brown [0 1 2 3 4 5 6 7 8])
(def blue (assoc brown 5 'beef))
(def brown [0 1 2 3 4 5])
(def blue (pop brown))
(def green (pop brown))
(map square (range 100))
(->> (range 1 100)
(comp #(square %) #(cube %))
(apply +))
(defn pow [n]
(fn [x] (apply * (repeat n x))))
(def square (pow 2))
(def qubic (pow 3))
But you don't do them at the same time, you "interleave"
If a task takes two hands, and you have two hands, you cannot do more than two tasks paralelly.
(def what-is-the-answer-to-life (future
(println "[Future] started computation")
(Thread/sleep 3000) ;; running for 3 seconds
(println "[Future] completed computation")
42))
(println "[Main] created future")
(Thread/sleep 1000)
(println "[Main] do other things while waiting for the answer")
(println "[Main] get the answer")
(println "[Main] the result" @what-is-the-answer-to-life)
(def p (promise)) ;=#'user/p
(realized? p) ;=false
(deliver p "as-promised")
(realized? p) ;=true
@p ;="as-promised"
(def click-count (atom 0))
(defn counting-component []
[:div
"The atom " [:code "click-count"] " has value: "
@click-count ". "
[:input {:type "button" :value "Click me!"
:on-click #(swap! click-count inc)}]])
(def accounts
[(ref 0)
(ref 10)
(ref 20)
(ref 30)])
(defn transfer [src-account
dest-account
amount]
(dosync
(alter dest-account + amount)
(alter src-account - amount)))
(map deref accounts)
;=> (5 5 20 30)
I don't like statically typed language and I have Java anonymous classes. JavaScript is cool so I shall use JavaScript.
-- NOT me.
var p_client = new Db('integration_tests_20', new Server("127.0.0.1", 27017, {}),
{'pk':CustomPKFactory});
p_client.open(function(err, p_client) {
p_client.dropDatabase(function(err, done) {
p_client.createCollection('test_custom_key', function(err, collection) {
collection.insert({'a':1}, function(err, docs) {
collection.find({'_id':new ObjectID("aaaaaaaaaaaa")}, function(err, cursor) {
cursor.toArray(function(err, items) {
test.assertEquals(1, items.length);
// Let's close the db
p_client.close();
});
});
});
});
});
});
(defn upper-caser
[in]
(let [out (chan)]
(go (while true (>! out (clojure.string/upper-case (<! in)))))
out))
(defn reverser
[in]
(let [out (chan)]
(go (while true (>! out (clojure.string/reverse (<! in)))))
out))
(defn printer
[in]
(go (while true (println (<! in)))))
(def in-chan (chan))
(def upper-caser-out (upper-caser in-chan))
(def reverser-out (reverser upper-caser-out))
(printer reverser-out)
(>!! in-chan "redrum")
; => MURDER
(>!! in-chan "repaid")
; => DIAPER
Credits: Clojure for the Brave and True
(Integer/parseInt "0")
;; Integer.parseInt("0")
(.toUpperCase "hello")
;; "hello".toUpperCase()
(.. System
(getProperties)
(get "os.name"))
;; System.getProperties().get("os.name")
(doto (new java.util.HashMap)
(.put "a" 1)
(.put "b" 2))
; HashMap hm = new HashMap();
; hm.put("a", 1);
; hm.put("b", 1);
(def-html page [title, body]
(title title)
(body (div (map employee-snippet employee-list))))
(def-snippet employee-snippet [e]
(table :data (employees) :class "alternate-row"))
(def-form employee-form
(html-form :map-with (get-employee-structure)
:on-click create-employee!))
(defn employee-list
(sql
(select "employee" [:first-name :last-name] :order-by "age")))
(defn create-employee!
(sql!
(create "employee" [:first-name :last-name] ["Mafinar" "Khan"]))
;; UNLESS <YES> <NO> = generate_code("if (!_LOGIC_) { _YES_ } else { _NO_ }")
(defmacro unless [pred a b] `(if (not ~pred) ~a ~b))