for another look at closures,
http://code.jsoftware.com/wiki/Guides/Lexical_Closure Your examples don't seem to follow the pattern of binding a dyad with the result of its application as a side effect of its application. A side effect and "singleton" assigned closure makes sense if the closure is to act as a guard that updates on each call. ________________________________ From: Jose Mario Quintana <[email protected]> To: Programming forum <[email protected]> Sent: Wednesday, July 6, 2016 10:56 PM Subject: [Jprogramming] Are closures possible in J? I recently saw the following message from June Kim in the Chat Forum [0]. http://procyonic.org/blog/?p=171 I miss closure in J. which seems to imply the closures are missing in J. Perhaps I am misunderstanding the concept but, as far as I can see, closures or similar alternatives are available in J, at least within the (wicked) tacit dialect which is my focus of interest. One indication that closures (or similar alternatives) are available in J is the solution [1] to the Rosetta Code J task Closures/Value capture, which presumably involves closures. The following is another example: the function derivative (verb) described in the Wikipedia entry [2] can be implemented easily with wicked spells. ;) I am not showing at the details because this can be an interesting future Jym exercise but the outline of the implementation in the form of an edited session follows. First some good news, at least from my perspective, the functionality of a new updated version of the old Wicked Tacit Toolkit (WTT) [3] survives all the improvements up to version Beta-9. JVERSION Engine: j805/j64/windows Beta-9: commercial/2016-07-05T15:45:22 Library: 8.04.15 Qt IDE: 1.4.10/5.4.2 Platform: Win 64 Installer: J804 install InstallPath: j:/program files/j Contact: www.jsoftware.com Running the (new) WTT, (0!:1) < '/ ... /Wicked Tacit Toolkit.ijs' NB. NB. ---------------------------------------------------------------------------- NB. Wicked Tacit Toolkit... NB. ---------------------------------------------------------------------------- . . . Defining the producing adverb, NB. Derivative (secant)... Der=. ... NB. A one-liner, in terms of the utilities, is not shown to avoid a spoiler The defined word Der is a wicked fixed adverb; actually, it is a recurrent adverb (aka, multiple adverb (a double adverb in this instance)). I guess I am not spoiling much by showing its linear representation which is faulty (unfortunately, linear representations often are faulty for complex fixed tacit adverbs, not to mention wicked tacit adverbs). type'Der' ┌──────┐ │adverb│ └──────┘ 66 Wrap'Der' NB. It is fixed... ((("_)(((`'')(&(,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@:(<@:((,'0') , &:< ])@:((<,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@:(,^:(0:``:)&6 :.(< @:((,'0') ,&:< ]))@:(([ ,^:(0:`@:) ,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@:(<@:((,'0') ,&:< ])@:] ; >@:((<+)"_) ; >@:((<])"_))) ; >@:(( <-)"_) ; [) ; >@:((<%)"_) ; ] ,^:(0:`") _:)) ,~ <) ; >@:((<,~)"_) ; >@:((<<)"_))@:(,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@:(<@:((0;1;0) &({::))))@:[)))((`_)(`:6))))(,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@: ((<@:((,'0') ,&:< ])@:(,^:(0:`/))@:(,^:(0:`&)) >@:((<>)"_))@:>@:{: , <@:((,'0') ,&:< ])@:}:)@:))(((`'')(&(,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@:((<(,'0');"_) ; ] ; (<(,'0');(@:(,^:(0:``:)&6 :.(<@:((, '0') ,&:< ]))@:(<@:((0;1;0)&({::)))))((((`''`)(`(((@:[)(&`))(`:6)) ))(`((`_)(`:6))))(`:6)))"_)@:[)))((`_)(`:6))) ( double=. *: 0.000001 Der ) NB. Is the verb double a closure (*: and 0.000001 persist)? (*:@:(9.9999999999999995e_7 + ]) - *:) % 9.9999999999999995e_7"_ *: double 0 1 2 3 4 1e_12 4 16 36 64 Since the produced verb double is fixed it becomes independent of the producing adverb Der. Likewise, an intermediate adverb can be defined as, der=. 0.000001 Der NB. Is the adverb der is also a closure (0.000001 persists)? type'der' ┌──────┐ │adverb│ └──────┘ 66 Wrap'der' NB. It is fixed ("_)(((`'')(&(,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@:((<@:((,'0') ,& :< ])@:(,^:(0:`/))@:(,^:(0:`&)) >@:((<>)"_))@:>@:{: , <@:((,'0') , &:< ])@:}:)@:((9.9999999999999995e_7;,^:(0:``:)&6 :.(<@:((,'0') ,& :< ]))@:(,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@:(([ ,^:(0:`@:) ,^:(0 :``:)&6 :.(<@:((,'0') ,&:< ]))@:(<@:((,'0') ,&:< ])@:] ; >@:((<+)" _) ; >@:((<])"_))) ; >@:((<-)"_) ; [) ; >@:((<%)"_) ; ] ,^:(0:`") _:)) ,~ <)@:(,^:(0:``:)&6 :.(<@:((,'0') ,&:< ]))@:(<@:((0;1;0)&({: :))))@:[)))((`_)(`:6))) Again, once der is produced it becomes independent of Der because it is fixed. *: der (*:@:(9.9999999999999995e_7 + ]) - *:) % 9.9999999999999995e_7"_ *: der 0 1 2 3 4 1e_6 2 4 6 8 These examples show that nouns and verbs in an "environment" can be embedded in verbs and adverbs. Adverbs can be embedded in adverbs and verbs, as well (and verbs, adverbs and conjunctions can be taken indirectly as arguments by verbs and adverbs; for instance, by wrapping them in gerunds or boxed arrays). Alas, user defined tacit conjunctions apparently are not possible employing current official interpreters; one would have to revert to official Golden Age interpreters or use alternative Unbox/Jx interpreters. I cannot pretend that I am very familiar with the concept of closures; yet, it seems to me that, the conventional way for producing closures in J would be via explicit adverbs or conjunctions with nested (local) explicit verbs, adverbs or conjunctions which could be deliver as products (of course, these constructions are not supported, not yet anyway). The methods I have partially shown for producing fixed tacit verbs and adverbs are unconventional (then again, J, tacit writing and wicked spells are unconventional) but the effects seem to be the same or at least very similar. Any enlightenment would be appreciated. References [0] [Jchat] Interesting Critique of J http://www.jsoftware.com/pipermail/chat/2016-July/007002.html [1] Tacit (unorthodox) version in task Closures/Value capture http://rosettacode.org/wiki/Closures/Value_capture#Tacit_.28unorthodox.29_version [2] First-class functions in Applications of Closure (computer programming) https://en.wikipedia.org/wiki/Closure_(computer_programming)#First-class_functions [3] [Jprogramming] Tacit Toolkit (was dyadic J) http://www.jsoftware.com/pipermail/programming/2015-December/043757.html ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
