Dependency DSL in Clojure
28 March 2013
An aim to create a dependency DSL similar to something we use in my workplace in Clojure. Below is a bit of sample code. This dependency snippet could be loaded as a separate file or embedded in Clojure source file as a macro.
$id <- (:tradeid $input) $coupon <- (get-in $input [:payments :coupon]) $daycount <- (get-in $input [:payments :daycount]) $product <- (:product $input) $product-mapping <- {"EXTSWAP" "DefaultableSwapLeg"} $xvar-product <- (get $product-mapping $product) $xvar <= {(keyword (str $id)) {:Id (str $id) :ProductType $xvar-product :Security {:Coupon (double $coupon) :DayCount (str $daycount)}}}
Each $-prefixed symbol defines a "variable" i.e. a reference in the global state that can be retrieved and updated. <- and <= operators define dependency rules. While <- create a rule that caches calculated values and prevents recalculation when there are no changes to the input, <= never caches (which can be useful to avoid wasteful storage of intermediate results).
Implementation
Two straight forward implementations spring to mind: to use a single ref to hold the state of the whole graph and implement updates and lazy reads as queries against the state that change the value of the ref, alternatively to use a ref for each individual variable and hence create a network of interlinked refs. Initial tests suggest that both implementations have about the same performance for vanilla operations.
See github here.
comments powered by Disqus