3 ms·
Looking at Advent of Code solutions, I'm not sure if APL is really so much shorter or if it is rather a styling of programming. Some day2 solutions from reddit
by qznc 7y ago
Looking at Advent of Code solutions, I'm not sure if APL is really so much shorter or if it is rather a styling of programming.
Some day2 solutions from reddit [0]:
APL:
0{⍺⋄s←⍵⋄c←s[f+⍳4]⋄99=0⌷c:0⌷s⋄i a b l←s[c]⋄s[3⌷c]←(0⌷c-1)⌷a(+,×)b⋄(⍺+4)∇s}{j←⍵⋄j[1 2]←12 2⋄j}⍎¨','(≠⊆⊢)(⎕NGET 'data')
Python:
def R(I,x=12,y=2,O=__import__('operator')):
q=p=[*map(int,I.split(','))];p[1:3]=x,y
while p[0]!=99:o,a,b,c,*p=p;q[c]={1:O.add,2:O.mul}[o](q[a],q[b])
return q[0]
R(inp),next(100*x+y for x in range(100)for y in range(100)if R(I,x,y)==19690720)
K (APL derivative)
f:** ({~99=x@y}/) {i:x@y+!4;n:@[x;i@3;$[1=*i;+;*]/x@i@1 2];(n;y+4)}//
d1:@[.-1_1:`2.txt;1 2;:;12 2]
f(d1;0)
d2:{@[d1;1 2;:;x]}'z:+!2#100
{y+100*x}/*z@&19690720=f'+(d2;0)
Another APL one:
⎕IO←0
s←⍎¨','(≠⊆⊢)⊃⊃⎕NGET'p02.txt' 1
f←{i←0⊣p[1 2]←⍵⊣p←s ⋄ ⊃p⊣{i+←4⊣a b c d←p[i+⍳4] ⋄ p[d]←(a-1)⌷p[b](+,×)p[c]}⍣{99=i⌷p}⊢0}
f 12 2 ⍝ part 1
+/100 1×∊{0::⍬ ⋄ 19690720=f⍵:⍵ ⋄ ⍬}¨1+,⍳99 99 ⍝ part 2
Ruby:
def exec_op v { 1 => method(define_method(:a) {|x,y,z,m| m[z] = m[x] + m[y]}), 2 => method(define_method(:a) {|x,y,z,m| m[z] = m[x] * m[y]}) }[v] || method(define_method(:empty) {|a,b,c,d| nil }) end
def run_program i i.each_slice(4).to_a.map {|p| exec_op(p[0]).call(p[1], p[2], p[3], i)} end
[0] https://old.reddit.com/r/adventofcode/comments/e4u0rw/2019_day_2_solutions/ https://old.reddit.com/r/adventofcode/comments/e4u0rw/2019_d...
- 7thaccount 7y agoI think it makes better sense to compare how APL looks in the wild versus Python/Ruby/Java whatever. Rosetta Code shows this pretty well. There are dozens of examples where the APL program is one line and the Python/Perl/Ruby/C#/Whatever is ~1/3 page if I recall correctly. So that might prove your point better now that I think about it lol. Python can be written like the user did above, but I've never written it that way or seen anybody at work that has. Most of the APL I've seen (except for the really old school kind) looks like oneliners.
- Athas 7y agoActually, my experience is that APL in the wild (as practiced by those who are just trying to do their job) is not very terse at all. It looks like an old-school procedural language. Since APL is an incredibly proprietary community, it can be a bit hard to find public examples of "industrial" APL style, but this looks similar to what I remember is written at places like SimCorp: https://github.com/Dyalog19/SA3/blob/953e591eace72bb1c147d1914734a78d7af50baf/DUI/Core/JSON.dyalog https://github.com/Dyalog19/SA3/blob/953e591eace72bb1c147d19...
- deleted 7y ago[deleted]
- kick 7y agoThat's really bad k. Try Arthur Whitney's solutions from a few years back (see ad.k): http://www.kparc.com/advent/ http://www.kparc.com/advent/
- vidarh 7y agoWith a handful of helpers you can get close to APL/J/K in any language. The evidence for that is that the 'famous' first draft implementation of J in C is about a page of code. The difference tends to be more that the APL way of thinking is hard for people to get used to, and so most non - APL'y languages tends to lack methods to support the bits that makes the APL family solutions brief. But adding naive versions of those facilities is easy.