leonoel / missionary

A functional effect and streaming system for Clojure/Script
Eclipse Public License 2.0
630 stars 26 forks source link

Hello flow documentation #18

Closed dpetranek closed 3 years ago

dpetranek commented 3 years ago

The examples in the "Hello flow" documentation aren't exactly matching what I'm getting in my repl:


(def hello-world
  (m/ap
    (println (m/?? (m/enumerate ["Hello" "World" "!"])))
    (m/? (m/sleep 1000))))

(comment
  (m/? (m/aggregate conj hello-world))
  #_> #object[java.lang.ThreadLocal$SuppliedThreadLocal 0xb8473c1 "java.lang.ThreadLocal$SuppliedThreadLocal@b8473c1"]
  ;; should be [nil nil nil]
)

The other examples where you check the result are the same way. This is in a clojure repl.
leonoel commented 3 years ago

I can't reproduce that result. Could you double-check from a fresh repl and provide more info about your environment ?

dpetranek commented 3 years ago

I tried again with a fresh repl and was not able to reproduce the issue above. Here is my exact buffer state, if that's any clue to what happened:

(ns user
  (:require [missionary.core :as m]))

;; task - value = action
;; action termination -> status (success/failure), result
;; if task is pending, it can be cancelled
;; repeats ok

;; sp - sequential process
(def hello-world
  (m/sp (println "hello world")))

(m/? hello-world)
nil

(def nap (m/sleep 1000))

(def slowmo-hello-world
  (m/sp (println "hello")
        (m/? nap)
        (println "world")
        (m/? nap)
        (println "!")))

(m/? slowmo-hello-world)
nil

(def chatty-hello-world
  (m/join vector slowmo-hello-world slowmo-hello-world))

(m/? chatty-hello-world)
[nil nil]

(def unreliable-hello-world
  (m/sp (println "Hello")
        (m/? (m/sleep 500))
        (throw (ex-info "oops" {}))))

(m/? unreliable-hello-world)

(def unreliable-chatty-hello-world
  (m/join vector slowmo-hello-world unreliable-hello-world))

(m/? unreliable-chatty-hello-world)

(def finally-hello-world
  (m/sp
    (try (println "hello")
         (m/? nap)
         (println "world")
         (m/? nap)
         (finally
           (println "!")))))

(m/? (m/join vector unreliable-hello-world finally-hello-world))

;; flow - source/emitter

(def input (m/enumerate (range 10)))

;; aggregate is like reduce
(def sum (m/aggregate + input))

(m/? sum)
45

(m/? (m/aggregate conj (m/transform (partition-all 4) input)))
[[0 1 2 3] [4 5 6 7] [8 9]]

;; ap - ambiguous process
;; ap : flow :: sp : task
(def hello-world-flow
  (m/ap
    (println (m/?? (m/enumerate ["Hello" "World" "!"])))
    (m/? (m/sleep 1000))))

(m/? (m/aggregate conj hello-world-flow))

;; this doesn't return what the docs say it should

(defn debounce [delay flow]
  (m/ap (let [x (m/?! flow)]
          (try (m/? (m/sleep delay x))
               (catch Exception _
                 (m/?? m/none))))))

(defn clock [intervals]
  (m/ap (let [i (m/?? (m/enumerate intervals))]
          (m/? (m/sleep i i)))))

(m/? (->> (clock [24 79 67 34 18 9 99 37])
          (debounce 50)
          (m/aggregate conj)))

;; this doesn't return the right thing - an object instead of a vector