Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In apl (incl. apl-alikes—kdb, etc.), you can essentially say this directly. 'age > 30' produces an array with the same shape as 'age' with a 1 for instances that are >30 and 0 else. So for instance:

        27 42 44 > 30
   0 1 1
If all you want is a count of teachers older than 30, you can simply sum up the resulting array, so:

         +/0 1 1  ⍝note: '/' is reduction, so +/ is sum reduction
   2

If you want the indices, you can use 'where', which will produce an array of indices corresponding to 1s in its argument, so

         ⍸0 1 1
   1 2
Indicates that '0 1 1' has 1s at indices 1 and 2. (We could also take the length of this array to get the count of teachers older than 30, although that would be a bit silly compared to just summing the original array.)

We can then use these indices to index a different array:

         ages←27 42 44
         names←'anthony' 'barbara' 'bert'
         names[1 2]
    barbara  bert
         names[⍸ ages>30]
    barbara  bert
However, we don't even need to use 'where' in this case; the reduction operator (/) has a second use which lets us say:

         0 1 1 / names
    barbara  bert
         (ages > 30) / names
    barbara  bert
Good luck with your database, and hope this helps!


Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: