-
Notifications
You must be signed in to change notification settings - Fork 1
/
scope.cljc
40 lines (28 loc) · 810 Bytes
/
scope.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
(ns fif.utils.scope
"Implements a container system which allows for retrieval of values
within a hierarchy of containers, where each container is a separate
scope.")
(defn new-scope
([scope]
(conj scope {}))
([] (new-scope [])))
(defn update-scope
[scope f & args]
(apply update-in scope [(dec (count scope))] f args))
(defn remove-scope
[scope]
(pop scope))
(def not-found ::miss)
(defn get-in-scope
([scope attrs default]
(as-> scope $ (map #(get-in % attrs not-found) $)
(filter #(not= % not-found) $)
(reverse $)
(if (empty? $) default (first $))))
([scope attrs] (get-in-scope scope attrs nil)))
(defn update-global-scope
[scope f & args]
(apply update-in scope [0] f args))
(defn get-merged-scope
[scope]
(reduce merge scope))