A non-trivial meta-programming exercise? That is a cheating opportunity :)
train=. (<'`:')(0:`)(,^:)&6 NB. Usual definition
an=. <@:((":0) ,&< ]) NB. Usual definition
ce=. &(|.@:[ train@:, an@:]) NB. Actual coding for this exercise (maybe
it was trivial after all)
0 0$(19$E`O)ce 100
98
97
95
94
92
91
89
88
86
85
83
82
80
79
77
76
74
73
71
On Mon, Jul 13, 2015 at 1:16 PM, Raul Miller <[email protected]> wrote:
> Actually, thinking about this, it would be nice to have an adverb
> which worked something like `:6 but which gave the effect of running
> the gerunds in sequence (from right to left). That way, I could
> encapsulate the details of how this to encode the train and then
> ignore those details after that.
>
> In other words, something like this:
>
> concatevoke=:1 :0
> fill=. {.[:`]
> (({:m),~_1|.1j1 #!.fill }:m)`:6
> )
>
> 0 0$(19$E`O) concatevoke 100
> 98
> 97
> 95
> 94
> 92
> 91
> 89
> 88
> 86
> 85
> 83
> 82
> 80
> 79
> 77
> 76
> 74
> 73
> 71
>
> I'm not sure if there's a more elegant way of phrasing concatevoke.
> But it gets the job done. Heres a shorter view of what it does
> (shortened to avoid email line wrap problems). I have also swapped E
> and O, just because:
>
> (9$O`E) concatevoke
> [: O [: E [: O [: E [: O [: E [: O [: E O
>
> Thanks,
>
> --
> Raul
>
> On Mon, Jul 13, 2015 at 1:01 PM, Raul Miller <[email protected]>
> wrote:
> > This is not a good candidate for the power conjunction.
> >
> > You could do it with the power conjunction but it's not a good fit.
> >
> > The reasons for this are:
> >
> > (1) You have two independent values you are working with - the value
> > you use for flow control is independent of the value you are passing
> > to your functions e and o
> >
> > (2) The expression itself produces no useful result.
> >
> > So this means that at every step of the way you are working against
> > the design of the power conjunction. There is no elegance to be had,
> > and probably a better approach would be to indent your explicit
> > definition.
> >
> > t =. monad define
> > n =. 20
> > while. n =. n - 1 do.
> > if. 2|n do. smoutput y =. e y
> > else. smoutput y =. o y
> > end.
> > end.
> > )
> >
> > That said, a if you wanted to rephrase this tacitly, here are the
> > steps I would take:
> >
> > First, since you really want the side effects, define variations on e
> > and o which display their result:
> >
> > E=:1!:2&2@e
> > O=:1!:2&2@o
> >
> > Second, looking at your implementation of t, you have a total of 19
> > times through the loop. You have 10 'e' invocations and 9 'o'
> > invocations. So I'd set this up as a train:
> >
> > (36$[:`E`[:`O)`E`:6
> >
> > But, also, your result is the result that smoutput would produce, so:
> >
> > T=: 0 0 $ (36$[:`E`[:`O)`E`:6
> > T 100
> > 98
> > 97
> > 95
> > 94
> > 92
> > 91
> > 89
> > 88
> > 86
> > 85
> > 83
> > 82
> > 80
> > 79
> > 77
> > 76
> > 74
> > 73
> > 71
> >
> > Good enough?
> >
> > Well, maybe not... since your 'e' happens for the odd values of N, and
> > your 'o' happens for the even instances, I'd be tempted to rename
> > them.
> >
> > Thanks,
> >
> > --
> > Raul
> >
> >
> > On Mon, Jul 13, 2015 at 12:39 PM, Brian Schott <[email protected]>
> wrote:
> >> I have the following verb t and wonder if anyone would like to show how
> it
> >> could be more elegantly defined (using the Power conjunction?) I would
> >> enjoy learning your result.
> >>
> >> The intent of t is to alternatively subtract 1 or 2 from a positive
> number
> >> showing the intermediate results of say n=20 subtractions.
> >>
> >>
> >> e =. -&2
> >> o =. -&1
> >>
> >> t =. monad define
> >> n =. 20
> >> while. n =. n - 1 do.
> >> if. 2|n do. smoutput y =. e y
> >> else. smoutput y =. o y
> >> end.
> >> end.
> >> )
> >> t 100
> >> 98
> >> 97
> >> 95
> >> 94
> >> 92
> >> 91
> >> 89
> >> 88
> >> 86
> >> 85
> >> 83
> >> 82
> >> 80
> >> 79
> >> 77
> >> 76
> >> 74
> >> 73
> >> 71
> >>
> >>
> >> --
> >> (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
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm