Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by stvn (Monsignor)
on Dec 12, 2006 at 15:27 UTC ( #589295=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)

Because I really have come to hate for loops, here is one in Javascript which uses a fixed point combinator to do the looping and filtering of the list.

function (orig, i, acc) { if (i > orig.length) return acc; if (orig[i] != "") acc[acc.length] = orig[i]; return arguments.callee(orig, (i + 1), acc); }(" one two three four ".split(" ").reverse(), 0, []).join(" ")

-stvn

Replies are listed 'Best First'.
Re^2: Five Ways to Reverse a String of Words (C#, Perl 5, Perl6, Ruby, Haskell)
by jdporter (Canon) on Dec 12, 2006 at 15:50 UTC

    Huh?

    " one two three four ".split(/ /).reverse().join(" ")
    We're building the house of the future together.

      Actually that doesn't work correctly in FF or Safari on OS X. It still has several empty strings filling up the array. They become more evident if you do this:

      " one two three four ".split(/ /).reverse().join("|")
      which gives you "||||four|three|two|||one||".

      -stvn

        Interesting... It works fine in EcmaScript. Or at least Jscript. :-)
        So how about

        " one two three four ".split(/ +/).reverse().join(" ")
        We're building the house of the future together.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://589295]
help
Chatterbox?
[pryrt]: Is there a way to search for/list "nodes I've voted on"? (I thought there was, but cannot find it now)
[choroba]: Your votes in your profile?
[ambrus]: pryrt: yes, there is. link "Your votes" from your profile page.
[ambrus]: (That link work two ways: from your own profile page, it lists all your votes; from anyone else's profile page, it lists your votes on that user's nodes only.)
[choroba]: Wow, and I can create an interesting statistics from it
[LanX]: I've also added a link "my votes" to the personal nodelet
[pryrt]: Thanks, that was what I was looking for

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2017-03-29 13:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (351 votes). Check out past polls.