6 ms·
You've reproduced the most trivial example, which many languages make easy. I would be very interested to see another language with a rank operator, for instan
by moonchild 6y ago
You've reproduced the most trivial example, which many languages make easy. I would be very interested to see another language with a rank operator, for instance.
------------------------------------------------------------------------
Challenge for you: rewrite a nontrivial program in one of those frameworks, with the following restrictions:
- No iteration (including implicit iterations—map, filter; reduce is ok)
- No loops whatsoever. Recursion is ok, but should be avoided wherever possible.
- No explicitly named arguments; everything in pointfree style.
(I know, map/filter can be implemented recursively. But compared with the equivalent constructs in apl, they're about 10× as verbose, and harder to reason about and understand. Even reduce is somewhat of a gimme.)
- ggrrhh_ta 6y agoI have want to chime in. Agree, it is great that ideas from APL have made it into the language... But there is, fortunately, a long and fruitful path still to explore... I love that J is like an alternative path to functional approaches like Haskell and how it plays in terms of providing expressive power. Still, I will not create code in J that is maintained jointly with other users from other domains (which is the case usually when you are using numpy, for example)
- TeMPOraL 6y agoI like how APL made it into Common Lisp wholesale. There's a library that compiles APL to Common Lisp, allowing you to mix APL and Lisp code, working on the same data structures with both languages. https://github.com/phantomics/april https://github.com/phantomics/april Example session I just did, copying APL code from https://aplwiki.com/wiki/John_Scholes%27_Conway%27s_Game_of_Life https://aplwiki.com/wiki/John_Scholes%27_Conway%27s_Game_of_...: CL-USER> (april:with-april-context ((:space *lifespace*)) (april:april "Show←{'·⌺'[⎕IO+⍵]}") (april:april "glider←3 3⍴1 1 1 1 0 0 0 1 0") (april:april-f "Show glider") (print (april:april "glider")) ; to prove it's a Lisp array (terpri) (april:april "grid←¯10 ¯10↑glider") (april:april-f "Show grid") (terpri) (april:april "life ← {⊃1 ⍵ ∨.∧ 3 4 = +/ +⌿ ¯1 0 1 ∘.⊖ ¯1 0 1 ⌽¨ ⊂⍵}") (terpri) (april:april-f "Show¨{life⍣⍵⊢grid}¨0,⍳3") (terpri)) ⌺⌺⌺ ⌺·· ·⌺· #2A((1 1 1) (1 0 0) (0 1 0)) ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·······⌺⌺⌺ ·······⌺·· ········⌺· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ·········· ········⌺· ·······⌺⌺· ·······⌺⌺· ·······⌺⌺⌺ ·······⌺⌺· ·······⌺·⌺ ······⌺⌺·· ·······⌺·· ·······⌺·⌺ ·······⌺·· ········⌺· ········⌺· ·········· ·········· ·········· (APL being compiled to Lisp is important, not just because it gives you proper interoperability out of the box, but also because on implementations like SBCL this means APL code will get compiled, through Lisp, to native code. And you can do that at runtime too.) Now I wish I could find something that does the same for J, because I'm sure as hell not going to buy an APL-friendly keyboard.
- icen 6y agoEmacs has an excellent APL keyboard implementation (via gnu-apl-mode) which uses . as a prefix for the apl keys.
- moonchild 6y ago> APL being compiled to Lisp is important, not just because it gives you proper interoperability out of the box, but also because on implementations like SBCL this means APL code will get compiled, through Lisp, to native code Except that simd in lisp isn't great and needs to be done mostly by hand[1]; and simd is super important for making apl performant. Also because gc attributes that are good for lisp aren't great for apl. Also because compiling to native code doesn't actually really affect performance of apl, and might actually harm it for larger programs. To be clear, april is a super-cool project, but not because of any of its performance characteristics. > Now I wish I could find something that does the same for J, because I'm sure as hell not going to buy an APL-friendly keyboard. You don't need a special keyboard, just a layout. I don't know how it works on windows (there's an 'IME', I think?) or macos, but x comes with a keyboard layout. I use this: setxkbmap -layout us,apl -variant dyalog -option grp:rwin_switch Which makes it so that holding down the right super key and pressing any key produces an appropriate apl symbol. You may want to use a different key to switch; check 'man xkeyboard-config' and then search for 'Switching to another layout' for other options. Try: modifier key (whatever it is)+r, for ⍴ (rho). 1. https://www.reddit.com/r/lisp/comments/fkfgjn/sbcl_with_simd_how_to_optimize_sseavx2_to_pointer/ https://www.reddit.com/r/lisp/comments/fkfgjn/sbcl_with_simd...
- ggrrhh_ta 6y agoRegarding the keyboard, I use something like this https://unix.stackexchange.com/questions/547705/finding-apl-symbols-on-the-keyboard https://unix.stackexchange.com/questions/547705/finding-apl-... and well, a picture of the mappings, but somehow they kind of end sticking in your head...
- ggrrhh_ta 6y agoBeautiful that it can get actually compiled... I dreamt of that, but in particular, with J, it'd be very hard as the actual implementation is data driven, i.e., lexical analysis alone would require building a kind of interpreter for chained operations to work efficiently under all circumstances or compile all possible variants of execution.
- SuperCuber 6y agoRank Is actually implicitly done by numpy using a mechanism called broadcasting. For example: >>> np.array([10, 20, 30]) + np.array([[1,2,3], [4,5,6], [7,8,9]]) array([[11, 22, 33], [14, 25, 36], [17, 28, 39]]) Sieves exist in numpy, called masks: >>>np.array([10, 20, 30]) > 15 array([False, True, True]) Of course they can be operated on just like any other numpy array. Grades exist in numpy: >>>np.array([5,4,3,2,1]).argsort() array([4, 3, 2, 1, 0]) Of course they are a little more verbose since all of those operations are from the library and not native to python.
- ipsum2 6y agoTo do sieves in J like the blog post mentioned, the equivalent numpy would be: >>> arr = np.array([10, 20, 30]) >>> arr[arr > 15] array([20, 30])
- moonchild 6y ago> Rank Is actually implicitly done by numpy using a mechanism called broadcasting. Numpy's broadcasting is scalar conformability, to which the rank operator (and general conformability) provides a general case. Example in j: ] x =. i. 2 3 4 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ] y =. i. 3 4 0 1 2 3 4 5 6 7 8 9 10 11 x + y NB. this will error because + expects that, if its arguments' shapes are not the same, one will be a prefix of the other |length error | x +y NB. this is easy enough to fix, however x +"2 y NB. +"2 is shorthand for +"2 2; meaning, choose rank-2 arrays from both the left and right arguments 0 2 4 6 8 10 12 14 16 18 20 22 12 14 16 18 20 22 24 26 28 30 32 34 Numpy will actually do this without the rank operator, because it uses suffix agreement rather than prefix agreement (which is absolutely bonkers—j used suffix agreement for about 5 minutes in 1990, before realising it was an awful idea). For for numpy, see if you can add: np.array([[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]) + np.array([[0, 1, 2], [3, 4, 5]]) Intelligently. (I'm sure it's not overly difficult to come up with a solution, but can you do it with a single higher-order function call which generalises to other argument shapes?) (The j equivalent, (i. 2 3 4) + (i. 2 3) also works without trouble.) ------------------------------------------------------------------------ Another example, which may be more illustrative, is the ability to perform reductions along arbitrary axes. For example: ] x =. i. 4 3 0 1 2 3 4 5 6 7 8 9 10 11 +/ x NB. sum reduced along leading axis, the default, producing an array of shape 3 18 22 26 +/"1 x NB. sum each rank-1 array (vector); or, reduce last axis, producing an array of shape 4 3 12 21 30 ------------------------------------------------------------------------ Another curiosity, which I have thus far neglected, is the extent to which numpy's being 'a little more verbose' is actually incredibly important in shaping the way you approach and think about problems. The great-uncle comment also addresses this, but Iverson probably says it better than either of us can: read https://www.jsoftware.com/papers/tot.htm https://www.jsoftware.com/papers/tot.htm