ToDo/FinnAplIdioms
Suggestions from the FinnAPL phrase book
I had a quick look through the FinnAPL Idiom list (http://aplwiki.com/FinnAplIdiomLibrary). Here are some of the more common idioms which we might want to add to the PhraseBook. This is just a suggested starting point, idioms which every APL programmer should know. There may be some which you disagree with, and there are bound to be some which I missed out.
The FinnAPL Idiom List predates APL2, so there may be a better way of doing some of these. Note also that some of the idioms are currently dependent on ⎕IO being 1, which conflicts with the standard used on the PhraseBook.
Please feel free to edit the list, add comments, etc. Or just pick a useful looking one and implement it! -- SimonMarsden 2010-08-19 13:44:36
Good idea.
3 points.
I think we should be very selective given the number of entries in there that we do phrases that actually are useful, perhaps because we have used them ourselves; subjective I know but how else?
To reinforce Simon's point re. ⎕IO we should force each selected phrase to be either ⎕IO independent (preferably) or assume ⎕IO=0 (and probably note it anyway) or specify clearly that it only works with ⎕IO=1 and cannot be made to work otherwise. I personally prefer X[?1] and X[1+?1] to X[⎕IO] and X[1+⎕IO]but that's because I try to avoid quad vars because they don't look nice. I shan't be editing others entries on that point.
We don't need to replicate similar phrases on separate pages, the second and third of these, for instance, as they can be under
Examples
on the page of the first general phrase:
Idiom |
(10*-X)×⌊0.5+Y×10*X |
Idiom |
0.01×⌊0.5+100×X |
Idiom |
⌊0.5+X |
-- PhilLast 2010-08-19 15:35:55
I disagree. This is the kind of cleverness that's fine for old-hands (or maybe it's not even for them) but a stumble stone for newcomers. It's not obvious why someone is using an expression like (?1)⊃array, and it should. For that reason we should avoid that and go for the ⎕IO expression. Maybe that ugly, but it's also obvious. -- KaiJaeger 2010-08-20 11:30:05
Idiom |
⍋⍋X |
Description |
Ascending cardinal numbers (ranking, all different) |
From |
FinnAPL Idiom 19 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
⍋⍒X |
Description |
Descending cardinal numbers (ranking, all different) |
From |
FinnAPL Idiom 25 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
X[⍋X]^.=⍳⍴X |
Description |
Test if X is a permutation vector |
From |
FinnAPL Idiom 29 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
X[⍋X+.≠' ';] |
Description |
Sorting words in list X according to word length |
From |
FinnAPL Idiom 31 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
X[(⍋X)[⌈.5×⍴X]] |
Description |
Median |
From |
FinnAPL Idiom 36 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
((1+⌊Y⍟X)⍴Y)⊤X |
Description |
Representation of X in base Y |
From |
FinnAPL Idiom 96 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
((1+⌊10⍟X)⍴10)⊤X |
Description |
Digits of X separately |
From |
FinnAPL Idiom 97 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
100⊥10000|3↑⎕TS |
Description |
Encoding current date |
From |
FinnAPL Idiom 82 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
⍉(10000 100 100)⊤X |
Description |
Separating a date YYMMDD to YY, MM, DD |
From |
FinnAPL Idiom 104 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
0 1⊤X |
Description |
Integer and fractional parts of positive numbers |
From |
FinnAPL Idiom 111 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
1+(X<0)+⌊10⍟|X+0= |
Description |
Number of digit positions in integers in X |
From |
FinnAPL Idiom 116 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
X×○÷180 |
Description |
Conversion from degrees to radians |
From |
FinnAPL Idiom 169 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
X×180÷○1 |
Description |
Conversion from radians to degrees |
From |
FinnAPL Idiom 170 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
(G⍳X)<G⍳Y |
Description |
Alphabetical comparison with alphabets G |
From |
FinnAPL Idiom 181 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
(2=+⌿0=(⍳X)∘.|⍳X)/⍳X |
Description |
All primes up to X |
From |
FinnAPL Idiom 224 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
X∘.+,0 |
Description |
Reshaping numeric vector X into a one-column matrix |
From |
FinnAPL Idiom 245 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
(⍳X)∘.=⍳X |
Description |
X×X identity matrix |
From |
FinnAPL Idiom 286 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
(X,X)⍴1,X⍴0 |
Description |
X×X identity matrix |
From |
FinnAPL Idiom 686 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
(⍳X)∘.≤⍳X |
Description |
X×X upper triangular matrix |
From |
FinnAPL Idiom 255 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
(⍳X)∘.≥⍳X |
Description |
X×X lower triangular matrix |
From |
FinnAPL Idiom 267 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
+/X∘.≥0 50 100 1000 |
Description |
Which class do elements of X belong to |
From |
FinnAPL Idiom 266 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
X<.<Y |
Description |
Test if X is within range [ Y[1;],Y[2;] ) |
From |
FinnAPL Idiom 299 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
X<.≤Y |
Description |
Test if X is within range ( Y[1;],Y[2;] ] |
From |
FinnAPL Idiom 300 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
<\X |
Description |
First one (turn off all ones after first one) |
From |
FinnAPL Idiom 348 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
≤\X |
Description |
Not first zero (turn on all zeroes after first zero) |
From |
FinnAPL Idiom 350 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
∨\X |
Description |
Not leading zeroes (turn on all zeroes after first one) |
From |
FinnAPL Idiom 365 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
^\X |
Description |
Leading ones (turn off all ones after first zero) |
From |
FinnAPL Idiom 377 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
×/⍴X |
Description |
Number of elements (also of a scalar) |
From |
FinnAPL Idiom 420 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
⌈/⍳0 |
Description |
Negative infinity; the smallest representable value |
From |
FinnAPL Idiom 428 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
⌊/⍳0 |
Description |
Positive infinity; the largest representable value |
From |
FinnAPL Idiom 434 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
(1*⍴X)⍉X |
Description |
Major diagonal of array X |
From |
FinnAPL Idiom 512 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
Y[1]⌈Y[2]⌊X |
Description |
Limiting X between Y[1] and Y[2], inclusive |
From |
FinnAPL Idiom 520 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
X=⌊X |
Description |
Test if integer |
From |
FinnAPL Idiom 533 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
(10*-X)×⌊0.5+Y×10*X |
Description |
Rounding to X decimals |
From |
FinnAPL Idiom 536 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
0.01×⌊0.5+100×X |
Description |
Rounding to nearest hundredth |
From |
FinnAPL Idiom 537 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
⌊0.5+X |
Description |
Rounding to nearest integer |
From |
FinnAPL Idiom 538 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
(0=Y|⍳⍴X)/X |
Description |
Taking every Y:th element of X |
From |
FinnAPL Idiom 546 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
0∊0\0⍴X |
Description |
Test if numeric |
From |
FinnAPL Idiom 565 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
(X-⎕IO)+⍳1+Y-X |
Description |
Consecutive integers from X to Y (arithmetic progression) |
From |
FinnAPL Idiom 658 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
0∨X |
Description |
Demote non-boolean representations to booleans |
From |
FinnAPL Idiom 661 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
X,[1.1]Y |
Description |
Forming a two-column matrix |
From |
FinnAPL Idiom 689 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
X,[.1]Y |
Description |
Forming a two-row matrix |
From |
FinnAPL Idiom 690 |
Alternative |
|
Comments |
|
Status |
To Do |
Idiom |
''⍴X |
Description |
Reshaping first element of X into a scalar |
From |
FinnAPL Idiom 722 |
Alternative |
|
Comments |
|
Status |
To Do |
APL Wiki