From decd9ed1eec38f0a91ccd3f08d5ae70e19a8af06 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Sat, 6 Apr 2013 13:12:57 -0700 Subject: [PATCH] math.combinatorics: make permutations-quot to reduce code duplication. --- basis/math/combinatorics/combinatorics.factor | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/basis/math/combinatorics/combinatorics.factor b/basis/math/combinatorics/combinatorics.factor index 69f2f2aca5..490d496992 100644 --- a/basis/math/combinatorics/combinatorics.factor +++ b/basis/math/combinatorics/combinatorics.factor @@ -68,13 +68,18 @@ M: permutations hashcode* tuple-hashcode ; INSTANCE: permutations immutable-sequence + + : each-permutation ( seq quot -- ) - [ [ permutation-iota ] keep ] dip - '[ _ permutation @ ] each ; inline + permutations-quot each ; inline : map-permutations ( seq quot -- seq' ) - [ [ permutation-iota ] keep ] dip - '[ _ permutation @ ] map ; inline + permutations-quot map ; inline : filter-permutations ( seq quot -- seq' ) selector [ each-permutation ] dip ; inline @@ -83,9 +88,8 @@ INSTANCE: permutations immutable-sequence [ ] map-permutations ; : find-permutation ( seq quot -- elt ) - [ dup [ permutation-iota ] keep ] dip - '[ _ permutation @ ] find drop - [ swap permutation ] [ drop f ] if* ; inline + [ permutations-quot find drop ] + [ drop over [ permutation ] [ 2drop f ] if ] 2bi ; inline : reduce-permutations ( seq identity quot -- result ) swapd each-permutation ; inline