Skip to content

Instantly share code, notes, and snippets.

@quux00
quux00 / iterate.rs
Created January 9, 2014 05:07
Simple Rust iteration example
use std::rand::{task_rng, Rng};
fn main() {
let names = ["Alice", "Bob", "Carol"];
for name in names.iter() {
let v = task_rng().shuffle(~[1,2,3]);
for num in v.iter() {
println!("{:s} says: {:d}", *name, *num);
}
}
@quux00
quux00 / knuth.go
Last active February 13, 2019 10:33
Knuth Fisher-Yates shuffle for Go (golang)
// implements Knuth or Fisher-Yates shuffle
package knuth
import (
"math/rand"
"time"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
@quux00
quux00 / barber.clj
Last active December 12, 2015 02:18
Implementation of the sleeping barbers concurrency example: https://en.wikipedia.org/wiki/Sleeping_barber_problem. This implementation based on Go routine implementation by Alexey Kacayev: https://gist.github.com/4688906. Uses the go-lightly Clojure library: https://github.com/midpeter444/go-lightly
(ns thornydev.go-lightly.examples.sleeping-barber.barber
(:refer-clojure :exclude [peek take])
(:require [thornydev.go-lightly.core :refer :all]))
;; Implementation based on Go routine implementation
;; by Alexey Kacayev: https://gist.github.com/4688906
;; This version uses two threads (Go routines) for:
;; - one to bring in clients to the shop one at a time
;; - one to select between handling a client just entered
@quux00
quux00 / barber.go
Last active December 12, 2015 01:08 — forked from kachayev/barber.go
package main
import (
"fmt"
"math/rand"
"time"
)
const (
CUTTING_TIME = 20
@quux00
quux00 / concPrimeSieve.go
Created January 12, 2013 17:57
Implementation of a prime sieve using a series of cascading Go channels building on each to filter out non-primes. From: http://play.golang.org/p/9U22NfrXeq
package main
// A concurrent prime sieve
// from: http://play.golang.org/p/9U22NfrXeq
// Send the sequence 2, 3, 4, ... to channel 'ch'.
func Generate(ch chan<- int) {
for i := 2; ; i++ {
ch <- i // Send 'i' to channel 'ch'.
}
@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
@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 / 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 / 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 / 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]