Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Syntactic Confectionery Delight
 
PerlMonks  

Re: Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)

by ambrus (Abbot)
on Dec 12, 2006 at 23:06 UTC ( #589445=note: print w/ replies, xml ) Need Help??


in reply to Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)

The J language isn't particularly strong in string manipulation. You can get an easy solution this way:

reverseWords =: (,' '&,)&:>/@:|.@:;: reverseWords ' one two three four ' four three two one
but it sort of feels like cheating because of the use of the ;: verb which breaks a string to J tokens.

A solution without this cheating is (I have replaced the tab character with a hat for legibility):

reverseWord1 =: [:(,' '&,)&:>/@:|.@:-.&a:e.&' ^'<@}.;.1] reverseWord1 ' one two three^four ' four three two one
Another solution is:
reverseWord2 =: [:}.@;@|.e.&' ^'<@(' '&,@}.`(0$])@.(2>#));.1] reverseWord2 ' one two three^four ' four three two one


Comment on Re: Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)
Select or Download Code
Re^2: Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)
by ambrus (Abbot) on Feb 13, 2009 at 20:21 UTC

    Incidentally, reverseWords =: |.&.;: works as well.

Re^2: Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)
by Anonymous Monk on May 22, 2010 at 08:00 UTC

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: note [id://589445]
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (20)
    As of 2013-06-20 07:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How many continents have you visited?









      Results (680 votes), past polls