Skip to content

Instantly share code, notes, and snippets.

@jeremyheiler
Last active August 29, 2015 13:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyheiler/10572988 to your computer and use it in GitHub Desktop.
Save jeremyheiler/10572988 to your computer and use it in GitHub Desktop.
: fib ( a b -- x y ) [ + ] keep swap ;
! My goal was to implement this with produce.
1 1
[ dup 4000000 < ] [ fib dup ] produce
2nip ! Remove the workspace
dup length 1 - swap remove-nth ! Remove the extra value
[ dup odd? [ drop 0 ] when ]
map-sum
@elasticdog
Copy link

Looking good! Sticking with your usage of produce, there are a few built-in words that can clean things up a bit:

dup length 1 - swap remove-nth

...can be replaced with:

1 head*

And:

[ dup odd? [ drop 0 ] when ] map-sum

...can be replaced with:

[ even? ] filter sum

@elasticdog
Copy link

Also, head-slice* would be slightly more efficient compared to head* in this case, since it's modifying the virtual sequence rather than outputting a new sequence, but it won't realistically make a difference for this problem.

@jeremyheiler
Copy link
Author

Thanks! I like how simple it is to replace logical sections of code with different words. After doing these two Euler problems, and you suggesting fixes that would be obvious to me in, say, Clojure, suggests that I am thinking too deeply about the stack. Needless to say, Factor is really messing with my brain, so that's good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment