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
| 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
| (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
| // 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
| 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
| 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() { |
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::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); |
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" | |
| // | |
| // Chinese Whispers example by Rob Pike | |
| // See http://www.youtube.com/watch?v=f6kdp27TYZs | |
| // | |
| func whisper(left, right chan int) { | |
| left <- 1 + <- right |
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
| 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; |
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
| // | |
| // 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); | |
| } | |