lab 42 - channels in shell

NAME

lab 42 - channels in shell

NOTES

This is a quicky. I ripped this code off from Rob Pike's squint, an interpreter for his squeak language, a predecessor of limbo. It's an implementation of Eratosthenes's Sieve using communicating processes and channels, which I translated to Inferno shell. This was just to try out the channels in shell, since somehow I've overlooked them till now.

load tk
load expr

fn counter {
 ch := $1
 {i:=2; while {} {send $ch $i; i=${expr $i 1 +}} } 
}

fn filter {
 prime := $1
 listen := $2
 s := $3
 while {} {
  i := ${recv $listen}
  if {ntest ${expr $i $prime %}} { send $s $i}
 } 
}

fn sieve {
 prime := $1
 chan count
 c := count
 counter count &
 {while {} {
  p := ${recv $c}
  send prime $p
  chan newc^$p
  filter $p  $c  newc^$p &
  c = newc^$p
 }} &
}

chan prime
sieve prime &
echo ${recv prime}
echo ${recv prime}
echo ${recv prime}

Watch your CPU meter spike when you get a hundred primes or so.

Comments

Popular posts from this blog

lab 110 - inferno archive edition

lab 107 - midiplay

The Cartesian Theater of AI