sum two numbers
- Code: Select all
: input { "6646544 7106685" }
: solve { input words map(#asInteger) sum }
solve println
This was basically just a warmup, that shows parsing of a string to integer, and not much else.
sum of pairs
- Code: Select all
: getFile(fileName) { File new(fileName) dup open(File.READ) }
: getCount(file) { file readLine asInteger }
: getLines(file, count) {
[]
#[ file readLine + ] times(count)
}
: getInput { "sums-in-loop" getFile dup getCount swap getLines }
: sumEachLine { map( #[ words map(#asInteger) sum ] ) }
getInput sumEachLine apply(#.) "" println
This was before I got the quick fix for stdin input, and shows reading from a file, Apart from that not much new. It also shows basic use of the functional words on lists.
min-max array
- Code: Select all
: getArray { System.Console askln }
: parse { words #asInteger swap map }
: min-max { dup #min swap reduce swap #max swap reduce }
getArray parse min-max . . "" println
This shows at least I think so, how compact and nice to read oforth can be.
integer division with rounding
- Code: Select all
: /round { over over mod rot + swap / }
: getLn { System.Console askln }
: get-number { getLn asInteger }
: get-lines { [] swap #[ getLn words #asInteger swap map dup first swap second /round +] times }
: pprint { #[ print " " print ] swap apply "" println }
get-number get-lines pprint
I like this, mostly becuse I was finding out how to do integer division with integers and rounding, without having to resort to floats.
I really enjoy oforth, and I hope some more examples of it around will let some more people see it, and become interested in it.