Codeabbey -- Vowel count

Oforth examples. Feel free to post your own code.

Codeabbey -- Vowel count

Postby sotolf » 11 Aug 2015 20:43

Just did this one, I'm still not that comfortable with working with strings in oforth, but I'm getting better, I have also been trying to put in some of the improvements Franck has been mentioning in replies to some of my other posts

Code: Select all
: getLn { System.Console askln }   // ( -- string )
: getNumber { getLn asInteger }    // ( -- integer )
: isVowel { "aeiouy" include }     // ( char -- f )
: solve { []
    getNumber #[ getLn #isVowel swap filter
    size + ] times }               // ( -- list)
: pprint { #. swap apply printcr } // ( list -- <stdout> )

solve printcr pprint


I could have saved myself 2 swaps using variables, but it would result in longer code if I would use descriptive names, then again I could have saved some in comments, but stack charts are always nice to have, so I'm not sure it's all for the better ;) at least that's how I chose to solve it. The input in this specific problem was consisting of all lower case letters, so I didn't have to downcase, but it wouldn't be much of a problem to do that in addition. Just applying #toLower on the strings as I get them.
sotolf
 
Posts: 55
Joined: 30 Jul 2015 15:53

Re: Codeabbey -- Vowel count

Postby Franck » 12 Aug 2015 11:56

You are doing well with strings.

May I suggest 2 things (tell me if you have enough of those suggestions... :) )

1) You have a method on collections, #count(p), that counts number of elements that return true when p is applied.

2) It is just presentation, but I think it could be interesting to separate code that resovle the point from code used to test.

For instance, if order to count number of vowels into a string :
Code: Select all
: isVowel { "aeiouy" include }                                   // ( char -- true | fasle )
String method: nbVowels { #isVowel self count }         // ( string -- n )


Then code for testing : something you want to test from stdin, sometimes directly into the interpreter. For instance :
Code: Select all
>"abcedfghijklmn" nbVowels .
3
>[ "abcd", "efgh", "ijkj" ] map(#nbVowels) sum .
3


Of course, this is just a suggestion. Please do it as you think it is better or used to.

Franck
Franck
 
Posts: 144
Joined: 29 Oct 2014 19:01

Re: Codeabbey -- Vowel count

Postby sotolf » 12 Aug 2015 15:30

May I suggest 2 things (tell me if you have enough of those suggestions... :) )


I love the suggestions, for each of them I feel that I'm getting better for every little piece of advice that I get, and I learn new things about the language, so don't worry about that.

2) It is just presentation, but I think it could be interesting to separate code that resovle the point from code used to test.

Yeah, sounds like a good idea, factoring stuff out as much as possible is almost always a good idea from what I've seen so that sounds good.
sotolf
 
Posts: 55
Joined: 30 Jul 2015 15:53


Return to Oforth examples

Who is online

Users browsing this forum: No registered users and 1 guest

cron