Computer Science Canada String class |
Author: | Clayton [ Mon Dec 11, 2006 8:51 pm ] | ||
Post subject: | String class | ||
got bored, so I made a string class, heres the source:
its really quite readable (it almost reads like english ) so you can probably tell what most of these do. please post suggestions, improvements, constructive critisism etc. |
Author: | Hackmaster [ Tue Dec 12, 2006 9:32 am ] |
Post subject: | |
Good Stuff, Freakman...but... can't you do most of this stuff without this class? you can find the length of a word anyways, and I believe that you can to stuff like toupper and tolower work to0 (but that just might be me thinking in C). but the strip function is cool, and repalcing single letters and such.... that's getting pretty sweet. Hey, you are on your way to making FreeWord! keep it up! |
Author: | Clayton [ Tue Dec 12, 2006 1:41 pm ] |
Post subject: | |
sure you can do those things without the class, but the whole point is to make the string an object. As an object, there is so much more freedom and flexibility. |
Author: | ericfourfour [ Tue Dec 12, 2006 3:27 pm ] |
Post subject: | |
If you want to make something very helpful, not that this isn't helpful, make a BigString class. It would remove the limit from the size of strings. |
Author: | [Gandalf] [ Tue Dec 12, 2006 7:56 pm ] |
Post subject: | |
Good idea, ericfourfour. Freakman, not bad, though next time I suggest you do something like learning a new language in your bored time. Also, the benefits of a String class are greatly diminished in Turing thanks to it's largely awkward OOP syntax. |
Author: | Clayton [ Tue Dec 12, 2006 9:45 pm ] |
Post subject: | |
Yes I know, but seeing as we're going to be forced of another year of Turing (doing OOP woohoo!), I figured I might as well try and do something to prepare for it.... I guess I could do something like make a module out of it (ie, like they do with most of the other modules, take a class and manipulate it with a module), but then things could get confusing... |
Author: | wtd [ Thu Dec 14, 2006 4:12 am ] |
Post subject: | |
You may wish to have separate get and get_line methods. Also, may I suggest throwing in something a bit functional and having filtering methods which can taken a predicate function as an argument? |
Author: | Clayton [ Thu Dec 14, 2006 12:21 pm ] |
Post subject: | |
and what might these certain filtering methods do? things like searching through a string for whatever, or something less obvious that I'm missing? EDIT: I'm working on it right now, and now I really, really appreciate dynamic typing, forcing someone to pass a function with the correct parameters is annoying... |
Author: | wtd [ Thu Dec 14, 2006 12:52 pm ] |
Post subject: | |
Imagine you wanted to remove all punctuation from a string. So you write a method to do that. Now you want a method that removes all whitespace. So you write a method to do that. Except that those methods would be identical, save for one thing... the check to see if a character should be removed. So that can be factored out into separate functions, and those functions passed to a single, generic method. |
Author: | Clayton [ Thu Dec 14, 2006 12:54 pm ] |
Post subject: | |
not quite sure what you want me to do here. You're saying I should have a generic method that does all of the work, with other functions to be passed to it to specify exaclty what is needed? |
Author: | rdrake [ Thu Dec 14, 2006 12:56 pm ] |
Post subject: | |
Freakman wrote: and what might these certain filtering methods do? things like searching through a string for whatever, or something less obvious that I'm missing?
As wtd said below me, there can be slight differences between methods. This in Ruby is usually represented by adding either "!" or "?". For example "chomp" and "chomp!". There are only slight differences between them (one modifies the original value, one does not... but both return the modified value regardless), but they are still slightly different.EDIT: I'm working on it right now, and now I really, really appreciate dynamic typing, forcing someone to pass a function with the correct parameters is annoying... |
Author: | wtd [ Thu Dec 14, 2006 12:56 pm ] |
Post subject: | |
Yes. All of the code that handles iterating through the string, and creating a new string... it's the same for both methods. The only thing that changes is how you check each character. So that gets factored out into separate functions. |
Author: | Clayton [ Thu Dec 14, 2006 12:59 pm ] |
Post subject: | |
What exactly do you mean by "How you check each character"? Does that mean I should have a function that tells the generic function what to remove from a string? If so, would this function be passed to the function from outside of the class, or internally? |
Author: | wtd [ Thu Dec 14, 2006 1:01 pm ] | ||
Post subject: | |||
Well, to check for punctuation:
|
Author: | Clayton [ Thu Dec 14, 2006 1:05 pm ] |
Post subject: | |
okay, I see where you're going now, so just have said generic method take in that function to check for punctuation, if its punctuation remove it? Or am I still missing something? |
Author: | wtd [ Thu Dec 14, 2006 1:17 pm ] | ||||
Post subject: | |||||
Imagine:
Then to strip all punctuation:
|
Author: | wtd [ Thu Dec 14, 2006 1:19 pm ] |
Post subject: | |
Oh, and ericfourfour, when your string class has a method that returns a string, you should probably have it return a String object. |
Author: | Clayton [ Thu Dec 14, 2006 1:22 pm ] |
Post subject: | |
I get it it makes so much more sense now. This way you can just pass the function and away you go |
Author: | ericfourfour [ Thu Dec 14, 2006 4:51 pm ] |
Post subject: | |
wtd wrote: Oh, and ericfourfour, when your string class has a method that returns a string, you should probably have it return a String object.
What? Did I miss something? |
Author: | wtd [ Thu Dec 14, 2006 11:45 pm ] | ||
Post subject: | |||
Well, consider:
It would seem consistent to have this method, as a single example, return a String object, rather than a value of type "string". |