Skip to content

Instantly share code, notes, and snippets.

@quux00
quux00 / boring-generators.clj
Last active December 10, 2015 11:58
Clojure implementation of boring-generators.go (https://gist.github.com/4428907)
(ns thornydev.go-lightly.boring.generator-tq
(:require [thornydev.go-lightly :refer [go go& stop]])
(:import (java.util.concurrent LinkedTransferQueue)))
(defn- channel []
(LinkedTransferQueue.))
(defn- boring [msg]
(let [ch (channel)]
@quux00
quux00 / boring-generators-lamina.clj
Last active December 10, 2015 12:08
Alternative to https://gist.github.com/4430997 using the Clojure lamina library.
(ns thornydev.go-lightly.boring.generator-lamina
(:require [thornydev.go-lightly.util :refer [go&]]
[lamina.core :refer [channel enqueue close
closed? read-channel]]))
;; this version is REPL-friendly since the deamon go routine
;; will exit when the channel is closed
(defn- boring [msg]
(let [ch (channel)]
(ns thornydev.go-lightly.boring.generator-lamina
(:require
[thornydev.go-lightly.util :refer [go& with-channel-open]]
[lamina.core :refer [channel enqueue close
closed? read-channel]]))
(defn- boring [msg]
(let [ch (channel)]
(go& (loop [i 0]
(when-not (closed? ch)
@quux00
quux00 / boring-select.go
Last active December 10, 2015 13:18
Example of the select statement from Go concurrency programming from Rob Pike's talk on Google I/O 2012
package main;
import (
"fmt"
"math/rand"
"time"
"os"
)
// Channels-driven concurrency with Go
(ns thornydev.go-lightly.boring.select-timeout
(:refer-clojure :exclude [peek take])
(:require [thornydev.go-lightly :refer
[go stop put take select select-timeout
channel timeout-channel with-timeout]]))
(defn- boring [msg]
(let [ch (channel)]
(go (loop [i 0]
(put ch (str msg " " i))
@quux00
quux00 / select.clj
Created January 4, 2013 03:23
Initial simple implementation of Go "select" or Rack "sync" in Clojure
(defn- choose [ready-chans]
(.take (nth ready-chans (rand-int (count ready-chans)))))
(defn- peek-channels [channels]
(let [ready (doall (keep #(when-not (nil? (.peek %)) %) channels))]
(if (seq ready)
(nth ready (rand-int (count ready))) ;; pick at random if >1 ready
(Thread/sleep 0 500))))
(defn- probe-til-ready [channels]
@quux00
quux00 / search-google.go
Created January 5, 2013 01:56
Fake google search example in Go using concurrent Go routines pushing to a common Go channel. From Rob Pike's Google IO 2012 presentation: http://www.youtube.com/watch?v=f6kdp27TYZs&feature=youtu.be and adapted from Alexey Kachayev's transcription of Pike's slides: https://gist.github.com/3124594#file-go-channels-7-search-go
package main;
import (
"fmt"
"math/rand"
"time"
)
var (
Web1 = fakeSearch("web1")
@quux00
quux00 / gist:4462372
Last active December 10, 2015 16:39
Unit test snippet for using timeout-channel from go-lightly library
(testing "timeout channel with other channels"
(let [ch1 (channel)
ch2 (channel)
tch (timeout-channel 250)
fnext-msg (partial select ch1 ch2 tch)]
(go (test-routine ch1 1))
(go (test-routine ch1 2))
(loop [msg (fnext-msg)]
(when-not (= msg :go-lightly/timeout)
(is (some #{1 2} [msg]))
@quux00
quux00 / select_lamina.clj
Last active December 10, 2015 16:48
Emulation of Go select with timeouts using lamina and some helper constructs from the go-lightly Clojure library. Ideas here adapted from: https://gist.github.com/3146759#file-clojure-channels-4-timeouts-clj
(ns thornydev.go-lightly.boring.select-lamina
(:require [thornydev.go-lightly :as go]
[thornydev.go-lightly.util :refer [with-channel-open]]
[lamina.core :refer [close] :as lam])
(:import (java.util.concurrent TimeoutException)))
(defn- boring [msg]
(let [ch (lam/channel)]
(go/go& (loop [i 0]
@quux00
quux00 / conc-prime-sieve.clj
Last active December 10, 2015 17:49
Concurrent Prime Sieve in Clojure using CSP Go-style concurrency channels. It uses a daisy-chain filter process.
(ns thornydev.go-lightly.primes.conc-prime-sieve
(:refer-clojure :exclude [take peek])
(:require [thornydev.go-lightly :refer :all]))
;; A Clojure implementation of the Concurrent Primary Sieve
;; example in Go, using the go-lightly library
;; Note: could not use a Lamina channel for this particular
;; implementation, bcs it uses ConcurrentLinkedQueue,
;; which is unbounded and non-blocking