Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 String class
Index -> Programming, Turing -> Turing Submissions
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Thu Dec 14, 2006 1:17 pm   Post subject: (No subject)

Imagine:

code:


    %removes all whitespace from a word
    fcn as_string_without (predicate : function f (ch : string) : boolean) : string
        if original = "" then
            original := value
        end if
        var oldword := value
        var newword := ""
        if oldword ~= "" then
            for i : 1 .. length (oldword)
                if not predicate (oldword (i)) then
                    newword += oldword (i)
                end if
            end for
        end if
        result newword
    end as_stripped


Then to strip all punctuation:

code:
new_str := my_str -> as_string_without (is_punctuation)
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Dec 14, 2006 1:19 pm   Post subject: (No subject)

Oh, and ericfourfour, when your string class has a method that returns a string, you should probably have it return a String object. Smile
Clayton




PostPosted: Thu Dec 14, 2006 1:22 pm   Post subject: (No subject)

I get it Very Happy it makes so much more sense now. This way you can just pass the function and away you go Razz
ericfourfour




PostPosted: Thu Dec 14, 2006 4:51 pm   Post subject: (No 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. Smile

What? Did I miss something?
wtd




PostPosted: Thu Dec 14, 2006 11:45 pm   Post subject: (No subject)

Well, consider:

code:
    %removes all whitespace from a word
    fcn as_stripped : string
        if original = "" then
            original := value
        end if
        var oldword := value
        var newword := ""
        if oldword ~= "" then
            for i : 1 .. length (oldword)
                if oldword (i) ~= " " then
                    newword += oldword (i)
                end if
            end for
        end if
        result newword
    end as_stripped


It would seem consistent to have this method, as a single example, return a String object, rather than a value of type "string".
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 20 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: