Posted to wiki http://www.jsoftware.com/jwiki/PascalJasmin/Multiline%20tacit%20expressions%20with%20macros
fixed a "misconvention" with (v c) on a line, and added suggested indentation conventions. A simple expression that looks pretty clear is: test4 =: 0 : 0 Tacify -: +: @ NB. note this creates expression leading with +:@-: @ +: @ +: @: +: +: +: MACcompose ) it evaluates to: test4 +:@-:@+:@+:@:(+:@:+:@:+:) ________________________________ From: Pascal Jasmin <[email protected]> To: "[email protected]" <[email protected]> Sent: Saturday, March 1, 2014 1:16:34 AM Subject: [Jprogramming] Multiline tacit expressions with comments and macros Here is something pretty cool. writing tacit expressions as multiline strings. Comments are supported as well as macros. Macros are commands appended at the end of any line (before any comment) that start with the letters MAC and are visible in the multiline locale. Any new macro may be defined without any change to processing 2 macros are defined: MACdef will parse an explicit definition inside a tacit expression. It consumes 1 word token off the line (valid tokens are any left argument to : ) MACcompose will insert @: between any v v or a v token pairs on the line. ( or ) will "break" the auto insert, but you may fill in your own. The overall usefulness is cutting down on parentheses and with macros cutting down on @: . Each line in a multiline tacit definition gets wrapped with parentheses, and so characters and common mistakes are avoided. Though I'm sure it is quirky, I found it pretty easy to pick up in writing a fairly challenging tacit expression. Its relatively easy to test partial expressions or to have commented alternative subsections. Tacify is an adverb used to create multiline tacit definitions. Called with 0 : 0 Tacify. The definition is simple enough to fit on one line, and so can be extended pretty easily. ncS=:3 :'z=.y 1 :y label_. 4!:0 <''z'' ' :: _2: NB. nameclass of string pD =: 1!:2&2 cocurrent 'multiline' Tacify =: 1 : 0 NB. predefine to bootstrap parenbutconjunction pD lines=. addparen@:procmac@:stripcomment leaf LF cut m 1 (1 : (;: inv lines)) ) procmac =: 3 : 0 if. ('MAC' -: 3&{.@:>@{:) ;: y do. (;: inv }: ;: y) 1 (1 : ( > {: ;: y)) else. y end. ) addparen =: (')' ,~ '(' , ])^:(0<#) stripcomment =: }:^:('NB.' -: 3&{.@:>@{:)&.;: NB.macros must return string NB. example macro transforms a quoteless explicit definition into a MACdef =: 1 : 0 (> {: ;: m) , ' : ' , (quote ;: inv }: ;: m ) ) MACcompose =: 1 : 0 NB. inserts @: between consecutive verbs. or adverb verb ;: inv ({. ;: m), 2 {:`('@:' , leaf {:)@.(((1=ncS@{. ) +. 3=ncS@{.) *. 3=ncS@{:)\ ;: m ) NB. 3 Tacify parenbutconjunction =: 0 : 0 Tacify addparen ` ( > {: ;: MACcompose NB. open cross line paren ( > {: ;:) ,~ (,&' ')@: addparen @: (;: inv) @: }: ;: MACcompose NB. wont compose to (, so fork made @.(1 < # @: ;:)) @. (1 = # @: ;:) +. 2 = ncS@>@{:@: ;: ) Tacify =: 1 : 0 pD lines=. parenbutconjunction@:procmac@:stripcomment leaf LF cut m 1 (1 : (;: inv lines)) ) Tacify_z_ =: Tacify cocurrent 'base' test =: 0 : 0 Tacify +/ x % y 13 MACdef NB.macro that will call 13 : x % y and return % # @:> ) NB. MACdef =: 1 : '(". > {: ;: m) : (;: inv }: ;: m )' test2 =: 0 : 0 Tacify +: y 3 MACdef (@:+: @:+ /) ) test3 =: 0 : 0 Tacify 4 + [ + 2 x + y 4 MACdef ] ) test4 =: 0 : 0 Tacify +: @ +: @ +: @: +: +: +: MACcompose ) ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm ` ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
