Last active
November 30, 2023 18:17
-
-
Save patham9/1258c899c1848a2219671c47d4b356b6 to your computer and use it in GitHub Desktop.
Parallel list (Chicken Scheme)
This file contains 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
(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