+(define (concurrent-filter-map num_workers f xs)
+ ; TODO switch from mailboxes to channels
+ (define (make-worker id f)
+ (define parent (current-thread))
+ (λ ()
+ (define self (current-thread))
+ (define (work)
+ (thread-send parent (cons 'next self))
+ (match (thread-receive)
+ ['done (thread-send parent (cons 'exit id))]
+ [(cons 'unit x) (begin
+ (define y (f x))
+ (when y (thread-send parent (cons 'result y)))
+ (work))]))
+ (work)))
+ (define (dispatch ws xs ys)
+ (if (empty? ws)
+ ys
+ (match (thread-receive)
+ [(cons 'exit w) (dispatch (remove w ws =) xs ys)]
+ [(cons 'result y) (dispatch ws xs (cons y ys))]
+ [(cons 'next thd) (match xs
+ ['() (begin
+ (thread-send thd 'done)
+ (dispatch ws xs ys))]
+ [(cons x xs) (begin
+ (thread-send thd (cons 'unit x))
+ (dispatch ws xs ys))])])))
+ (define workers
+ (range 1 (add1 num_workers)))
+ (define threads
+ (map (λ (id) (thread (make-worker id f))) workers))
+ (define results
+ (dispatch workers xs '()))
+ (for-each thread-wait threads)
+ results)
+
+(define (msg-print out-format odd msg)
+ (printf
+ (match out-format
+ ['single-line "~a \033[1;37m<~a ~a>\033[0m \033[0;~am~a\033[0m~n"]
+ ['multi-line "~a~n\033[1;37m<~a ~a>\033[0m~n\033[0;~am~a\033[0m~n~n"]
+ [_ (raise (format "Invalid output format: ~a" out-format))])
+ (date->string (seconds->date [msg-ts_epoch msg]) #t)
+ (msg-nick msg)
+ (msg-uri msg)
+ (if odd 36 33)
+ (msg-text msg)))
+
+(define re-msg-begin
+ ; TODO Zulu offset. Maybe in several formats. Which ones?
+ (pregexp "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}"))