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\$

2 Answers 2

6
\$\begingroup\$

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

: mult3or5? ( x -- ? ) [ mult3? ] [ mult5? ] bi or ;
\$\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? ;
\$\endgroup\$
1
  • \$\begingroup\$ Thanks. A small improvement though. Let me know if you find something else. \$\endgroup\$ Commented Oct 4, 2011 at 21:12

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.