-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph.clj
37 lines (33 loc) · 1.44 KB
/
graph.clj
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
(ns flc-x.graph
"This namespace will be moved to flc-x/graph, or be removed altogether."
(:refer-clojure :exclude [compile])
(:require [flc.program :as program]
[flc.component :as component]
[flc.let-like :as let-like]
[flc.map-like :as m]
[clojure.set :as set]))
(defmacro fnk' [args expr]
`[(fn ~args ~expr) ~(mapv keyword args)])
(defn constants [m]
(m/fmap (comp vector constantly) m))
(defn compile
"A drop-in replacement for graph/compile."
[computations]
{:pre [(map? computations)]}
(let [all-deps (mapcat second (let-like/dependencies computations))
missing (set/difference (set all-deps) (set (keys computations)))
; Note: Prepending and dropping dummy computations works because let-like/arrange is stable.
computations (concat (map vector missing) ; Note: Only the key; the rest will be nil during destructuring.
computations)
computations (let-like/arrange computations)
computations (drop (count missing) computations)]
(fn [m]
(->> computations
(let-like/run m)
m/->map))))
(defn ->components
"Turns pure computations into components (with no-op stop functions), so that you can use extensions that work on processes/components, e.g. logging for slow computations."
[computations]
(m/fmap (fn [[f deps]]
(component/component (program/clean f) deps))
computations))