Fork me on GitHub
#missionary
<
2023-12-25
>
joshcho10:12:54

Given a string s, I want to convert s to a flow that emits each character of s sequentially every 100ms. Here is my attempt at it:

(let [s    "dummy-string"
      !val (atom "")]
  ((fn [i]
     (when (< i (count msg))
       (reset! !val (subs s i))))
   (- (int (/ e/system-time-ms 100))
      (int (/ (e/snapshot e/system-time-ms) 100)))))
This works when you load the page, but if I place this in an e/fn, the snippet doesn't work. It makes sense as e/system-time-ms won't be referenced multiple times. How best to resolve this?

Dustin Getz16:12:26

my idea is to use (m/seed “dummy”) to create a discrete flow of chars, and then consume it slowly, perhaps by creating another discrete flow of ticks and then figuring out how to alternate them

Dustin Getz16:12:56

broadly, i think discrete things are best modeled in missionary not electric

leonoel20:12:02

(defn delayed-seed [d xs]
  (m/ap (m/? (m/sleep d (m/?> (m/seed xs))))))

((->> "hello"
   (delayed-seed 1000)
   (m/reduce (fn [_ x] (prn x)) nil))
 #(prn :success %) #(prn :failure %))

❤️ 2
Dustin Getz20:12:52

m/sleep solutions all drift don’t they?

leonoel20:12:29

this one yes, it's a fixed delay. But you could build a fixed rate with m/sleep too

leonoel20:12:56

(defn fixed-rate-seed [d xs]
  (m/ap (let [[x t] (m/?> (m/seed (map vector xs (iterate (partial + d) (System/currentTimeMillis)))))]
          (m/? (m/sleep (- t (System/currentTimeMillis)) x)))))

👀 2
👌 1
joshcho23:12:44

wow thank you!

joshcho23:12:02

what is the difference between fixed-rate-seed and delayed seed? i don't think i understand "drift"

joshcho23:12:50

oh is it because the later values depend on the sleep of the previous values?

joshcho23:12:07

gpt4 knows about missionary! i wonder how correct this is

Dustin Getz01:12:21

in java, (thread/sleep N) will sleep at least N seconds, generally a couple ms over, so if you repeat these sleeps you’ll drift away from a true clock

joshcho01:12:59

how about in javascript? i assume not so

joshcho01:12:25

also these differences probably don’t matter too much for ux, so we are good

joshcho01:12:31

just interesting

Dustin Getz01:12:42

in js chain of setTimeout will drift, setInterval should not

❤️ 1
joshcho01:12:04

and drift by couple ms, right?

joshcho01:12:10

nothing too crazy

Dustin Getz01:12:21

a couple ms per tick

joshcho01:12:28

right right

joshcho01:12:36

i wanted to add noise anyway lol

Dustin Getz01:12:54

the corrected ticker, which leo shows, uses platform sleep (which is blurry) but then measures how long it actually took, thus adjusting for the variance

❤️ 1
joshcho01:12:06

very elegant

joshcho10:12:53

Here is the full e/fn snippet for context. api is https://github.com/wkok/openai-clojure