Codeabbey -- Median of three

Oforth examples. Feel free to post your own code.

Codeabbey -- Median of three

Postby sotolf » 11 Aug 2015 21:03

A quite light task find the medium of three and three lines. I was trying to be a bit better at factoring out this time, so that my words stay small

Code: Select all
 : getLn { System.Console askln }               // ( <stdin> -- string )
: getNumber { getLn asInteger }                // ( <stdin> -- integer )
: getInput { [] getNumber #[ getLn + ] times } // ( <stdin> -- [string] )
: parse { words #asInteger swap map }          // ( string -- [integer] )
: 3median { sort 2 swap at }                        // ( 3[integer] -- median )
: pprint { #. swap apply printcr }             // ( [a] -- <stdout> )

getInput printcr #[ parse 3median ] swap map pprint


Pretty proud of this one, since it feels a lot easier to read it than my other attempts at oforth.

just for fun, the same with variables

Code: Select all
: getLn { System.Console askln }               // ( <stdin> -- string )
: getNumber { getLn asInteger }                // ( <stdin> -- integer )
: getInput { | cases result | getNumber ->cases #[ result getLn + ->result] times(cases) }
: parse(inStr) { inStr words map(#asInteger) }
: 3median(inList) { inList sort at(2) }
: pprint(list) { list apply(#.) printcr }

getInput printcr map(#[ parse 3median ] ) pprint
sotolf
 
Posts: 55
Joined: 30 Jul 2015 15:53

Re: Codeabbey -- Median of three

Postby Franck » 12 Aug 2015 11:58

No suggestions :)

Well done.

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


Return to Oforth examples

Who is online

Users browsing this forum: No registered users and 1 guest

cron