I was very slow in implementing ` sort in CoSy because I so wanted ` grade .
Finally , I figured out how to append iota to the list to be sorted to be carried along during the sort , built on Helmar Wodke's terse algorithm , http://cosy.com/4thCoSy/Code/CoSy/sort.f . ( It needs to be revisited because it is not order preserving for equals . )
I've found no way to get the ` grade `w/o doing the sort . If there is , please enlighten me .
So I agree ` sort should have been and be a primitive . I'm surprised the inefficiency of having to go around the grade > index ring to retrieve the sort survived the early years of APL .
I'm taking the liberty of showing the coded for ` nub and ` group which are at about line 1580 in http://cosy.com/4thCoSy/Code/CoSy/CoSy.f .
Code: Select all
| return unique items . Like K's ' ?
: nubb 1p> R@ ['] where 'R ,/ R@ rho iota =i 1P> ; | bool of 1st occurances
: nubx nubb & ; | indices of 1st occurences
: nub 1p> dup nubx at 1P> ; | unique items .
: nnub 1p> dup nubb 0=i & at 1P> ; | redundants
| Group items by uniques | see Wed.Oct,20191002 | https://youtu.be/fxx9kS_seXk |
: grpib 1p> nub R@ ,/ ['] =i 'L 1P> ; | group integer bool
: grpix grpib ['] & 'm ; | group integer index
: grpcx >a> nub a> swap ' conn 'R ;
| 20191008 | ` nubgrp optimized merge . Eliminates redundant call of ' nub
: nubgrpib 1p> nub >a> R@ ,/ ['] =i 'L a> swap ,L 1P> ; | nub & group integer bool
| and uses CoSy's access to addresses using ' ix rather than just ' i@ and ' i!
| to optimally replace just the second item in the result from ' nubgrpib
: nubgrpix nubgrpib >a> 1 ix dup @ ['] & 'm swap rplc a> ;
: nubgrpcx >a> nub a> swap >a> ' conn 'R a> swap ,L ; | 20210625
| /\ | Takes list . Returns 2 item list of `( nub grp )` .
| ( L R -- nub sums ,L )
: aggr nubgrpcx >a> 1th { at +/ } 'R a> dsc swap ,L ;
| aggregate ( sum ) values L by nub of R . | 20210705 |
` nub and ` group are a similar situation where you can't do the ` group w/o doing the nub . So you can save some work by having a word which returns both together .