-
Notifications
You must be signed in to change notification settings - Fork 24
/
core.cljc
1090 lines (891 loc) · 35.9 KB
/
core.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(ns missionary.core
(:refer-clojure :exclude [reduce reductions eduction group-by])
(:require [cloroutine.core :refer [cr] :include-macros true])
(:import (missionary.impl Reduce Reductions GroupBy Relieve Latest Sample Reactor Fiber Sequential Ambiguous
Continuous Watch Observe Buffer Rendezvous Dataflow Mailbox Semaphore RaceJoin Sleep
Never Seed Eduction Zip Propagator #?(:clj Thunk) #?(:clj Pub) #?(:clj Sub))
#?(:clj org.reactivestreams.Publisher))
#?(:cljs (:require-macros [missionary.core :refer [sp ap cp amb amb> amb= ! ? ?> ?< ?? ?! ?= holding reactor]])))
(def
^{:static true
:doc "A `java.util.concurrent.Executor` optimized for blocking evaluation."}
blk #?(:clj Thunk/blk))
(def
^{:static true
:doc "A `java.util.concurrent.Executor` optimized for non-blocking evaluation."}
cpu #?(:clj Thunk/cpu))
(defn
^{:static true
:arglists '([executor thunk])
:doc "
Same as `via`, except the expression to evaluate is provided as a zero-arity function on second argument.
Not supported on clojurescript.
```clojure
(? (via-call blk read-line))
;; reads a line from stdin and returns it
```
"} via-call [e t]
(fn [s f]
#?(:clj (Thunk/run e t s f)
:cljs (throw (js/Error. "Unsupported operation.")))))
(defmacro
^{:arglists '([executor & body])
:style/indent 1
:doc "
Returns a task evaluating body (in an implicit `do`) on given `java.util.concurrent.Executor` and completing with its result.
Cancellation interrupts the evaluating thread.
Not supported on clojurescript.
Example :
```clojure
```
"} via [exec & body] `(via-call ~exec #(do ~@body)))
(defn
^{:static true
:arglists '([duration] [duration value])
:doc "
Returns a task completing with given value (nil if not provided) after given duration (in milliseconds).
Cancelling a sleep task makes it fail immediately.
Example :
```clojure
(? (sleep 1000 42))
#_=> 42 ;; 1 second later
```
"} sleep
([d] (sleep d nil))
([d x] (fn [s f] (Sleep/run d x s f))))
(defn
^{:static true
:arglists '([f & tasks])
:doc "
Returns a task running given `tasks` concurrently.
If every task succeeds, `join` completes with the result of applying `f` to these results.
If any task fails, others are cancelled then `join` fails with this error.
Cancelling propagates to children tasks.
Example :
```clojure
(? (join vector (sleep 1000 1) (sleep 1000 2)))
#_=> [1 2] ;; 1 second later
```
"} join
([c] (fn [s _] (s (c)) #(do)))
([c & ts] (fn [s f] (RaceJoin/run false c ts s f))))
(defn ^{:static true :no-doc true} race-failure [& errors]
(ex-info "Race failure." {::errors errors}))
(defn
^{:static true
:arglists '([& tasks])
:doc "
Returns a task running given `tasks` concurrently.
If any task succeeds, others are cancelled then `race` completes with this result.
If every task fails, `race` fails.
Cancelling propagates to children tasks.
Example :
```clojure
(? (race (sleep 1000 1) (sleep 2000 2)))
#_=> 1 ;; 1 second later
```
"} race
([] (fn [_ f] (f (race-failure)) #(do)))
([& ts] (fn [s f] (RaceJoin/run true race-failure ts s f))))
(defn
^{:static true
:arglists '([task])
:doc "
Returns a task always succeeding with result of given `task` wrapped in a zero-argument function returning result if successful or throwing exception if failed.
"} attempt [task]
(fn [s _] (task (fn [x] (s #(-> x))) (fn [e] (s #(throw e))))))
(defn
^{:static true
:arglists '([task])
:doc "
Returns a task running given `task` completing with a zero-argument function and completing with the result of this function call.
"} absolve [task]
(fn [s f]
(task
(fn [t]
(try (s (t))
(catch #?(:clj Throwable
:cljs :default) e
(f e)))) f)))
(def
^{:static true
:arglists '([task delay] [task delay value])
:doc "
Returns a task running given `task` and completing with its result if available within specified `delay` (in
milliseconds). Otherwise, input is cancelled and the process succeeds with `value`, or `nil` if not provided.
```clojure
(m/? (m/timeout (m/sleep 20 :a) 25 :b)) ;; :a after 20ms
(m/? (m/timeout (m/sleep 20 :a) 15 :b)) ;; :b after 15ms
(m/? (m/timeout (m/sleep 20 :a) 15)) ;; nil after 15ms
```
"} timeout
(fn timeout
([task delay] (timeout task delay nil))
([task delay value]
(-> task
(attempt)
(race (sleep delay #(-> value)))
(absolve)))))
(defn ^:no-doc check []
(Fiber/check (Fiber/current)))
(defn ^:no-doc park [task]
(Fiber/park (Fiber/current) task))
(defn ^:no-doc switch [flow]
(Fiber/swich (Fiber/current) flow))
(defn ^:no-doc fork [par flow]
(assert (pos? par) "Non-positive parallelism.")
(Fiber/fork (Fiber/current) par flow))
(defn ^:no-doc unpark []
(Fiber/unpark (Fiber/current)))
(defn ^:no-doc sp-run [c s f]
(Sequential/run c s f))
(defn ^:no-doc ap-run [c n t]
(Ambiguous/run c n t))
(defmacro ! "
Throws an instance of `missionary.Cancelled` if current evaluation context is interrupted, otherwise returns nil. The
evaluation context defaults to the current thread if the host platform supports it, and can be redefined with `sp`,
`ap`, or `cp`.
" [] `(check))
(defmacro ? "
Parks current evaluation context by given task. Evaluation resumes when the task completes, result is returned or
rethrown according to completion status. Interrupting the evaluation context cancels the parking task. The evaluation
context defaults to the current thread if the host platform supports it, and can be redefined with `sp` or `ap`.
" [task] `(park ~task))
(defmacro ?> "
Forks current evaluation context by given flow. Evaluation resumes whenever the flow transfers, result is returned or
rethrown according to transfer status. Each transfer creates a new processing branch and defines a new evaluation
context inherited from its parent. `par` is an optional positive number, defaulting to 1, defining the maximal count
of processing branches allowed to be run concurrently. Interrupting the parent evaluation context cancels the forking
flow and interrupts all processing branches currently being run and those subsequently run. The evaluation context is
undefined by default and can be defined with `ap`.
Example :
```clojure
(require '[missionary.core :as m])
(m/? (m/reduce conj (m/ap (inc (m/?> (m/seed [1 2 3]))))))
#_=> [2 3 4]
```
Example :
```clojure
(require '[missionary.core :as m])
(m/? (->> (m/ap
(let [x (m/?> 5 (m/seed [19 57 28 6 87]))]
(m/? (m/sleep x x))))
(m/reduce conj)))
#_=> [6 19 28 57 87] ;; in 87 ms
```
" ([flow] `(fork 1 ~flow))
([par flow] `(fork ~par ~flow)))
(defmacro ^{:deprecated true
:doc "Alias for `?>`"}
?? [f] `(?> ~f))
(defmacro ?< "
Forks current evaluation context by given flow. Evaluation resumes whenever the flow transfers, result is returned or
rethrown according to transfer status. Each transfer creates a new processing branch and defines a new evaluation
context inherited from its parent. Concurrent processing branches are not allowed, and the current processing branch is
interrupted when the forking flow becomes ready to transfer again. Interrupting the parent evaluation context cancels
the forking flow and interrupts all processing branches currently being run and those subsequently run. The evaluation
context is undefined by default and can be defined with `ap` or `cp`.
Example :
```clojure
(require '[missionary.core :as m])
(import missionary.Cancelled)
(defn debounce [delay flow]
(m/ap (let [x (m/?< flow)]
(try (m/? (m/sleep delay x))
(catch Cancelled _ (m/amb))))))
(m/? (->> (m/ap (let [n (m/amb 24 79 67 34 18 9 99 37)]
(m/? (m/sleep n n))))
(debounce 50)
(m/reduce conj)))
#_=> [24 79 9 37]
```
" [flow] `(switch ~flow))
(defmacro ^{:deprecated true
:doc "Alias for `?<`"}
?! [f] `(?< ~f))
(defmacro
^{:arglists '([flow])
:deprecated true
:doc "Alias for `(?> ##Inf flow)`"}
?= [flow] `(?> ##Inf ~flow))
(defmacro
^{:arglists '([& body])
:style/indent 0
:doc "
Returns a task evaluating `body` (in an implicit `do`) in a new evaluation context and completing its result. Body
evaluation can be parked by a task with `?`. Cancelling a `sp` process interrupts its evaluation context.
"} sp [& body]
`(partial
(cr {park unpark}
~@body) sp-run))
(defn ^:no-doc cp* [cr] (Continuous/flow cr))
(defmacro
^{:arglists '([& body])
:style/indent 0
:doc "
Returns a continuous flow evaluating `body` (in an implicit `do`) in a new evaluation context and producing values of
each subsequent fork. Body evaluation can be forked by a continuous flow with `?<`. Evaluation and transfers are lazy,
driven by downstream sampling. Cancelling an `cp` process interrupts its root evaluation context.
"} cp [& body]
`(cp*
(cr {switch unpark}
~@body)))
(defmacro
^{:arglists '([& body])
:style/indent 0
:doc "
Returns a discrete flow evaluating `body` (in an implicit `do`) in a new evaluation context and producing values of each
subsequent fork. Body evaluation can be parked by a task with `?` and forked by a flow with `?>` and `?<`. Evaluation
and transfers are eager, backpressured by downstream transfers. Cancelling an `ap` process interrupts its root
evaluation context.
"} ap [& body]
`(partial
(cr {park unpark
fork unpark
switch unpark}
~@body) ap-run))
(defn
^{:static true
:arglists '([task])
:doc "
Inhibits cancellation signal of given `task`.
"} compel [task]
(fn [s f] (task s f) #(do)))
(defn
^{:static true
:arglists '([])
:doc "
Creates an instance of dataflow variable (aka single-assignment).
A dataflow variable is a function implementing `assign` on 1-arity and `deref` on 2-arity (as task). `assign` immediately binds the variable to given value if not already bound and returns bound value. `deref` is a task completing with the value bound to the variable as soon as it's available.
Cancelling a `deref` task makes it fail immediately.
```
"} dfv [] (Dataflow/make))
(defn
^{:static true
:arglists '([])
:doc "
Creates an instance of mailbox.
A mailbox is a function implementing `post` on 1-arity and `fetch` on 2-arity (as task). `post` immediately pushes given value to mailbox and returns nil. `fetch` is a task pulling a value from mailbox as soon as it's non-empty and completing with this value.
Cancelling a `fetch` task makes it fail immediately.
Example : an actor is a mailbox associated with a process consuming messages.
```clojure
(defn crash [^Throwable e] ;; let it crash philosophy
(.printStackTrace e)
(System/exit -1))
(defn actor
([init] (actor init crash))
([init fail]
(let [self (mbx)]
((sp
(loop [b init]
(recur (b self (? self)))))
nil fail)
self)))
(def counter
(actor
((fn beh [n]
(fn [self cust]
(cust n)
(beh (inc n)))) 0)))
(counter prn) ;; prints 0
(counter prn) ;; prints 1
(counter prn) ;; prints 2
```
"} mbx [] (Mailbox/make))
(defn
^{:static true
:arglists '([])
:doc "
Creates an instance of synchronous rendez-vous.
A synchronous rendez-vous is a function implementing `give` on its 1-arity and `take` on its 2-arity (as task). `give` takes a value to be transferred and returns a task completing with nil as soon as a taker is available. `take` is a task completing with transferred value as soon as a giver is available.
Cancelling `give` and `take` tasks makes them fail immediately.
Example : producer / consumer stream communication
```clojure
(defn reducer [rf i take]
(sp
(loop [r i]
(let [x (? take)]
(if (identical? x take)
r (recur (rf r x)))))))
(defn iterator [give xs]
(sp
(loop [xs (seq xs)]
(if-some [[x & xs] xs]
(do (? (give x))
(recur xs))
(? (give give))))))
(def stream (rdv))
(? (join {} (iterator stream (range 100)) (reducer + 0 stream))) ;; returns 4950
```
"} rdv [] (Rendezvous/make))
(defn
^{:static true
:arglists '([] [n])
:doc "
Creates a semaphore initialized with n tokens (1 if not provided, aka mutex).
A semaphore is a function implementing `release` on 0-arity and `acquire` on 2-arity (as task). `release` immediately makes a token available and returns nil. `acquire` is a task completing with nil as soon as a token is available.
Cancelling an `acquire` task makes it fail immediately.
Example : dining philosophers
```clojure
(defn phil [name f1 f2]
(sp
(while true
(prn name :thinking)
(? (sleep 500))
(holding f1
(holding f2
(prn name :eating)
(? (sleep 600)))))))
(def forks (vec (repeatedly 5 sem)))
(? (timeout 10000
(join vector
(phil \"descartes\" (forks 0) (forks 1))
(phil \"hume\" (forks 1) (forks 2))
(phil \"plato\" (forks 2) (forks 3))
(phil \"nietzsche\" (forks 3) (forks 4))
(phil \"kant\" (forks 0) (forks 4)))))
```
"} sem
([] (sem 1))
([n] (Semaphore/make n)))
(defmacro
^{:arglists '([semaphore & body])
:style/indent 1
:doc "
`acquire`s given `semaphore` and evaluates `body` (in an implicit `do`), ensuring `semaphore` is `release`d after evaluation.
"} holding [lock & body]
`(let [l# ~lock] (? l#) (try ~@body (finally (l#)))))
(def never
^{:static true
:doc "
A task never succeeding. Cancelling makes it fail immediately."}
(fn [_ f] (Never/run f)))
(def
^{:static true
:doc "
The empty flow. Doesn't produce any value and terminates immediately. Cancelling has no effect.
Example :
```clojure
(? (reduce conj none))
#_=> []
```
"} none (fn [_ t] (t) #(do)))
(defn
^{:static true
:arglists '([collection])
:doc "
Returns a discrete flow producing values from given `collection`. Cancelling before having reached the end makes the flow fail immediately.
"} seed [coll]
(fn [n t] (Seed/run coll n t)))
(def ^{:deprecated true
:doc "Alias for `seed`"}
enumerate seed)
(defmacro
^{:arglists '([& forms])
:doc "In an `ap` block, evaluates each form sequentially and returns successive results."}
amb
([] `(?> none))
([form] form)
([form & forms]
(let [n (inc (count forms))]
`(case (?> (seed (range ~n)))
~@(interleave (range) (cons form forms))))))
(defmacro
^{:arglists '([& forms])
:doc "In an `ap` block, evaluates each form concurrently and returns results in order of availability."}
amb=
([] `(?> none))
([form] form)
([form & forms]
(let [n (inc (count forms))]
`(case (?> ~n (seed (range ~n)))
~@(interleave (range) (cons form forms))))))
(defmacro
^{:deprecated true
:arglists '([& forms])
:doc "Alias for `amb`"}
amb> [& forms] (cons `amb forms))
(defn
^{:static true
:arglists '([rf flow] [rf init flow])
:doc "
Returns a task reducing values produced by given discrete `flow` with `rf`, starting with `init` (or, if not provided, the result of calling `rf` with no argument).
Cancelling propagates to upstream flow. Early termination by `rf` (via `reduced` or throwing) cancels upstream flow.
Example :
```clojure
(? (reduce + (seed (range 10))))
#_=> 45
```
"} reduce
([rf flow] (fn [s f] (Reduce/run rf flow s f)))
([rf i flow] (reduce (fn ([] i) ([r x] (rf r x))) flow)))
(def ^{:deprecated true
:doc "Alias for `reduce`"}
aggregate reduce)
(defn
^{:static true
:arglists '([reference])
:doc "
Returns a continuous flow reflecting the current state of a reference type. `reference` must support `add-watch`,
`remove-watch` and `deref`. On initialization, the process is ready to transfer. On transfer, the current state is
returned. Whenever the state of the reference changes and a transfer is not pending, the process becomes ready to
transfer again. Cancelling the process makes it fail immediately with an instance of `missionary.Cancelled` and
terminates the process.
"} watch [r] (fn [n t] (Watch/run r n t)))
(defn
^{:static true
:arglists '([subject])
:doc "
Returns a discrete flow observing values produced by a non-backpressured subject. `subject` must be a function taking a
callback and returning a cleanup thunk. On initialization, the process calls the subject with a fresh callback. Passing
a value to the callback makes the process ready to transfer this value. Cancelling the process makes it fail immediately
with an instance of `missionary.Cancelled` and terminates the process. The cleanup thunk is called on termination. The
callback throws an `Error` if the process is cancelled or terminated, or if a transfer is pending.
"} observe [s] (fn [n t] (Observe/run s n t)))
(def
^{:static true
:arglists '([xf* flow])
:doc "
Returns a discrete flow running given discrete `flow` and transforming values with the composition of given transducers `xf*`.
Cancelling propagates to upstream flow. Early termination by the transducing stage (via `reduced` or throwing) cancels upstream flow.
Example :
```clojure
(? (->> (seed (range 10))
(eduction (filter odd?) (mapcat range) (partition-all 4))
(reduce conj)))
#_=> [[0 0 1 2] [0 1 2 3] [4 0 1 2] [3 4 5 6] [0 1 2 3] [4 5 6 7] [8]]
```
"} eduction
(fn e
([f] f)
([x f] (fn [n t] (Eduction/run x f n t)))
([x y & zs] (apply e (comp x y) zs))))
(def ^{:deprecated true
:doc "Alias for `eduction`"}
transform eduction)
(defn
^{:static true
:arglists '([rf flow] [rf init flow])
:doc "
Returns a discrete flow running given discrete `flow` and emitting given `init` value (or, if not provided, the result of calling `rf` with no argument) followed by successive reductions (by rf) of upstream values with previously emitted value.
Cancelling propagates to upstream flow. Early termination by `rf` (via `reduced` or throwing) cancels upstream flow.
Example :
```clojure
(? (->> [1 2 3 4 5]
(seed)
(reductions +)
(reduce conj)))
#_=> [0 1 3 6 10 15]
```
"} reductions
([rf f] (fn [n t] (Reductions/run rf f n t)))
([rf i f] (reductions (fn ([] i) ([r x] (rf r x))) f)))
(def ^{:deprecated true
:doc "Alias for `reductions`"}
integrate reductions)
(defn
^{:static true
:arglists '([flow])
:doc "
Returns a `org.reactivestreams.Publisher` running given discrete `flow` on each subscription.
"} publisher [f]
#?(:clj (reify Publisher (subscribe [_ s] (Pub/run f s)))
:cljs (throw (js/Error. "Unsupported operation."))))
(defn
^{:static true
:arglists '([pub])
:doc "
Returns a discrete flow subscribing to given `org.reactivestreams.Publisher`.
"} subscribe [pub]
#?(:clj (fn [n t] (Sub/run pub n t))
:cljs (throw (js/Error. "Unsupported operation."))))
(def
^{:static true
:arglists '([flow] [sg flow])
:doc "
Returns a flow consuming input `flow` as fast as possible and producing aggregates of successive values according to
downstream transfer rate. The set of transferred values must form a semigroup with given function `sg` as the internal
binary operation, i.e. `sg` must be associative. If `sg` is not provided, `{}` is used by default, i.e. all values but
the latest are discarded.
Cancelling propagates to upstream. If `sg` throws, upstream `flow` is cancelled.
Example :
```clojure
;; Delays each `input` value by `delay` milliseconds
(defn delay-each [delay input]
(ap (? (sleep delay (?> input)))))
(? (->> (ap (let [n (?> (seed [24 79 67 34 18 9 99 37]))]
(? (sleep n n))))
(relieve +)
(delay-each 80)
(reduce conj)))
#_=> [24 79 67 61 99 37]
```
"} relieve
(fn
([f] (fn [n t] (Relieve/run {} f n t)))
([sg f] (fn [n t] (Relieve/run sg f n t)))))
(defn
^{:static true
:arglists '([capacity flow])
:doc "
Returns a discrete flow producing values emitted by given discrete `flow`, accumulating upstream overflow up to `capacity` items.
"} buffer [c f]
(assert (pos? c) "Non-positive buffer capacity.")
(Buffer/flow c f))
(def
^{:static true
:arglists '([f & flows])
:doc "
Returns a flow running an arbitrary number of flows concurrently. The process is ready to transfer when at least one
input is ready to transfer. On transfer, all ready inputs are transferred, the function is called with the latest
value of each input and the result is returned. If an input emits consecutive values, all of them are transferred and
only the latest one is retained. Each input must be initially ready.
Input failures and exceptions thrown by `f` cancel the process and propagate the error. The process terminates when all
inputs are terminated. Cancelling the process cancels all inputs.
```clojure
(defn sleep-emit [delays]
(reductions {} 0
(ap (let [n (?> (seed delays))]
(? (sleep n n))))))
(defn delay-each [delay input]
(ap (? (sleep delay (?> input)))))
(? (->> (latest vector
(sleep-emit [24 79 67 34])
(sleep-emit [86 12 37 93]))
(delay-each 50)
(reduce conj)))
#_=> [[0 0] [24 0] [24 86] [79 12] [79 37] [67 37] [34 93]]
```
"} latest (fn [c & fs] (fn [n t] (Latest/run c fs n t))))
(def
^{:static true
:arglists '([f sampled* sampler])
:doc "
Returns a flow running an arbitrary number of sampled flows concurrently with a sampler flow. The process is ready to
transfer when the sampler is ready to transfer. On transfer, all ready inputs are transferred, the function is called
with the latest value of each input and the result is returned. If a sampled input emits consecutive values, all of
them are transferred and only the latest one is retained. Each sampled input must be initially ready.
When the sampler input terminates, all sampled inputs are cancelled. Input failures and exceptions thrown by `f` cancel
the process, and propagate the error. The process terminates when all input flows are terminated. Cancelling the process
cancels the sampler input.
Example :
```clojure
(defn sleep-emit [delays]
(ap (let [n (?> (seed delays))]
(? (sleep n n)))))
(defn delay-each [delay input]
(ap (? (sleep delay (?> input)))))
(m/? (->> (m/sample vector
(m/reductions {} 0 (sleep-emit [24 79 67 34]))
(sleep-emit [86 12 37 93]))
(delay-each 50)
(m/reduce conj)))
#_=> [[24 86] [24 12] [79 37] [67 93]]
```
"} sample (fn [c f & fs] (fn [n t] (Sample/run c f fs n t))))
(defn
^{:static true
:arglists '([f & flows])
:doc "
Returns a discrete flow running given discrete `flows` concurrently and emitting the result of applying `f` to the set of first values emitted by each upstream flow, followed by the result of applying `f` to the set of second values and so on, until any upstream flow terminates, at which point the flow will cancel all other upstream flows and wait for their termination.
Cancelling propagates to every upstream flow. If any upstream flow fails or if `f` throws, the flow is cancelled.
Example :
```clojure
(m/? (->> (m/zip vector
(m/seed [1 2 3])
(m/seed [:a :b :c]))
(m/reduce conj)))
#_=> [[1 :a] [2 :b] [3 :c]]
```
"} zip [c f & fs] (fn [n t] (Zip/run c (cons f fs) n t)))
(defn
^{:static true
:arglists '([kf >f])
:doc "
Returns a discrete flow running given discrete flow, calling given key function on each produced value, grouping values
according to keys returned by the function, and producing a key-group pair for each grouping found. A group is a flow
consuming values matching a key. Upstream values are dispatched in constant time to their group consumer.
Cancelling a group consumer makes it fail immediately. If a value is subsequently found for the same grouping, the
key-group pair is produced again, including in the special case where the consumer is cancelled while a transfer was
pending. Cancelling a group consumer has no effect when the main process is cancelled.
If upstream fails, or if the key function throws, then upstream is cancelled and flushed and the error is propagated
downstream.
When the last upstream value is consumed, downstream terminates along with each active consumer and subsequent ones.
Concurrent consumers on a single group are not allowed, attempting to do so will fail the latest consumer.
Example :
```clojure
(def words [\"Air\" \"Bud\" \"Cup\" \"Awake\" \"Break\" \"Chunk\" \"Ant\" \"Big\" \"Check\"])
(def groups
(m/ap (let [[k >x] (m/?= (m/group-by (juxt first count) (m/seed words)))]
[k (m/? (m/reduce conj >x))])))
(m/? (m/reduce conj {} groups))
#_=> {[\\C 3] [\"Cup\"],
[\\B 5] [\"Break\"],
[\\A 5] [\"Awake\"],
[\\B 3] [\"Bud\" \"Big\"],
[\\A 3] [\"Air\" \"Ant\"],
[\\C 5] [\"Chunk\" \"Check\"]}
```
"} group-by [kf f] (fn [n t] (GroupBy/run kf f n t)))
(def
^{:static true
:arglists '([boot])
:doc "Use lazy publishers instead - memo, stream, signal."}
reactor-call (fn [i] (fn [s f] (Reactor/run i s f))))
(defmacro
^{:arglists '([& body])
:doc "Use lazy publishers instead - memo, stream, signal."}
reactor [& body] `(reactor-call (fn [] ~@body)))
(def
^{:static true
:arglists '([flow])
:doc "Use stream instead."}
stream! (fn [f] (Reactor/publish f false)))
(def
^{:static true
:arglists '([flow])
:doc "Use signal instead."}
signal! (fn [f] (Reactor/publish f true)))
(def
^{:static true
:arglists '([t])
:doc "
Returns a new publisher memoizing the result of task `t`.
As long as the task process did not terminate spontaneously, running the publisher as a task registers a subscription.
Cancelling a subscription deregisters it. A new task process is spawned when the first subscription is registered and
cancelled when the last subscription is deregistered. After the task process terminated spontaneously, every registered
subscription and any subsequent subscription terminates immediately with the process result.
Example :
```clojure
(require '[missionary.core :as m])
(def fib42
(m/memo
(m/via m/cpu
(println \"Computing 42th fibonacci...\")
((fn fib [n]
(case n
0 0
1 1
(+ (fib (dec n))
(fib (dec (dec n))))))
42))))
(fib42 prn prn) ;; expensive computation starts here, result is eventually printed
(fib42 prn prn) ;; expensive computation doesn't run again, previous result is reused
```
"} memo
(letfn [(run [])
(sub []
(when-some [f (Propagator/getp)] (f)))
(success [x]
(Propagator/resolve)
(Propagator/setp #(Propagator/success x))
(Propagator/schedule))
(failure [x]
(Propagator/resolve)
(Propagator/setp #(Propagator/failure x))
(Propagator/schedule))
(tick []
(Propagator/waiting (Propagator/getp)))]
(fn [task] (Propagator/task nil nil run sub success failure tick task))))
(def
^{:static true
:arglists '([f])
:doc "
Returns a new publisher distributing successive items emitted by flow `f` while collecting subscribers' backpressure.
As long as the flow process did not terminate spontaneously, running the publisher as a flow registers a subscription.
Cancelling a subscription deregisters it. A new flow process is spawned when the first subscription is registered and
cancelled when the last subscription is deregistered. After the flow process has terminated spontaneously, every
registered subscription and any subsequent subscription terminates immediately after consuming the current item, if any.
Example :
```clojure
(require '[missionary.core :as m])
(def >clock ;; A shared process emitting `nil` every second.
(m/stream
(m/ap
(loop [i 0]
(m/amb
(m/? (m/sleep 1000))
(recur (inc i)))))))
(defn counter [r _] (inc r)) ;; A reducing function counting the number of items.
((m/join vector
(m/reduce counter 0 (m/eduction (take 3) >clock))
(m/reduce counter 0 (m/eduction (take 4) >clock)))
prn prn) ;; After 4 seconds, prints [3 4]
```
"} stream
(let [slot-ready 0
slot-failed 1
slot-done 2
slot-value 3
slot-pending 4
slot-thread 5
slot-time 6
slots 7]
(letfn [(thread []
#?(:clj (Thread/currentThread) :cljs nil))
(propagate-step []
(let [^objects state (Propagator/getp)]
(aset state slot-pending
(inc (aget state slot-pending)))
(Propagator/step)))
(propagate-done []
(Propagator/done))
(emit [^objects state]
(aset state slot-value state)
(aset state slot-ready false)
(aset state slot-thread (thread))
(aset state slot-time (Propagator/time))
(Propagator/waiting propagate-step))
(ack [^objects state]
(when (zero? (aset state slot-pending
(dec (aget state slot-pending))))
(when (aget state slot-ready) (emit state))))
(run []
(let [state (object-array slots)]
(aset state slot-ready false)
(aset state slot-failed false)
(aset state slot-done false)
(aset state slot-pending 0)
(aset state slot-time -1)
(Propagator/setp state)))
(sub []
(let [^objects state (Propagator/getp)]
(when (identical? (thread) (aget state slot-thread))
(when (identical? (Propagator/time) (aget state slot-time))
(aset state slot-pending
(inc (aget state slot-pending)))
(Propagator/step)))))
(step []
(Propagator/schedule))
(done []
(Propagator/resolve)
(let [^objects state (Propagator/getp)]
(aset state slot-done true)
(Propagator/waiting propagate-done)))
(tick []
(let [^objects state (Propagator/getp)]
(aset state slot-ready true)
(when (zero? (aget state slot-pending))
(emit state))))
(accept []
(let [^objects state (Propagator/getp)
x (aget state slot-value)]
(if (identical? x state)
(try (let [x (Propagator/transfer)]
(aset state slot-value x)
(ack state) x)
(catch #?(:clj Throwable :cljs :default) e
(aset state slot-failed true)
(aset state slot-value e)
(ack state) (throw e)))
(do (when (aget state slot-done)
(Propagator/done))
(ack state)
(if (aget state slot-failed)
(throw x) x)))))
(reject []
(ack (Propagator/getp)))]
(fn [flow] (Propagator/flow nil nil run sub step done tick accept reject flow)))))
(def
^{:static true
:arglists '([flow] [sg flow])
:doc "
Returns a new publisher exposing successive values of `flow` regardless of subscribers' sampling rate. The set of
transferred values must form a semigroup with given function `sg` as the internal binary operation, i.e. `sg` must be
associative. If `sg` is not provided, `{}` is used by default, i.e. all values but the latest are discarded.
As long as the flow process did not terminate spontaneously, running the publisher as a flow registers a subscription.
Cancelling a subscription deregisters it. A new flow process is spawned when the first subscription is registered and
cancelled when the last subscription is deregistered. After the flow process has terminated spontaneously, every
registered subscription and any subsequent subscription terminates immediately after consuming the latest item.
Example :
```clojure
(require '[missionary.core :as m])
(def !input (atom 1))