A follow up, its pretty easy to add other parameters to the function:

   '(T2|.(<T1)= ] ) T0@]^:[each]' evalTP ('3&*';'2') evalTP '2'  ;/ 1 2 3 2 2 4 
2 
┌─┬─┬─┬─┬─┬─┬─┐ 
│1│6│9│2│6│4│6│ 
└─┴─┴─┴─┴─┴─┴─┘

turns the resulting verb into tripling the item 2 cells previous when one cell 
is 2


   '(T2|.(<T1)= ] ) T0@]^:[each]' evalTP ('3&*';'2') 
( T0 |. ( < 2 ) = ] ) 3&* @ ] ^: [ each ] 




----- Original Message -----
From: 'Pascal Jasmin' via Programming <[email protected]>
To: "[email protected]" <[email protected]>
Cc: 
Sent: Sunday, May 18, 2014 12:16:46 PM
Subject: Re: [Jprogramming] ^: and boxes

yes.  sorry I didn't bother fixing the 2nd example:

one fix:

   ((1|.(<2)=])  +:@]^:[each]) scalarize@:tonumifnum each cut '1 2 3 2 2 4 2 a 
1' 
┌─┬─┬─┬─┬─┬─┬─┬─┬─┐ 
│2│2│6│4│2│8│2│a│1│ 
└─┴─┴─┴─┴─┴─┴─┴─┴─┘
where:
tonumifnum =: 0&".^:(1&". -: 0&".) 
scalarize =: {.^: ((,1) -: $) 


Here is another interesting approach to solving the problem with a template 
approach:

I've shown this conjunction before:

evalT =: 2 : 0 
NB. strip unused Ts (optional) 
n =. boxopen n 
pD imax =: <: # n NB. =. v"_ y 
m =.  ]`(''"_)@.(imax< 0 ". }.)^:('T'={.) each ;: m 
for_i. n do.  m =. i (I. m = <'T',": i_index) } m  end. 
(;: inv m) eval 
)
eval =: 1 : ' a: 1 :  m'


A conjunction that substitutes into template m, parameters from n.  Though this 
can already mostly be done through @. tricks, this is probably much more 
readable, and easy to write:  Here the verb is parametarized for double (+:) 
when following cell is 2

   '(1|.(<T1)= ] ) T0@]^:[each]' evalT ('+:';'2') 
(1 |. (<2) = ]) +:@]^:[&.> ] 

Its producing a tacit verb:

  '(1|.(<T1)= ] ) T0@]^:[each]' evalT ('+:';'2')       (<'a') ,~ ;/ 1 2 3 2 2 4 
2 
┌─┬─┬─┬─┬─┬─┬─┬─┐ 
│2│2│6│4│2│8│2│a│ 
└─┴─┴─┴─┴─┴─┴─┴─┘

I can improve this to provide partial substitutions with this conjunction:

evalTP =: 2 : 0 
n =. boxopen n 
c=. i.0 
for_i. n do. c=. c,   ('T' , [: ": i_index"_)^:(0=# ) each i end. n=. c 
imax =: <: # n NB. =. v"_ y 
m =. ;: m 
for_i. n do. m =. i (I. m = <'T',": i_index) } m end. 
 m =.('T', [: ": (>: imax) -~ 0 ". }.)^:(imax< 0 ". }.)^:('T'={.) each  m 
 if. -. (<'T0') e. m do. (;: inv m) eval else. ;: inv m end. 
) 

This allows for partial substitutions, and then returns the completed verb when 
no more substitutions are available:

   '(1|.(<T1)= ] ) T0@]^:[each]' evalTP ('+:') evalTP ('2') 
(1 |. (<2) = ]) +:@]^:[&.> ]  NB.tacit completed verb

   '(1|.(<T1)= ] ) T0@]^:[each]' evalTP ('+:') 
( 1 |. ( < T0 ) = ] ) +: @ ] ^: [ each ]    NB. string (template) with T0 
replacing T1 parameter 

   '(1|.(<T1)= ] ) T0@]^:[each]' evalTP ('';'2') 
( 1 |. ( < 2 ) = ] ) T0 @ ] ^: [ each ]     NB. string (template) with binding 
of T1 parameter but not T0

   '(1|.(<T1)= ] ) T0@]^:[each]' evalTP ('+:';'2')     (<'a') ,~ ;/ 1 2 3 2 2 4 
2 
┌─┬─┬─┬─┬─┬─┬─┬─┐ 
│2│2│6│4│2│8│2│a│ 
└─┴─┴─┴─┴─┴─┴─┴─┘

The last example shows binding all parameters such as evalT.  (evalT has 
different behaviour that deletes template items if not passed) 




----- Original Message -----
From: greg heil <[email protected]>
To: Programming forum <[email protected]>
Cc: 
Sent: Sunday, May 18, 2014 11:48:47 AM
Subject: Re: [Jprogramming] ^: and boxes

Pascal

>The difficulty is your examples are contradictory. The number 2 is different 
>from the letter 2.

>Perhaps a gold standard of the test case is needed eg
    d2=: <2
or
    d2=: <,2
or
    d2=: <,'2'
or it might be a set...

greg
~krsnadas.org

--

from: 'Pascal Jasmin' via Programming <[email protected]>
to: "[email protected]" <[email protected]>
date: 18 May 2014 06:56
subject: Re: [Jprogramming] ^: and boxes

That is a cool way of getting doubles:

   ([: >: (<2) = 1 |. ]) ;/ 1 2 3 2 2 4 2 1
2 1 2 2 1 2 1 1

>I thank you for the creative approach, while still following all the 
>directions.  I learned something from all of the creative approaches provided. 
> I was most interested in a good way to modify boxed data cells based on the 
>content of other cells.  I understand how people are helpfully trying to solve 
>the problem they want to solve instead of magically reading my intent.  When 
>presenting the problem I prefer that there be a final unboxed presentation for 
>display purposes, and I also prefer the simplest possible data argument, but 
>it makes deciphering my intent more difficult.

>Here is a solution/problem statement that makes the boxed input requirement 
>more than just an inconvenience.  Perhaps if I put white space between the 
>verb and data, its clear that the solution being sought should only modify the 
>left of the white space:

   ((1|.(<2)=]each) +:@]^:[each ])              (<'a') ,~ ;/ 1 2 3 2 2 4 2
+---------------+
¦2¦2¦6¦4¦2¦8¦2¦a¦
+---------------+

>Though that example invites solutions that remove the tail then add it back 
>later :P.  So, perhaps I should provide 2 sample inputs:

   ((1|.(<2)=]each) +:@]^:[each ])      cut '1 2 3 2 2 4 2 a 1'
+-----------------+
¦1¦2¦3¦2¦2¦4¦2¦a¦1¦
+-----------------+

--

From: Joe Bogner <[email protected]>
To: [email protected]
Sent: Sunday, May 18, 2014 8:04:08 AM
Subject: Re: [Jprogramming] ^: and boxes

>I would probably do this. Boxing is based on original requirement of right 
>side being boxed

(> * [: >: (<2) = 1 |. ]) ;/ 1 2 3 2 2 4 2 1
2 2 6 4 2 8 2 1

Works on the negative test too

(> * [: >: (<2) = 1 |. ])  ;/ _1 2 _3 2 2 _4 2 _1
_2 2 _6 4 2 _8 2 _1

>It doesn't use ^: , which was in the subject line and may not be valid if ^: 
>is required

--

from: greg heil <[email protected]>
to: Programming forum <[email protected]>
date: 17 May 2014 08:28
subject: Re: [Jprogramming] ^: and boxes


Linda

>Wow that is super understandable and compact! Of course its domain is limited, 
>Eg it does not like negative numbers:

    d=:  1 2 3 2 2 4 2 1
    dn=: _1 2 _3 2 2 _4 2 _1
    dn=: (]* _1&^ @ (2~:]))  d

    fLA=: ] >. +: * 1 |. 2 = ]
    fLA dn
_1 2 _3 4 2 _4 2 0

>It did take me a _long_ time to penetrate Aai`s sophistry! Finally the key was 
>that /. was really oblique, not key! ...but it does survive this negative 
>number test:

     fA=: [: }. 2&*//.@(2&=,:])
     fA dn
_2 2 _6 4 2 _8 2 _1

>Anyway i think i am awake this am, thanks! i could be fooled though, only 
>dreaming i was awake;)

greg
~krsnadas.org

--

from: Linda Alvord <[email protected]>
to: [email protected]
date: 16 May 2014 21:51
subject: Re: [Jprogramming] ^: and boxes

Maybe you'll like this.


   f=: 13 :'y >.(1 |. 2 = y)*+:y'
   f 1 2 3 2 2 4 2
2 2 6 4 2 8 2

   f
] >. +: * 1 |. 2 = ]

Linda
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to