Skip to content

Instantly share code, notes, and snippets.

@smcl
Last active March 27, 2018 10:49
Show Gist options
  • Save smcl/543625663222acc6f30824565b714524 to your computer and use it in GitHub Desktop.
Save smcl/543625663222acc6f30824565b714524 to your computer and use it in GitHub Desktop.
Reversing a string or array in J
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