10
\$\begingroup\$

I'm a complete beginner of Factor, but I just managed to solved Euler Problem #1 using it, and would love to have the code reviewed. Anything that can be improved? In particular I'm wondering if there is a cleaner or more idiomatic way to write the mult3or5? word.

USING: math kernel sequences math.ranges prettyprint ;
IN: euler1

: mult? ( x y -- ? ) rem 0 = ;

: mult3? ( x -- ? ) 3 mult? ;
: mult5? ( x -- ? ) 5 mult? ;

: mult3or5? ( x -- ? ) dup mult3? swap mult5? or ;

: sumMultsOf3or5 ( seq -- n ) [ mult3or5? ] filter sum ;

: solveEuler1 ( -- ) 0 1000 (a,b) sumMultsOf3or5 . ;
\$\endgroup\$
6
\$\begingroup\$

dup f swap g is a common idiom, so Factor has bi.

: mult3or5? ( x -- ? ) [ mult3? ] [ mult5? ] bi or ;
| improve this answer | |
\$\endgroup\$
5
\$\begingroup\$

I'm a Factor beginner too, but I'd use the zero? word from math in the definition of mult:

: mult? ( x y -- ? ) rem zero? ;
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks. A small improvement though. Let me know if you find something else. \$\endgroup\$ – Torbjørn Oct 4 '11 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.