← Back to context

Comment by gmfawcett

6 years ago

I'm resisting the urge to rewrite it in APL. :) Nicely done.

No need to resist

  (⊃,(⍕2-⍨≢),⊃∘⌽)'accessibility'

  • I tried to write it a couple of different ways, but I keep coming back to approximately your code as the most direct way, if written slightly differently with (⍕¯2+⍴) in the middle; I'm puzzled by the behaviour of being able to drop and take negative numbers to index from the end of the array, but not being able to use negative indexes in other contexts. Why is (¯1⌷word) an error, instead of working like (¯1↑word) works?

    Here's a much more convoluted way, because everything is difficult in this language:

        ,/((⊂∘⍕∘⍴∘⊃@2)⊢⊂⍨((1@2)1,⍨¯1↓1⌷(↑(1,⊂))))'accessibility'
        a11y
    

    a frustratingly roundabout way to build up the boolean vector:

        'accessibility'
         1100000000001   ⍝ for penclose
    

    without counting the length first.

    (Is there a way to drop from the middle of an array? "delete index 4 5 6"? Or to insert into the middle of an array? "Insert between elements 2 and 3"?)

    • "Everything is difficult in this language" made me smile. :) I mostly agree. There are things things that APL / J make amazingly easy, too, but a lot of simple things are just brutal to me. I'm not a hardcore APL programmer -- I've played with J more than APL, but I'm still a novice at both. I just enjoy the mental challenge sometimes.

      To insert items from B into A at point N, you could (in pseudocode, I don't have time to play with APL right now!):

      (Take N of A) , B , (Drop N of A)

      To remove items from the middle of an array between N and M, I can think of two ways (I might have off-by-one errors here):

      - (Take N of A) , (Drop M of A)

      - Bools / A

      where "Bools" is an array of 1/0 values, and the 1's indicate the elements to keep. As a dyadic verb, "/" means "compress", that is, to keep only the indicated elements from the right-hand object.

      I'm sure there are other ways! These are the few ways that my limited brain could come up with. :)

      5 replies →

Why can I not resist these urges? Here it is in J, which is basically APL but in ASCII:

       ({.,(":@(_2+$)),{:) 'accessibility'
       -->  a11y