Last active
January 22, 2025 17:16
-
-
Save fogus/61d4689dc221fe136831d5bdc9688d81 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(ns fogus.vthreads | |
(:require [clojure.core.async :as ca])) | |
(set! *warn-on-reflection* true) | |
ca/io-thread-exec | |
(let [c (ca/chan)] | |
(ca/io-thread (ca/>!! c "hello")) | |
(assert (= "hello" (ca/<!! (ca/io-thread (ca/<!! c))))) | |
(ca/close! c)) | |
(def echo-chan (ca/chan)) | |
(ca/io-thread (println (ca/<!! echo-chan))) | |
(ca/>!! echo-chan "task ketchup") | |
(macroexpand '(ca/io-thread (println (ca/<!! echo-chan)))) | |
(def hi-chan (ca/chan)) | |
(doseq [n (range 1000)] | |
(ca/io-thread (ca/>!! hi-chan (str "hi " n)))) | |
(defn hot-dog-machine [] | |
(let [in (ca/chan) | |
out (ca/chan)] | |
(ca/io-thread (ca/<!! in) | |
(ca/>!! out "hot dog")) | |
[in out])) | |
(let [[in out] (hot-dog-machine)] | |
(ca/>!! in "pocket lint") | |
(ca/<!! out)) | |
(let [c1 (ca/chan) | |
c2 (ca/chan) | |
c3 (ca/chan)] | |
(ca/io-thread (ca/>!! c2 (clojure.string/upper-case (ca/<!! c1)))) | |
(ca/io-thread (ca/>!! c3 (clojure.string/reverse (ca/<!! c2)))) | |
(ca/io-thread (println (ca/<!! c3))) | |
(ca/>!! c1 "redrum")) | |
(defn upper-caser | |
[in] | |
(let [out (ca/chan)] | |
(ca/io-thread | |
(while true | |
(ca/>!! out (clojure.string/upper-case (ca/<!! in))))) | |
out)) | |
(defn reverser | |
[in] | |
(let [out (ca/chan)] | |
(ca/io-thread | |
(while true | |
(ca/>!! out (clojure.string/reverse (ca/<!! in))))) | |
out)) | |
(defn printer | |
[in] | |
(ca/io-thread (while true (println (ca/<!! in))))) | |
(def in-chan (ca/chan)) | |
(def upper-caser-out (upper-caser in-chan)) | |
(def reverser-out (reverser upper-caser-out)) | |
(printer reverser-out) | |
(ca/>!! in-chan "redrum") | |
(ca/>!! in-chan "repaid") | |
(ca/>!! in-chan "loop") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment