On Friday, May 25, 2018 at 7:40:12 AM UTC-5, Sonny To wrote:
>
> Hi James,
> I'm trying to access the local bindings of the closure like it is an object
> (.-x bar) should give  1. I can use a defrecord for this but I want to use 
> regular functions and no macro magic
>

This isn't quite what you're asking for, but one approach to what records 
actually do would be to return a map of closures that include accessors for 
the state. (Disclaimer: I'm totally doing this off the top of my head in 
the google groups web interface, without even checking that the parentheses 
are balanced) One example would be:

(defn build-incrementer

  [x]

             {:inc (fn [] (inc x))
              :dec (fn [] (dec x))
              :status (fn [] x)})

Is that close to what you wanted?

Regards,
James
 

>
> On Friday, May 25, 2018 at 9:01:22 AM UTC+3, James Gatannah wrote:
>
>> On Thursday, May 24, 2018 at 1:22:42 PM UTC-5, Sonny To wrote:
>>>
>>> (defn foo[]
>>>  (let [x  1]
>>>    (fn []  
>>>      (+ x 1)
>>>     )
>>>  )
>>>
>>> (def bar  (foo))
>>>
>>> Is there a way to get the value of x from closure bar?
>>>
>>
>> I may be describing this incorrectly.
>>
>> You're creating a top-level var named bar.
>>
>> You're calling the function foo, and associating the value it returns to 
>> that var bar.
>>
>> foo's return value happens to be a function that looks like it should be 
>> a counter. Except that, in clojure, it isn't.
>>
>> This vaguely looks like an approach to lexical closures that I vaguely 
>> remember from scheme textbooks (and Paul Graham essays).
>>
>> What are you really trying to do?
>>
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to