This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// implements Knuth or Fisher-Yates shuffle | |
package knuth | |
import ( | |
"math/rand" | |
"time" | |
) | |
func init() { | |
rand.Seed(time.Now().UTC().UnixNano()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
) | |
const ( | |
CUTTING_TIME = 20 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'. | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main; | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
) | |
var ( | |
Web1 = fakeSearch("web1") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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] |