How can I extract the redundancy in this Factor word definition with dataflow combinators?

54 Views Asked by At

I'm using the early ProjectEuler problems as a way to get to know Factor. Already in the very first problem I don't find a satisfactory solution.

I can solve the division test with this

: 3or5divisible ( n -- ? ) [ 3 mod ] [ 5 mod ] bi * 0 = ;

But what I don't like is the repetition of the mod. Of course it's only two times, but another problem might need to check 200.

I tried with map, curry, bi, 2bi, bi@, arrays and plain stack values etc. I always get a stack underflow or an effect mismatch (when using map). I have have not yet found a way to see the results of my trials in the inspector.

How can I factor that mod out and have it applied to { 3 5 } or an equivalent stack?
Cool would be two variants e.g. of a mod3and5 (including effect specification); one that leaves 2 1 on the stack for input 11 and one that returns { 2 1 }.

1

There are 1 best solutions below

0
forthnutter On

I can only think of doing it this way, which removes one of the mods, but ends up adding a bi-curry@ function.

: 3or5divisible ( n -- ? ) 3 5 [ mod ] bi-curry@ bi * 0 = ;