Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / monster.rs
Last active January 2, 2016 23:49
Learning Rust traits ... poorly
fn main() {
let mut mon1: ~Sleestak = ~Monster::new();
let mut mon2: ~Reaver = ~Monster::new();
let mut monsters = [mon1 as ~Monster, mon2 as ~Monster];
println!("{:?}", monsters);
println("------------------------------");
// attempt 1: fail
for m in monsters.iter() {
@quux00
quux00 / tckill.rs
Last active January 3, 2016 01:49
Mutable pointers in Rust
use std::run;
use std::str;
fn main() {
let options = run::ProcessOptions::new();
let process = run::Process::new("fuser", &[~"-m", ~"/media/truecrypt3"], options);
match process {
Some(mut p) => {
let outdata = p.finish_with_output();
let outtxt = str::from_utf8(outdata.output);
@quux00
quux00 / chinese-whispers.go
Created January 16, 2014 04:56
Chinese Whispers example by Rob Pike http://www.youtube.com/watch?v=f6kdp27TYZs
package main
import "fmt"
//
// Chinese Whispers example by Rob Pike
// See http://www.youtube.com/watch?v=f6kdp27TYZs
//
func whisper(left, right chan int) {
left <- 1 + <- right
@quux00
quux00 / chinese-whispers.rs
Created January 16, 2014 04:58
Chinese Whispers in Rust Based on example by Rob Pike in http://www.youtube.com/watch?v=f6kdp27TYZ
static N:int = 100000;
//
// Chinese Whispers in Rust
// Based on example by Rob Pike
// in http://www.youtube.com/watch?v=f6kdp27TYZs
//
fn main() {
let (leftmost_port, leftmost_chan) = Chan::new();
let mut leftc: Chan<int> = leftmost_chan;
@quux00
quux00 / proc.rs
Last active January 3, 2016 13:09
// Trying to learn closures/procs in Rust // Want to capture a variable whose private state I can mutate // Currently failing
//
// Trying to learn closures/procs in Rust
// Want to capture a variable whose private state I can mutate
//
fn main() {
fn call_proc(b: proc(u64) -> u64) {
let x = b(10u64);
println!("{:?}", x);
}