Hmm, thanks. I must've been tired!

Firstly, John: How did you perceive such an elegant solution? Is this
simply "experience", or were you looking at the problem in a certain light?

Secondly, why is it the drop-dead ugly version outperforms the elegant
version by so much (~ 2.7 times).

Relative timings:
gc [ 100,000 [ { 1 2 3 4 5 6 7 8 9 10 } dup dup 2cartesian-product drop ]
times ] time
-->32.070791587

gc [ 100,000 [ { 1 2 3 4 5 6 7 8 9 10 } dup dup 2-cartesian-product drop ]
times ] time
--> 11.547108682

: 2cartesian-product ( seq1 seq2 seq3 -- newseq )
    cartesian-product cartesian-product concat
    [ first2 swap '[ _ prefix ] map ] map concat ;

: 2-cartesian-product ( seq1 seq2 seq3 -- newseq )
    cartesian-product cartesian-product concat
    [ [ first2 first2 [ >vector ] bi@ [ over ] dip swap prefix [ swap
prefix ] dip swap , , ] each
    ] V{ } make ;

Loryn Jenkins


On Tue, Apr 2, 2013 at 1:27 AM, John Benediktsson <mrj...@gmail.com> wrote:

> Just call "concat" again;
>
> : 2cartesian-product ( seq1 seq2 seq3 -- newseq )
>     cartesian-product cartesian-product concat
>     [ first2 swap '[ _ prefix ] map ] map concat ;
>
>
> IN: scratchpad { 1 2 } { 3 4 } { 5 6 } 2cartesian-product .
> {
>     { 1 3 5 }
>     { 1 3 6 }
>     { 1 4 5 }
>     { 1 4 6 }
>     { 2 3 5 }
>     { 2 3 6 }
>     { 2 4 5 }
>     { 2 4 6 }
> }
>
>
>
>
>
> On Mon, Apr 1, 2013 at 7:25 AM, Loryn Jenkins <lor...@gmail.com> wrote:
>
>> Thank you!
>>
>> That's certainly cleaner.
>>
>> Just needed to flatten the output. I think I've seen a single word
>> that'll do that for me, but I can't remember what it is just now. unzip and
>> union will have to do for this morning.
>>
>> Thanks for your help both, Rupert and John.
>>
>> USING: USING: kernel sequences vectors assocs sets
>>
>> : 2cartesian-product ( seq1 seq2 seq3 -- newseq )
>>     cartesian-product cartesian-product concat
>>     [ first2 swap '[ _ prefix ] map ] map unzip union ;
>>
>>
>> Good night!
>>
>>
>> On Tue, Apr 2, 2013 at 1:07 AM, John Benediktsson <mrj...@gmail.com>wrote:
>>
>>> Or, alternatively doing the concat first:
>>>
>>> : 2cartesian-product ( seq1 seq2 seq3 -- newseq )
>>>     cartesian-product cartesian-product concat
>>>     [ first2 swap '[ _ prefix ] map ] map ;
>>>
>>>
>>>
>>> On Mon, Apr 1, 2013 at 7:06 AM, John Benediktsson <mrj...@gmail.com>wrote:
>>>
>>>> Instead of using make, you can do this which looks cleaner:
>>>>
>>>> : 2cartesian-product ( seq1 seq2 seq3 -- newseq )
>>>>     cartesian-product cartesian-product [
>>>>         [ first2 swap '[ _ prefix ] map ] map
>>>>     ] map concat ;
>>>>
>>>>
>>>>
>>>> On Mon, Apr 1, 2013 at 6:59 AM, Loryn Jenkins <lor...@gmail.com> wrote:
>>>>
>>>>> Thanks, Rupert.
>>>>>
>>>>> I followed your suggestion, and managed to get it working. :-)
>>>>>
>>>>> USING: kernel sequences vectors make
>>>>>
>>>>> : 2-cartesian-product ( seq1 seq2 seq3 -- newseq )
>>>>>     cartesian-product cartesian-product concat
>>>>>     [ [ first2 first2 [ >vector ] bi@ [ over ] dip swap prefix [ swap
>>>>> prefix ] dip swap , , ] each
>>>>>     ] V{ } make ;
>>>>>
>>>>> Loryn Jenkins
>>>>>
>>>>>
>>>>> On Mon, Apr 1, 2013 at 11:17 PM, Rupert Swarbrick <
>>>>> rswarbr...@gmail.com> wrote:
>>>>>
>>>>>> Loryn Jenkins <lor...@gmail.com> writes:
>>>>>> > Hi
>>>>>> >
>>>>>> > I'm attempting to adapt the code for cartesian-product into
>>>>>> > 2cartesian-product, in order to produce the cartesian product of 3
>>>>>> > sequences.
>>>>>>
>>>>>> I'm afraid I haven't got time to properly look at your code, but don't
>>>>>> forget that the cartesian product is associative. That is:
>>>>>>
>>>>>>   (A × B) × C = A × (B × C)
>>>>>>
>>>>>> and either of these two makes sense for the cartesian product of three
>>>>>> sets. In your case, this means that
>>>>>>
>>>>>>   { 1 2 } { 3 4 } { 5 6 } cartesian-product cartesian-product
>>>>>>
>>>>>> will almost do what you want (it's the right hand side of the equation
>>>>>> above). But the nesting won't be quite right, since you'll have
>>>>>> elements
>>>>>> that look like { 1 { 3 5 } }, so you can fix that with something like
>>>>>>
>>>>>>   [ first2 first2 ] map
>>>>>>
>>>>>> I think (untested).
>>>>>>
>>>>>> Rupert
>>>>>>
>>>>>>
>>>>>> ------------------------------------------------------------------------------
>>>>>> Own the Future-Intel&reg; Level Up Game Demo Contest 2013
>>>>>> Rise to greatness in Intel's independent game demo contest.
>>>>>> Compete for recognition, cash, and the chance to get your game
>>>>>> on Steam. $5K grand prize plus 10 genre and skill prizes.
>>>>>> Submit your demo by 6/6/13. http://p.sf.net/sfu/intel_levelupd2d
>>>>>> _______________________________________________
>>>>>> Factor-talk mailing list
>>>>>> Factor-talk@lists.sourceforge.net
>>>>>> https://lists.sourceforge.net/lists/listinfo/factor-talk
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>> ------------------------------------------------------------------------------
>>>>> Own the Future-Intel&reg; Level Up Game Demo Contest 2013
>>>>> Rise to greatness in Intel's independent game demo contest.
>>>>> Compete for recognition, cash, and the chance to get your game
>>>>> on Steam. $5K grand prize plus 10 genre and skill prizes.
>>>>> Submit your demo by 6/6/13. http://p.sf.net/sfu/intel_levelupd2d
>>>>> _______________________________________________
>>>>> Factor-talk mailing list
>>>>> Factor-talk@lists.sourceforge.net
>>>>> https://lists.sourceforge.net/lists/listinfo/factor-talk
>>>>>
>>>>>
>>>>
>>>
>>>
>>> ------------------------------------------------------------------------------
>>> Own the Future-Intel&reg; Level Up Game Demo Contest 2013
>>> Rise to greatness in Intel's independent game demo contest.
>>> Compete for recognition, cash, and the chance to get your game
>>> on Steam. $5K grand prize plus 10 genre and skill prizes.
>>> Submit your demo by 6/6/13. http://p.sf.net/sfu/intel_levelupd2d
>>> _______________________________________________
>>> Factor-talk mailing list
>>> Factor-talk@lists.sourceforge.net
>>> https://lists.sourceforge.net/lists/listinfo/factor-talk
>>>
>>>
>>
>>
>> ------------------------------------------------------------------------------
>> Own the Future-Intel&reg; Level Up Game Demo Contest 2013
>> Rise to greatness in Intel's independent game demo contest.
>> Compete for recognition, cash, and the chance to get your game
>> on Steam. $5K grand prize plus 10 genre and skill prizes.
>> Submit your demo by 6/6/13. http://p.sf.net/sfu/intel_levelupd2d
>> _______________________________________________
>> Factor-talk mailing list
>> Factor-talk@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/factor-talk
>>
>>
>
>
> ------------------------------------------------------------------------------
> Own the Future-Intel&reg; Level Up Game Demo Contest 2013
> Rise to greatness in Intel's independent game demo contest.
> Compete for recognition, cash, and the chance to get your game
> on Steam. $5K grand prize plus 10 genre and skill prizes.
> Submit your demo by 6/6/13. http://p.sf.net/sfu/intel_levelupd2d
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
>
------------------------------------------------------------------------------
Own the Future-Intel&reg; Level Up Game Demo Contest 2013
Rise to greatness in Intel's independent game demo contest.
Compete for recognition, cash, and the chance to get your game 
on Steam. $5K grand prize plus 10 genre and skill prizes. 
Submit your demo by 6/6/13. http://p.sf.net/sfu/intel_levelupd2d
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to