Skip to content

Instantly share code, notes, and snippets.

(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 / 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.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-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)]
@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 / go-lightly.util.clj
Last active December 10, 2015 11:58
Part of the go-lightly utility namespace showing the go and go& macros.
(ns thornydev.go-lightly
(:refer-clojure :exclude [peek take])
(:import (java.io Closeable)
(java.util ArrayList)
(java.util.concurrent LinkedTransferQueue TimeUnit
LinkedBlockingQueue TimeoutException)))
;; ---[ go routines ]--- ;;
(def inventory (atom []))
@quux00
quux00 / boring-generators.go
Last active December 10, 2015 11:39
Simple example of Go channels from Rob Pike's 2012 Google IO presentation: http://www.youtube.com/watch?v=f6kdp27TYZs&feature=youtu.be
package main;
import (
"fmt"
"math/rand"
"time"
"os"
)
// Channels-driven concurrency with Go
(ns thornydev.top100.heap
(:require [thornydev.lib.leftist-heap
:refer [pq-first pq-rest pq-insert pq-empty]]))
(defn distance [[x y]]
(+ (java.lang.Math/abs x) (java.lang.Math/abs y)))
(defn dist-lt? [pt1 pt2]
(< (distance pt2) (distance pt1)))
@quux00
quux00 / gist:4299232
Created December 15, 2012 21:08
Leftist Priority Queue Heap in Clojure
(ns thornydev.lib.leftist-heap)
;; Implementation of a "leftist priority queue" on an immutable
;; heap data structure
;; This code was (manually) transpiled into Clojure from
;; the Scheme version here:
;; http://programmingpraxis.com/2009/05/05/priority-queues/2/
;; ---[ core lib fns ]--- ;;
(defn dist-then-first [pt1 pt2]
(let [dist1 (distance pt1)
dist2 (distance pt2)]
(if (= dist1 dist2)
(> (first pt1) (first pt2))
(> dist1 dist2))))
(defn mk-sorted-sift-fn [max-size]
(fn [sst pt]
(if (< (count sst) max-size)