-
-
Save jeremyheiler/10015006 to your computer and use it in GitHub Desktop.
USING: kernel math math.functions sequences ; | |
IN: euler1 | |
: euler1 ( x -- y ) | |
iota [ dup [ 3 divisor? ] [ 5 divisor? ] bi or [ drop 0 ] unless ] map-sum ; |
Thanks! Appreciate it.
- Is there a good way to search for things? I'm using the offline help, but I feel like it's hard to find things unless I know exactly what I'm looking for, or am able to know what vocabulary to look in.
- Duh. I'm not sure what I was thinking. It took me a while to find iota, as I was thinking "range" the whole time (a la Clojure). Though, I guess I didn't dive too hard because I didn't see
[1,b]
.
3 & 4. Excellent.
You bet! If you keep posting gists, I'd be glad to review stuff.
The online documentation is a bit easier to search because it has all of the vocabularies loaded, where your local image won't. There are words like apropos, see, about and so on to help explore, but discoverability is admittedly a bit difficult. The docs are great, but it's still hard to narrow things down if you don't know what you're looking for...but that gets easier. Scanning through the "Vocabulary Index" in the help can give you a good idea of the types of stuff that's covered, and also -completing things while paying attention to the stack effect declaration displayed at the bottom of the listener can also be useful. The #concatenative chatroom is also helpful and friendly folks are always willing to review code and offer advice.
http://elasticdog.com/2008/12/beginning-factor-shufflers-and-combinators/#iota 😄
Using numeric ranges can be handy; my first attempt at this problem (from many years ago) looked like:
: euler1 ( x -- y )
0 999 3 <range> sum 0 999 5 <range> sum + 0 999 15 <range> sum - ;
Thanks again!
That's a clever way to solve the problem.
Nice! Here are a few suggestions for improvement:
multiple-of
is the same asdivisor?
from the math.functions vocabulary.1 - iota [ 1 + ] map
to justiota
without consequence. Note that if you do need a 1-based sequence, the typical way you'd do that in Factor would be to use the[1,b]
word from the math.ranges vocabulary.when
orunless
instead ofif
.map-sum
from the sequences vocabulary that does the same thing asmap sum
but without creating an intermediate sequence, so it's more efficient.