Codeabbey in oforth

Project Euler problems in Oforth . Feel free to post your own code.

Codeabbey in oforth

Postby sotolf » 09 Aug 2015 10:37

I'm not quite sure if this is the right place to post this, but I'll do anyway, I've been working a bit on codeabbey in oforth, and I was thinking this is the best place to place the code for more people to see, It's probably not good code, as I'm still pretty new, but it worked for me at least.

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.
Posts: 55
Joined: 30 Jul 2015 15:53

Re: Codeabbey in oforth

Postby Franck » 09 Aug 2015 17:29


It is ok here, but perhap's you can move your post into "Oforth source examples" Forum.

Interesting code.

Here are some suggestions :

Text files can be treated like collections ie use #forEach:, #apply, ...
And the file will be automatically opened and closed.

Code: Select all
: getCount(fileName) { 0 File new(fileName) apply(#[ drop 1 + ]) }

: sumEachLine(fileName) { File new(fileName) map( #[ words map(#asInteger) sum ] ) }

If you want to work on each line, you can write something like this :

Code: Select all
: test(fileName)
| line |
   File new(fileName) forEach: line [

min-max array :
Everything is ok and it is mostly a code style, but I use to write :

Code: Select all
: min_max    // ( aList -- u u }
   { dup reduce(#min) swap reduce(#max) }

Otherwise, I think :
- print " " print is function #.
- "" println is function #printcr

So #pprint could be written like this :

Code: Select all
: pprint { apply(#.) printcr }

Thank you for thoses examples and your interest.

Posts: 140
Joined: 29 Oct 2014 19:01

Re: Codeabbey in oforth

Postby sotolf » 09 Aug 2015 19:15

Hmm, can I move my posts myself? It makes sense that it fits better there yeah, of course I can delete my post and post it again, but then your excelent reply will be gone as well :)

Cool, I wasn't aware that I could treat the files as collections, that's a really nice thing, and so much less complicated than what I was doing :)

And the different print functions makes it a lot cleaner.

I guess I can treat strings as collections as well then, so that I can count different letters in a word and stuff too, that makes other things easier. I'm really enjoying learning about this language, so many things still that I'm doing a clunky way, then finding something better later, I guess that's something that comes with practice.
Posts: 55
Joined: 30 Jul 2015 15:53

Re: Codeabbey in oforth

Postby Franck » 09 Aug 2015 19:48

It's ok here :)

Yes, strings are collections and all methods on collections work on strings.

I think it is a very good way to learn a language by trying to do something by ourselves, then find out other ways to do it.
And Oforth allows many ways to write something ...

This comes with practice, yes, but this is also my fault : I should take some time to write some other tutorials and documentation, but I have so many things to do between each Oforth release that I don't take this time. Sorry for that...

I didn't know about codeabbey site. This is an interesting site. I will subscribe (when I have time :) ).

Posts: 140
Joined: 29 Oct 2014 19:01

Re: Codeabbey in oforth

Postby sotolf » 10 Aug 2015 07:05

That's cool :)

That's one of the nice things to me, it feels like some kind of perverse mix between perl and python that way, there is many ways to do something, but one is very ideomatic and nice to work with ;)

Well, yeah, I know what you mean with the free time, with full time work and a family there's already so few hours of the day that I have to do small projects, and even more when it comes to a language it's so much more fun writing implementations than it is to document it, I'm having fun finding out what I can do with it and how to though, and the documentation can also be extended later.

Codeabbey is nice, I found it in a reddit thread after project euler went down again, it's so nice that you can solve the challenges in whatever language you want to, that's what really puts it over the others for me, I also really like hackerrank, but it still doesn't support all the language testing I do, now I'm kind of stuck on oforth for a while though, but I don't think they'll support it in the near future ;)
Posts: 55
Joined: 30 Jul 2015 15:53

Return to Project Euler in Oforth

Who is online

Users browsing this forum: No registered users and 1 guest