Hacker News new | ask | show | jobs
by Avshalom 4034 days ago
if it makes you feel any better in Dyalog APL[1] I'd write that as:

  s←{
  	sp←⍵[⍋ ⍵]
  	h t←(1↑sp) (1↓sp)
  	h , t[⍋ 12○(t-h)]
  }
  or I can make it ugly as hell:
  s←{h,t[⍋ 12○(t←1↓p)-h←1↑p←⍵[⍋ ⍵]]}
In Dyalog (and most APLs now) ←{...} creates function that automatically has ⍵ as it's left argument (so invoking "s points" ⍵ is points )

⍋ give the indexes in sorted order and [ ] is how you access elements so ⍵[⍋ ⍵] is how you sort an array

↑ and ↓ are take and drop so (1↑sp) takes the head and (1↓sp) drops the head leaving the tail "h t←(1↑sp) (1↓sp)" is just multiple assignments

J's verb trains are cute but kind of awful

"h , t[⍋ 12○(t-h)]" sorts the tail according to the value of each elements phase (I agree the circle functions are particularly bad) and cats its back on to the head (also strictly speaking i don't need the parens around "t-h" but I like them).

[1] well no actually I wouldn't because Dyalog's grade up (⍋) function doesn't work on complex numbers (though how to write a function ( gu ) that DOES is covered here http://dfns.dyalog.com/c_le.htm )