Clojure closure

前端 未结 4 515
迷失自我
迷失自我 2021-01-31 19:43

the other day I was trying to come up with an example of closure in Clojure. I came up with and example I had seen before and thought it was appropriate.

Alas, I was tol

4条回答
  •  渐次进展
    2021-01-31 20:24

    I wanted to have something that setup constant value(s) that are to be used each time.

    (def myran 
      (let [constrand (rand)]
        (fn [n] (* n constrand))))
    
    
    (myran 3)
    2.7124521745892096
    (myran 1)
    0.9041507248630699
    (myran 3)
    2.7124521745892096
    

    This will only set a value for "constrand" once. This is a very contrived example, but I wanted to be able to do something like:

    JavaScript: The Good Parts

    This is from: JavaScript: The Good Parts

提交回复
热议问题