Is a function defined in a loop created at each iteration? #1418
Unanswered
amano-kenji
asked this question in
Q&A
Replies: 1 comment 5 replies
-
|
I examined the disassembly output of what seemed like an example of the kind of code the question may have been referring to: (disasm (fn [] (for i 0 2 (defn my-fn [x] (+ x 1)))))
# =>
'{:arity 0
:bytecode @[(ldi 0 0)
(ldi 1 2)
(clo 2 1)
(call 2 2)
(retn)]
:constants @[]
:defs @[{:arity 1
:bytecode @[(addim 2 0 1) (ret 2)]
:constants @[]
:defs @[]
:environments @[]
:max-arity 1
:min-arity 1
:name "my-fn"
:slotcount 3
:source "repl"
:sourcemap @[(3 43) (3 43)]
:structarg false
:symbolmap @[(0 2 0 x) (0 2 1 my-fn)]
:vararg false}
{:arity 0
:bytecode @[(ldu 1 0 0)
(ldu 2 0 1)
(lt 0 1 2)
(jmpif 0 2)
(retn)
(ldu 1 0 0)
(clo 2 0)
(ldu 4 0 0)
(addim 2 4 1)
(setu 2 0 0)
(lds 4)
(tcall 4)]
:constants @[]
:defs @[{:arity 1
:bytecode @[(addim 2 0 1) (ret 2)]
:constants @[]
:defs @[]
:environments @[]
:max-arity 1
:min-arity 1
:name "my-fn"
:slotcount 3
:source "repl"
:sourcemap @[(3 43) (3 43)]
:structarg false
:symbolmap @[(0 2 0 x) (0 2 1 my-fn)]
:vararg false}]
:environments @[-1]
:max-arity 2147483647
:min-arity 0
:name "_while"
:slotcount 5
:source "repl"
:sourcemap @[(3 16)
(3 16)
(3 16)
(3 16)
(3 16)
(3 16)
(3 27)
(3 16)
(3 16)
(3 16)
(3 16)
(3 16)]
:structarg false
:symbolmap @[(:upvalue 0 0 _00000r)
(:upvalue 0 1 _00000s)
(5 12 1 i)
(7 12 3 my-fn)]
:vararg false}]
:environments @[]
:max-arity 0
:min-arity 0
:slotcount 3
:source "repl"
:sourcemap @[(3 16) (3 16) (3 16) (3 16) (3 16)]
:structarg false
:vararg false}spoiler
My initial impression is that I think what :defs @[{:arity 1
:bytecode @[(addim 2 0 1) (ret 2)]
:constants @[]
:defs @[]
:environments @[]
:max-arity 1
:min-arity 1
:name "my-fn"
:slotcount 3
:source "repl"
:sourcemap @[(3 43) (3 43)]
:structarg false
:symbolmap @[(0 2 0 x) (0 2 1 my-fn)]
:vararg false}]
For reference, "closure" here, may mean this. So, tentatively, "yes" to the original question because it looks to me like |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Or, is it created only once and given the new fiber environment each time?
What about a function defined in a recursive function?
Beta Was this translation helpful? Give feedback.
All reactions