Namespace local variables in clojure -


i have 1 loop, looping on multiple functions different namespaces. lets have (syntax not perfect im sure):

namespace a:    (defn evaluate [time] ...do calculations involve running variables pertain current namespace... )  core.clj : (loop      (a.evaluate [(gettimeincrement)]) ) 

lets need keep running total value, running max , running min in evaluate. dont want return of these each time , pass them each loop iteration. feel better keep these "namespace local variables" , have separate core file. "clojure" way of going this?

a.evaluate not valid clojure syntax. suspect [(gettimeincrement)] erroneous (this passes vector containing result of (gettimeincrement) time arg of a/evaluate. further, usage of loop won't compile because not have valid binding form, , not looping because lacks invocation of recur.

on design level, idea namespace should keep local state misguided in clojure. namespaces each singleton class, function tracks state mixing concerns of various callers. responsibility of function in functional programming not encapsulate mutable state, encapsulate stateless operations on immutible input.

all said, despite fact local state bad idea in clojure, possible keep mutable local state if needed (for example, keeping local store of results memoization, or timing info profiling).

user> (ns a) nil a> (def profile-data (atom {})) #'a/profile-data a> (defn evaluate [time]       (let [random-result (rand)]         (swap! profile-data assoc time random-result)         random-result)) #'a/evaluate a> (in-ns 'user) #<namespace user> user> (loop [i 0]              (println "evaluated" (a/evaluate (java.util.date.)))               (when (< 10)                  (thread/sleep (rand-int 10000))                  (recur (inc i)))) evaluated 0.3592263204831543 evaluated 0.44324065840598925 evaluated 0.774719597719642 evaluated 0.6153068093424424 evaluated 0.19318953291776575 evaluated 0.7854068601989888 evaluated 0.21019788814948281 evaluated 0.2737466390531492 evaluated 0.1150218371862356 evaluated 0.16852130959555156 evaluated 0.6928076988931166 nil user> (clojure.pprint/pprint (deref a/profile-data)) {#inst "2014-06-27t19:52:56.036-00:00" 0.2737466390531492,  #inst "2014-06-27t19:53:20.068-00:00" 0.6928076988931166,  #inst "2014-06-27t19:52:51.438-00:00" 0.7854068601989888,  #inst "2014-06-27t19:52:53.038-00:00" 0.21019788814948281,  #inst "2014-06-27t19:52:43.215-00:00" 0.774719597719642,  #inst "2014-06-27t19:52:44.204-00:00" 0.6153068093424424,  #inst "2014-06-27t19:52:49.833-00:00" 0.19318953291776575,  #inst "2014-06-27t19:53:05.138-00:00" 0.1150218371862356,  #inst "2014-06-27t19:52:40.595-00:00" 0.3592263204831543,  #inst "2014-06-27t19:53:11.870-00:00" 0.16852130959555156,  #inst "2014-06-27t19:52:42.842-00:00" 0.44324065840598925} nil 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -