-
Notifications
You must be signed in to change notification settings - Fork 2
/
pmapf.rkt
35 lines (27 loc) · 959 Bytes
/
pmapf.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#lang racket
;pmap is a parallel concurrent map function.
;its inspired of Clojures pmap.
(require racket/future)
(provide pmapf)
;(provide pmapf-old)
(define (transpose lists) ; collumns to rows!
(apply map list lists))
;(define (pmapf-old func . lists) ; pmapf
; (map touch
; (for/list ([a (transpose lists)])
; (future (lambda () (apply func a)))
; )))
(define (pmapf func . args)
(let* ([joblist (transpose args)] ;Transpose list or lists.
[nofut (if (< (processor-count) (length joblist)) ;Determin amount of futures per iter.
(processor-count) (length joblist))]
[results
(for/list ([sl (in-slice nofut joblist)]) ;Take slice of jobs to process.
(map touch
(for/list ([a sl])
(future (lambda () (apply func a)))
))
)]
[cresults (apply append '() results)]) ;Clean results.
cresults
))