• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

1000 characters  RSS feed

 
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is valid Groovy though (I think):
 
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this?



the * operator is overloaded to repeat the String.
[ September 22, 2008: Message edited by: Matthew Taylor ]
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh. My.

That rocks!
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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...?
 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about it gives you pause?
 
Ranch Hand
Posts: 3091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!