a: is a dummy argument used to avoid the bracketted alternative 1 : '(0) 1 : m'
providing an argument executes the 1 : m adverb.
eval can return (evaluate) nouns, dyadic verbs and modifiers, in addition to
apply's monadic verbs.
2 '+' eval 3
5
# '33' eval
1
+: '@+/\' eval i.4
0 2 12 44
'+:' eval '@+/\' eval i.4
0 2 12 44
22 '+' eval '+/\' eval i.4
22 23 25 28
'22' eval '+' eval '+/\' eval i.4
22 23 25 28
Here is a templating conjunction that produces any form of speech, and has some
fancyness with stripping out unused template variables:
evalT =: 2 : 0
imax =: <: # n NB. strip unused Ts (optional)
m =. ]`(''"_)@.(imax< 0 ". }.)^:('T'={.) each ;: m
NB. for_i. n do. ('T',": i_index) =. (>i) eval end. NB. used with eval f. on
last line
. creates temp vars
for_i. n do. m =. i (I. m = <'T',": i_index) } m end.
(;: inv m) eval NB. f. version cannot eval to a noun, and generally
unpreferable to substitution method
)
'(T0 % T1) T2' evalT ('+/';'#') NB. T2 unused and stripped out
+/ % #
'(T0 % T1) T2' evalT ('+/';'#';'';'nothing') NB. can also pass empty
arg/skip/too many args
+/ % #
'(T0 % T1) T2' evalT ('+/';'#';":@:i.5) NB. can use lr instead of ": for
more complex data arg
2
'(T0 % T1) T2' evalT ('+/';'#') i.5
2
'(T0 % T1) T2' evalT ('+/';'#';'@:(#~0<])') i.5
2.5
substitution version also allows invalid fragments:
'T0 % T1' evalT ('(+/';'#)') i.5
2
'T0 % T1' evalT ('(+/';'#)@:(#~0<])') i.5
2.5
sum =: +/
'T0 % T1' evalT ('(sum';'#)@:(#~0<])') i.5
2.5
you can also use multiple T# in template and they will all be substituted by
same value from n
'T0 @: T1 @: T2' evalT ('+/';'#';'';'nothing') NB. makes an adverb dealing
with too many params
+/@:#@:
----- Original Message -----
From: Brian Schott <[email protected]>
To: Programming forum <[email protected]>
Cc:
Sent: Saturday, April 26, 2014 10:53:33 AM
Subject: Re: [Jprogramming] J in 5 minutes
Pascal,
Not bad, at all. I have seen eval before I think, but did not understand
its use.
Now I am trying to understand its pieces.
eval =: 1 : ' a: 1 : m'
Am I correct that the `1 : m` part is an explict adverb and that `a:` is
its argument that is not even used until eval is executed. I am pretty sure
that is right, because I tried to redefine eval as follows, as an
experiment, and got the undesirable, but informative result I show below.
Notice that I replaced a: with eval, and the final result of FName_eq is
wrong, but telling.
eval =: 1 : ' eval 1 : m'
evalassign =: 4 : ('(x) =: y eval';'1')
cols=: 'FName';'LName';'Age';'Company'
( (') # ]',~ '(= ' ,]) evalassign~ '_eq' ,~ ]) each cols
┌─┬─┬─┬─┐
│1│1│1│1│
└─┴─┴─┴─┘
FName_eq
eval(1 : '(= FName) # ]')
On Fri, Apr 25, 2014 at 10:23 PM, 'Pascal Jasmin' via Programming <
[email protected]> wrote:
> remembering eval
>
> eval =: 1 : ' a: 1 : m'
> evalassign =: 4 : ('(x) =: y eval';'1')
>
> evalassign turns eval into a verb (so we can use rank or each on it), and
> discards the result (would be a domain error to not return a noun from a
> verb), but in the process assign what y evals to (x).
>
> so:
> cols=: 'FName';'LName';'Age';'Company'
>
> ( (') # ]',~ '(= ' ,]) evalassign~ '_eq' ,~ ]) each cols
> ┌─┬─┬─┬─┐
> │1│1│1│1│
> └─┴─┴─┴─┘
> FName_eq
> (= FName) # ]
>
>
> it could be prettier with a templating verb (that substitutes into an x
> argument), but still not bad?
>
>
> --
(B=)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm