Skip to content

Instantly share code, notes, and snippets.

@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()
(defn =?
[a b]
(= a 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 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 =?
[a b]
(= a b))
(def partial=? (partial =? 10))
;... do some stuff
(partial=? 10)
(defn =?
[a b]
(= a b))
(defn channel=?
[a c]
(thread (=? a (<!! c))))
(def partial=? (partial =? 10))
(defn mutate-chan
[c]
(<!! c))
(let [c (chan)]
(thread (>!! c 1))
(mutate-chan c)
(<!! c)) ; the 1 is now gone!
@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)
@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 / cljs-line-reader.cljs
Last active August 29, 2015 14:21
Line by line reader in cljs
(ns cljs-line-reader.core
(:refer-clojure :exclude [flush])
(:require clojure.string
[cljs.core.async :refer [>!]])
(:require-macros [cljs.core.async.macros :refer [go]]))
(def fs (js/require "fs"))
(def stream (js/require "stream"))
(def ^:const eol (.-EOL (js/require "os"))) ;;eg "\n" or "\r\n"