Last active
March 27, 2018 10:49
-
-
Save smcl/543625663222acc6f30824565b714524 to your computer and use it in GitHub Desktop.
Reversing a string or array in J
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NB. first attempt, took me a whole hour | |
sub1 =: 1 & (-~) | |
subtractindices =: ((# sub1) @: #) | |
forwardindices =: i. @: # | |
revindices =: (subindices - forwardindices) | |
revarray =: (({~) revindices) | |
NB. alternative implementation, I realised you could give a -ve number to "i." | |
revindices2 =: (i. @: - @: #) | |
revarray2 =: ({~) revindices2 | |
NB. final unreadable version (above but smooshed together) | |
revarray3 =: ({~) (i. @: - @: #) | |
NB. bonus, slightly more readable version using explicit parameters | |
revarray4 =: 3 : 0 | |
( (((# y) - 1)) - (i. (# y)) ) { y | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment