Back
fold (clj)
(source)function
(fold system f val)
Reduce all the key value pairs in system map in dependency order, starting
from an initial value. The function should take three arguments: the
accumulator, the current key and the current value.
Examples
integrant
(ns integrant.core-test
(:require #?(:clj [clojure.test :refer [are deftest is testing]]
:cljs [cljs.test :refer-macros [are deftest is testing]])
[integrant.core :as ig]
[weavejester.dependency :as dep]))
(deftest fold-test
(let [config {::a (ig/ref ::ppp), ::b (ig/ref ::pp), ::p 1, ::c 2}
system (ig/init config)]
(is (= (ig/fold system #(conj %1 [%2 %3]) [])
[[::p [1]] [::a [[1]]] [::b [[1]]] [::c [2]]]))))
duct-framework/core
(ns duct.core-test
(:require [clojure.java.io :as io]
[clojure.test :refer :all]
[duct.core :as core]
[duct.core.merge :as merge]
[integrant.core :as ig]))
(deftest test-fold-modules
(let [m {::foo {:x 1}, ::bar {:x 2, :r (ig/ref ::foo)}}]
(is (= (core/fold-modules (ig/init m))
{::x [1 2]}))))
(deftest test-profile-keyword
(core/load-hierarchy)
(let [m {:duct.profile/base {::a 1, ::b (ig/ref ::a)}
[:duct/profile ::x] {::a 2, ::c (ig/refset ::b)}}
p (ig/prep m)]
(is (= p
{:duct.profile/base {::a 1, ::b (core/->InertRef ::a)}
[:duct/profile ::x] {::a 2, ::c (core/->InertRefSet ::b)
::core/requires (ig/refset :duct.profile/base)}}))
(is (= (core/fold-modules (ig/init p))
{::a 2, ::b (ig/ref ::a), ::c (ig/refset ::b)}))))
(deftest test-profile-dev-keyword
(core/load-hierarchy)
(let [m {:duct.profile/base {::a 1, ::b (ig/ref ::a)}
:duct.profile/dev {::a 2, ::c (ig/refset ::b)}}
p (ig/prep m)]
(is (= p
{:duct.profile/base {::a 1, ::b (core/->InertRef ::a)}
:duct.profile/dev {::a 2, ::c (core/->InertRefSet ::b)
::core/requires (ig/refset :duct.profile/base)
::core/environment :development}}))
(is (= (core/fold-modules (ig/init p))
{::a 2, ::b (ig/ref ::a), ::c (ig/refset ::b)
::core/environment :development}))))
(deftest test-profile-test-keyword
(core/load-hierarchy)
(let [m {:duct.profile/base {::a 1, ::b (ig/ref ::a)}
:duct.profile/test {::a 2, ::c (ig/refset ::b)}}
p (ig/prep m)]
(is (= p
{:duct.profile/base {::a 1, ::b (core/->InertRef ::a)}
:duct.profile/test {::a 2, ::c (core/->InertRefSet ::b)
::core/requires (ig/refset :duct.profile/base)
::core/environment :test}}))
(is (= (core/fold-modules (ig/init p))
{::a 2, ::b (ig/ref ::a), ::c (ig/refset ::b)
::core/environment :test}))))
(deftest test-profile-prod-keyword
(core/load-hierarchy)
(let [m {:duct.profile/base {::a 1, ::b (ig/ref ::a)}
:duct.profile/prod {::a 2, ::c (ig/refset ::b)}}
p (ig/prep m)]
(is (= p
{:duct.profile/base {::a 1, ::b (core/->InertRef ::a)}
:duct.profile/prod {::a 2, ::c (core/->InertRefSet ::b)
::core/requires (ig/refset :duct.profile/base)
::core/environment :production}}))
(is (= (core/fold-modules (ig/init p))
{::a 2, ::b (ig/ref ::a), ::c (ig/refset ::b)
::core/environment :production}))))