Skip to content

Instantly share code, notes, and snippets.

@bostonou
bostonou / lazy-cat-fib-1.clj
Created April 22, 2015 13:08
lazy-cat-fib 1
(take 1 head-fibo)
(take 1 (lazy-cat [0N 1N] (map + head-fibo (rest head-fibo))))
(take 1 (concat (lazy-seq [0N 1N])
(lazy-seq (map + head-fibo (rest head-fibo)))))
@bostonou
bostonou / lazy-cat-fib.clj
Created March 31, 2015 03:55
lazy cat fibonacci
;;So let’s work out the first couple of simple cases:
(take 1 head-fibo)
(take 1 (lazy-cat [0N 1N] (map + head-fibo (rest head-fibo))))
(take 1 (concat (lazy-seq [0N 1N])
(lazy-seq (map + head-fibo (rest head-fibo)))))
;;since we’re taking 1, the 0N of the first lazy-seq suffices and
;;we don’t need to further evaluate
;; => (0N)
(defn mutate-chan
[c]
(<!! c))
(let [c (chan)]
(thread (>!! c 1))
(mutate-chan c)
(<!! c)) ; the 1 is now gone!
(defn =?
[a b]
(= a b))
(defn channel=?
[a c]
(thread (=? a (<!! c))))
(def partial=? (partial =? 10))
(defn =?
[a b]
(= a b))
(def partial=? (partial =? 10))
;... do some stuff
(partial=? 10)
(defn channel=?
[a c]
(let [begin (System/currentTimeMillis)
v (= a (<!! c))]
(println "Waited" (- (System/currentTimeMillis) begin) "ms")
v))
(let [b (thread (<!! (timeout 500)) 10)]
(channel=? 10 b))
(defn future=?
[a f]
(let [begin (System/currentTimeMillis)
v (= a @f)]
(println "Waited" (- (System/currentTimeMillis) begin) "ms")
v))
(let [b (future (Thread/sleep 500) 10)]
(future=? 10 b))
(defn =?
[a b]
(= a b))
@bostonou
bostonou / Test.coffee
Created May 10, 2011 19:03
@ gives incorrect reference to "this"
class Test
constructor: ->
Test.count = 2
@count: 0
@show: ->
console.log "Count = #{@count}"
console.log ('Not printed' for i in [1..@count]).join(" ")
new Test
Test.show()