Skip to content

Instantly share code, notes, and snippets.

@patham9
Last active November 30, 2023 18:17
Show Gist options
  • Save patham9/1258c899c1848a2219671c47d4b356b6 to your computer and use it in GitHub Desktop.
Save patham9/1258c899c1848a2219671c47d4b356b6 to your computer and use it in GitHub Desktop.
Parallel list (Chicken Scheme)
(import (chicken process) (chicken file posix))
(define-syntax parallel-list
(syntax-rules ()
((_ arg1 arg2)
(receive (pipefd0 pipefd1) (create-pipe)
(let ((pid (process-fork)))
(if (eq? pid 0)
(begin (write arg1 (open-output-file* pipefd1))
(exit 0)) ;child is done
(let* ((res1 arg2) ;get to work before waiting for child to return result (else there is no point)
(res2 (read (open-input-file* pipefd0))))
(list res1 res2))))))
((_ arg1 arg2 argi ...)
(let ((chain (parallel-list arg1 (parallel-list arg2 argi ...))))
(append (car chain) (cdr chain))))))
;example:
(define (test x) (list 2 x (+ 2 x)))
(display (parallel-list (test 42) (test 3) (test 1)))
(newline)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment