I've written I word, combinations, which takes a sequence:
{ "a" "b" "c" "d" }
and emits a sequence where each term is combined with each other term:
{ { "a" "b" } { "a" "c" } { "a" "d" } { "b" "c" } { "b" "d" } { "c" "d" } }
When I was writing it, I was trying to write it in a functional style. But it hasn't turned out so functional. I figure I'm missing a number of abstractions or approaches to doing this in a functional style.
USING: kernel make sequences arrays prettyprint ;
IN: combinations
: make-pairs ( seq seq -- seq )
[ [ [ , , ] { } make , ] cartesian-each ] { } make ;
: combine ( seq -- seq seq )
1 cut* over make-pairs ;
: process ( seq seq -- seq seq )
dup length 1 = [ ] [ combine swap [ append! ] dip process ] if ;
: combinations ( seq -- seq )
V{ } clone swap process drop ;