Skip to content

Instantly share code, notes, and snippets.

@dannypsnl
Created September 15, 2022 10:20
Show Gist options
  • Save dannypsnl/c055ba336872f0e31c08668be2dc2be8 to your computer and use it in GitHub Desktop.
Save dannypsnl/c055ba336872f0e31c08668be2dc2be8 to your computer and use it in GitHub Desktop.
use future in weird way
#lang racket/base
(require racket/future
racket/async-channel
racket/function
racket/match)
(define process-map (make-hasheq))
(struct pid ())
(struct process (id fu ch))
(define (process-send p msg)
(define np (hash-ref process-map (process-id p)
p))
(async-channel-put (process-ch np) msg)
(hash-set! process-map (process-id p) (touch (process-fu np))))
(define (^friend my-name
[id (pid)]
[ch (make-async-channel)])
(process id
(future
(thunk (match (async-channel-get ch)
[(list 'ping from)
(printf "ping from ~a~n" my-name)
(process-send from 'pong)
(^friend my-name id ch)]
['pong
(printf "pong from ~a~n" my-name)
(^friend my-name id ch)])))
ch))
(define bob (^friend "Bob"))
(define jack (^friend "Jack"))
(process-send bob (list 'ping jack))
(process-send bob (list 'ping jack))
(process-send bob (list 'ping jack))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment