Skip to content

Instantly share code, notes, and snippets.

@YorickPeterse YorickPeterse/main.scm
Last active Mar 23, 2017

What would you like to do?
(import bootstrap)
(import std integer)
(import std array)
(import std stdout)
(import std process)
; Defines the "main" module, #f indicating it has no explicit receiver. This means it will be defined
; in the default location.
(let main (defmod bootstrap "main" #f))
(let max 1000)
(let num 0)
(let current_pid (pid process))
(while (< integer num max)
(let pid (spawn process
; Processes are completely isolated and thus don't
; inherit any variables. This means we need to re-import
; our modules.
(import std array)
(import std process)
(import std integer)
; Wait for a message in the format of [pid, number]
(let message (receive process))
(let pid (at array message 0))
(let num (at array message 1))
; Multiply the number by 2
(let resp (* integer num 2))
; Send the number back to the sender
(send process pid resp))))
; Send the number to the "worker"
(send process pid (array current_pid num))
(let num (+ integer num 1)))
(let num 0)
(let total 0)
; Collect the output of every process.
(while (< integer num max)
(let got (receive process))
(let total (+ integer total got))
(let num (+ integer num 1)))
(print stdout (to_string integer total))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.