Skip to content

Instantly share code, notes, and snippets.

@ygrenzinger
Last active April 18, 2018 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ygrenzinger/9003bdedff761be9a571414d91bc8b56 to your computer and use it in GitHub Desktop.
Save ygrenzinger/9003bdedff761be9a571414d91bc8b56 to your computer and use it in GitHub Desktop.
NCraft code
%http://rigaux.org/language-study/syntax-across-languages-per-language/Oz.html
% https://mozart.github.io/mozart-v1/doc-1.4.0/tutorial/index.html
% Function which produce number with closure and
declare ProduceNumbersUntil
fun {ProduceNumbersUntil N}
local Produce in
fun {Produce L}
{Delay 500}
if L == N then N|nil else L|{Produce L+1} end
end
{Produce 2}
end
end
% Higher order programming for fun
declare Consumer
proc {Consumer F S}
case S of
nil then skip
[] H|T then
{F H} {Consumer F T}
end
end
% Small recursive function
% (no state like a real Functionalist)
fun {IsPrime N Primes}
case Primes of
nil then true
[] H|T then
if (N mod H) == 0 then false
else {IsPrime N T}
end
end
end
% declare an Agent (or Actor) for storing and getting primes
% it will act like a "database"
declare PrimeDatabase
fun {PrimeDatabase}
Port FunctionsStream PrimesStore Functions
in
{Browse ['Stream of functions : ' FunctionsStream]}
% Create a 'Cell' where the inside assignment can change
PrimesStore={NewCell nil}
/*
Create of a 'Port'
an asynchronous channel that supports many-to-one communication
the client can 'Send' thing to the 'Port'
*/
Port={NewPort FunctionsStream}
proc {Functions F}
case F
of get(R) then
R = @PrimesStore
[] add(P) then
PrimesStore := P|@PrimesStore
end
end
% A port associated to a thread is an Agent
% going through all message sent to the agent following the stream
thread {ForAll FunctionsStream Functions} end
Port
end
% Where the magic happens
% recursivily checking if number is prime
% by using our primes database
fun {CheckAndAddPrime Numbers PrimesStore}
case Numbers of
nil then nil
[] H|T then
local Primes in
{Send PrimesStore get(Primes)}
if {IsPrime H Primes} then
{Send PrimesStore add(H)}
H|{CheckAndAddPrime T PrimesStore}
else
{CheckAndAddPrime T PrimesStore}
end
end
end
end
% plug everything together using mostly asynchronous thread
local Numbers Primes PrimesStore Displayer in
PrimesStore={PrimeDatabase}
{Browse ['Producing numbers : ' Numbers]}
{Browse ['List of primes : ' Primes]}
{Delay 2000}
thread Numbers={ProduceNumbersUntil 20} end
thread Primes={CheckAndAddPrime Numbers PrimesStore} end
% Function as a primitive
Displayer = proc {$ N} {Browse ['Found prime ' N]} end
thread {Consumer Displayer Primes} end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment