Skip to content

Instantly share code, notes, and snippets.

@marinuso
Created October 12, 2014 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marinuso/884915fb157c0c102fa8 to your computer and use it in GitHub Desktop.
Save marinuso/884915fb157c0c102fa8 to your computer and use it in GitHub Desktop.
Explanation of APL code.
type←{
''≡0↑⍵:'str' ⍝ if the first zero items are the empty string, this is a string.
⍬≢⍴⍵:'list' ⍝ if it is not a string, but it has more than zero dimensions, it is a list.
⍵=⌈⍵:'int' ⍝ if it is equal to its ceiling, it can be considered an int.
⍝ (there is literally no way to tell the difference between 1 and 1.0 in APL.)
'float' ⍝ if it isn't one of the above, it must then be a float.
}
round←{
0::1 ⍝ if something goes wrong, return 1
∆←10*⍺ ⍝ 10 log left arg
∆÷⍨⍎0⍕⍵×∆ ⍝ multiply right arg by ∆, format with zero decimal places, execute the result, and divide by ∆ again.
}
enumerate←{
0::1 ⍝ if something goes wrong, return 1
(¯1+⍳⍴⍵),¨⊂¨⍵ ⍝ combine each element with its position minus 1.
}
range←{
0::1 ⍝ if something goes wrong, return 1
0<∆←-⍺⍺:⌽∆+⍵(∆∇∇)⍺ ⍝ if the step is negative, build the reverse of the list with the positive step, then reverse that.
⍺<⍵:⍺,⍵∇⍨⍺-∆ ⍝ if ⍺ is smaller than ⍵, return ⍺ followed by the result of building the list with the next step.
⍬ ⍝ if not, return the empty list.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment