Hey there, I' ve been thinking about possible implementations for adding curryfication to block closures. Here's how it would be used, following by the implementation.
addBinary := [:p1 :p2 | p1 + p2] .
add3 := addBinary curry: 3. add3 curry: 5. 8 BlockClosure >> curry: aParameter (numArgs = 0)
ifTrue: [Error signal.]. (numArgs = 1) ifTrue: [^ self value: aParameter.].
(numArgs = 2) ifTrue: [ ^ [:p1 | self value: aParameter value: p1]
]. (numArgs = 3) ifTrue: [
^ [:p1 :p2 | self value: aParameter value: p1 value: p2] ]. (numArgs = 4) ifTrue: [ ^ [:p1 :p2 :p3 | self value: aParameter value: p1 value: p2 value: p3]
]. Yes, I know the implementation works only for a fixed number of arguments. Any ideas on how to dynamically create a block with an arbitary number of parameters.? Maybe creating the source code string and compiling it?
Basically I 'd like to create this block : ^ [:p1 .. :pn | self value: aParameter value: p1 ... value: pn ] Thanks, Seba
|
You can use #valueWithArguments: or #valueWithPossibleArgs: instead of using #value:value:value:
numArgs instVar might be your friend in what you want to achieve. Regards, |
Esteban, thanks for your answer.. I think i need to dynamically create a block which accepts exactly n parameters , so that the new block would behave as expected. Is that possible ? Is that what you had in mind?
2012/12/25 Esteban A. Maringolo <[hidden email]> You can use #valueWithArguments: or #valueWithPossibleArgs: instead of using |
On 26 December 2012 00:54, Sebastián Krynski <[hidden email]> wrote:
> Esteban, thanks for your answer.. I think i need to dynamically create a > block which accepts exactly n parameters , so that the new block would > behave as expected. Is that possible ? Is that what you had in mind? You have read Vassili Bykov's posts on the subject, right? Starting here: http://blog.3plus4.org/2007/03/23/currying-in-smalltalk/ frank > 2012/12/25 Esteban A. Maringolo <[hidden email]> > >> You can use #valueWithArguments: or #valueWithPossibleArgs: instead of >> using >> #value:value:value: >> >> numArgs instVar might be your friend in what you want to achieve. >> >> Regards, >> >> >> >> -- >> View this message in context: >> http://forum.world.st/on-blockclosure-currying-tp4660895p4660896.html >> Sent from the Pharo Smalltalk mailing list archive at Nabble.com. >> > |
Frank, yes I have, but I wasn't quite happy with the protocol used nor the creation of CurriedBlock and Currier classes.
My idea is to create a new method #curry: in BlockClosure, which in turn, returns another BlockClosure (with n-1 arity).
Thanks again.
2012/12/26 Frank Shearar <[hidden email]>
|
Free forum by Nabble | Edit this page |