Skip to content

Instantly share code, notes, and snippets.

@jeremyheiler
Last active August 29, 2015 13:58
Show Gist options
  • Save jeremyheiler/10015006 to your computer and use it in GitHub Desktop.
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 ;
@elasticdog
Copy link

Nice! Here are a few suggestions for improvement:

  1. Your word multiple-of is the same as divisor? from the math.functions vocabulary.
  2. Since sequences are 0-based and, in this case, having 0 in there won't matter, you can change 1 - iota [ 1 + ] map to just iota 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.
  3. If you have an empty quotation for a conditional, you can use either when or unless instead of if.
  4. There's a word named map-sum from the sequences vocabulary that does the same thing as map sum but without creating an intermediate sequence, so it's more efficient.

@jeremyheiler
Copy link
Author

Thanks! Appreciate it.

  1. 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.
  2. 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.

@elasticdog
Copy link

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 - ;

@jeremyheiler
Copy link
Author

Thanks again!

That's a clever way to solve the problem.

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