I have the J curse: whenever I see a J snippet, I’m compelled to try my own spin on it. There’s gotta be a way to do this without indexes dangit
Okay got it: +/\ <: +: (? 100 #2). You have an array of 0 1, then +: doubles it (0 2), then <: decrements (_1 1).
+/\ <: +: (? 100 #2)
0 1
+:
0 2
<:
(_1 1)
I’ve started leaning on Raku as a calculator instead of J, here’s what it’s like in Raku: [\+] <-1 1>.roll(100). No builtin plot library, though.
[\+] <-1 1>.roll(100)
I have another curse. I can’t help removing all the useless bytes. Why can’t you just write it as it should be.
+/\<:+:?100#2
Indeed. All the intermediate variables for such a trivial problem are very un-J-like in spirit.
Your snippet is clear and instantly grokkable by a J programmer.
The OP got rid of all the intermediate variables later :P
Random walk in 2 lines of J
Fair enough, I missed that.
I think the original article was written for the ‘non-J programmer’.
Another sufferer here. My first thought was to rewrite it using the power conjunction ^:
^:
+/\{{y,<:+:?2}}^:1e3]1
With power you can directly compute the final random walk array without the extra sumscan [Dyalog version]
{⍵,¯1 1[?2]+⊃⌽⍵}⍣100⊢0
Some folks in the APL Farm discord had additional suggestions and putting them together resulted in this: (+_1^?@2)^:(<1e3)0. The boxed n (<1e3) collects results into a list; not sure if there’s an analog to that in Dyalog.
(+_1^?@2)^:(<1e3)0
<1e3
That’s cool, but afaik Dyalog doesn’t have a variant that collects the list like that.
reading this made my head spin
Is that in a positive or negative way?
I take quantum solace in that joke
I have the J curse: whenever I see a J snippet, I’m compelled to try my own spin on it. There’s gotta be a way to do this without indexes dangit
Okay got it:
+/\ <: +: (? 100 #2). You have an array of0 1, then+:doubles it (0 2), then<:decrements(_1 1).I’ve started leaning on Raku as a calculator instead of J, here’s what it’s like in Raku:
[\+] <-1 1>.roll(100). No builtin plot library, though.I have another curse. I can’t help removing all the useless bytes. Why can’t you just write it as it should be.
Indeed. All the intermediate variables for such a trivial problem are very un-J-like in spirit.
Your snippet is clear and instantly grokkable by a J programmer.
The OP got rid of all the intermediate variables later :P
Fair enough, I missed that.
I think the original article was written for the ‘non-J programmer’.
Another sufferer here. My first thought was to rewrite it using the power conjunction
^:+/\{{y,<:+:?2}}^:1e3]1With power you can directly compute the final random walk array without the extra sumscan [Dyalog version]
{⍵,¯1 1[?2]+⊃⌽⍵}⍣100⊢0Some folks in the APL Farm discord had additional suggestions and putting them together resulted in this:
(+_1^?@2)^:(<1e3)0. The boxed n (<1e3) collects results into a list; not sure if there’s an analog to that in Dyalog.That’s cool, but afaik Dyalog doesn’t have a variant that collects the list like that.
reading this made my head spin
Is that in a positive or negative way?
I take quantum solace in that joke