wood burning stoves 2.0*
The moose likes Groovy and the fly likes 1000 characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Languages » Groovy
Bookmark "1000 characters" Watch "1000 characters" New topic
Author

1000 characters

Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

I need a test String with 1000 characters. I'm lazy and want to build it off of a String of '0123456789'.

What's the best way? The code should be easy to read and modify and it should be fast.

Here's a few different ideas to start with. Can you come up with others?

We can discuss which ones we like best and then time them head to head.





A good workman is known by his tools.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I got one...

+1 because it's easy to read

-5 because it's not in Groovy


[ September 22, 2008: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
This is valid Groovy though (I think):
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
How about this?



the * operator is overloaded to repeat the String.
[ September 22, 2008: Message edited by: Matthew Taylor ]

Grails Consultant
http://dangertree.net
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Oh. My.

That rocks!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Matthew Taylor:
How about this?



the * operator is overloaded to repeat the String.



Am I the only one who thinks that this is just wrong...?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
Am I the only one who thinks that this is just wrong...?

Probably not :-) But the idea isn't new - in Perl it would be


Ping & DNS - my free Android networking tools app
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Am I the only one who thinks that this is just wrong...?


Works the same way in Scala also. In Scala it's just a plain ol' method call so it shows up in the regular scaladocs. I don't have a problem with it.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
What about it gives you pause?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2982
    
    9
Works the same in Ruby too.

It also makes sense in that

    "foo" * 3

is the same as

    "foo" + "foo" + "foo"

As long as the + operator has been overloaded, this overloading of * seems consistent.

However in all these languages, 3 * "foo" results in an error. So * is not symmetric when applied between numbers and strings. Then again, + isn't symmetric either:

    "a" + "b"

is not the same as

    "b" + "a"

And furthermore

    "foo" + 1 + 2

evaluates differently then

    1 + 2 + "foo"

- even in Java.
[ September 24, 2008: Message edited by: Mike Simmons ]
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
In Groovy, this is done with operator overloading. Meaning that I can write my own class that can overload the '*' operator. For example...



I have provided two multiply() method implementations that overloads the '*' operator. When Groovy sees a '*' in the code, it looks for a method on the preceeding object called 'multiply'. In many cases, this is Number, so the Number.multiply(Number operand) method is called. If it is not a Number, but a Cat, Groovy will still find the right multiply method if you have provided it.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

A slight variation:

Which now lets me easily create an army of waaaar kittens!

I have an army of 1000 all named Destroyer
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Does the Groovy syntax of:

multiply => *

extend to other symbols:

divide => /
subtract => -
add => +
etc...

Are there any non-math related symbols that can be defined by this convention?
[ September 25, 2008: Message edited by: Garrett Rowe ]
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
Originally posted by Garrett Rowe:
Does the Groovy syntax of:

multiply => *

extend to other symbols:

divide => /
subtract => -
add => +
etc...

Are there any non-math related symbols that can be defined by this convention?

[ September 25, 2008: Message edited by: Garrett Rowe ]


Yes. Take a look at the Groovy JDK for List.

The subscript operator []:
myList[0] <====> myList.getAt(0)
myList[1..2] <====> myList.getAt(1..2)
myList[0] = 2 <====> myList.putAt(0, 2)
etc... (there are many more overloaded putAt() methods)

Leftshift <<:
myList << 4 <====> myList.leftShift(4)

Minus -:
myList - 3 <=====> myList.minus(3)

This is just an one example.

Here is the full list of operators supported by Groovy and the methods they map go:

[ September 26, 2008: Message edited by: Matthew Taylor ]
 
jQuery in Action, 2nd edition
 
subject: 1000 characters
 
Similar Threads
Output from synchronization ....
for loop
Is It Possible To Set Input Numbers for JTextFiled?
Leading Zeroes disappearing whilst hashing
Interview String question