1. 21
    1. 9

      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).

      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. 4

        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
        
        1. 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.

          1. 3

            The OP got rid of all the intermediate variables later :P

            1. 2

              Random walk in 2 lines of J

              Fair enough, I missed that.

            2. 2

              I think the original article was written for the ‘non-J programmer’.

          2. 2

            Another sufferer here. My first thought was to rewrite it using the power conjunction ^:

            +/\{{y,<:+:?2}}^:1e3]1

            1. 2

              With power you can directly compute the final random walk array without the extra sumscan [Dyalog version]

              {⍵,¯1 1[?2]+⊃⌽⍵}⍣100⊢0

              1. 1

                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. 1

                  That’s cool, but afaik Dyalog doesn’t have a variant that collects the list like that.

            2. 1

              reading this made my head spin

              1. 1

                Is that in a positive or negative way?

                1. 2

                  I take quantum solace in that joke