Code Abbey - Averages

Oforth examples. Feel free to post your own code.

Code Abbey - Averages

Postby sotolf » 09 Sep 2015 09:50

This is another really simple one, get lists of numbers, and get the average over them, the only little complication is that the lists are 0 terminated, which means that we can't use the built in #avg, also they want rounded results, so it's a bit of a casting hassle to get where we want, but it's just a short small script.

Code: Select all
: getLn { System.Console askln }                                                // ( <stdin> -- string )
: getNumber { getLn asInteger }                                                 // ( <stdin> -- integer )
: pprint { apply(#.) printcr }                                                  // ( [a] -- <stdout> )
: getInput { [] getNumber #[ getLn + ] times }                                  // ( <stdin> -- [string] )
: parseLine { words map(#asInteger) }                                           // ( string -- [integer] )
: avgNoZero { dup sum   swap size 1 -   asFloat / round }                           // ( [int] -- int )

getInput map(#parseLine) map(#avgNoZero) pprint


Again franck will probably say that I should use:

Code: Select all
 : avgNoZero(n) {  n sum   n size 1 -   asFloat / round }


to get some locals in there and make it easier to read, at least I have showed that I know this time :) And yeah, it's easier to reason about when you don't have to keep the stack in the head the whole time, to know what values are where, I just find it easier to write without locals, when I'm writing the function I'm thinking about the stack, and what's where, and when it's finished and tested I don't really think about it anymore, but then when you come back the second time, something like the second one is something that you'll be more happy to see I think, because it's at once clear what's meant. I can't seem to make up my mind I think it's something about the brackets that makes me not like the locals, is there any way I can change that for myself to make it look different, like : avgNoZero|n| ; or something? I don't know why I feel so strangely about the locals syntax, I don't even really know what it is that I don't like about it :P

One thing I noticed this time is how much whitespace does for the formatting some times, grouping phrases with 3 spaces makes it a bit easier to parse, and it helps readability quite a lot.
sotolf
 
Posts: 55
Joined: 30 Jul 2015 15:53

Re: Code Abbey - Averages

Postby Franck » 09 Sep 2015 16:05

No, no, I will never say that "you should use" :)

I just show the version with locals when it seems to me that it is an interesting version.

btw, grouping with whitespaces is a very good idea.
Franck
 
Posts: 145
Joined: 29 Oct 2014 19:01

Re: Code Abbey - Averages

Postby sotolf » 09 Sep 2015 17:45

No, no, I will never say that "you should use" :)

Hehe, I didn't mean it like that either. No hard feelings, it's just usually the answer ;)

btw, grouping with whitespaces is a very good idea.


It makes stuff so much clearer :)
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