Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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 . ;
share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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

: mult3or5? ( x -- ? ) [ mult3? ] [ mult5? ] bi or ;
share|improve this answer
add comment

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? ;
share|improve this answer
 
Thanks. A small improvement though. Let me know if you find something else. –  Torbjørn Oct 4 '11 at 21:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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