<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=utf-8">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    Hmm, I tried to post this via google groups but it was rejected. So
    I try to mail the list instead. Sorry for any duplicates.<br>
    <br>
    <div>
      <div style="overflow: auto">
        <div style="max-height: 10000px;">
          <div dir="ltr">Hi list!<br>
            <br>
            How to transform a list of streams of elements into a stream
            of lists of elements such that all combinations of exactly
            one element of each input stream is present? I.e. something
            like this:<br>
            <br>
            <div
style="background-color:rgb(250,250,250);border-color:rgb(187,187,187);border-style:solid;border-width:1px;word-wrap:break-word"><code>
                <div><span style="color:#000"><code><span
                        style="color:#000">type </span><span
                        style="color:#606">Stream</span><span
                        style="color:#000"> a </span><span
                        style="color:#660">=</span><span
                        style="color:#000"> </span><span
                        style="color:#660">[</span><span
                        style="color:#000">a</span><span
                        style="color:#660">]</span><span
                        style="color:#000"><br>
                      </span></code>tr </span><span style="color:#660">::</span><span
                    style="color:#000"> </span><span style="color:#660">[</span><span
                    style="color:#606">Stream</span><span
                    style="color:#000"> e</span><span style="color:#660">]</span><span
                    style="color:#000"> </span><span style="color:#660">-></span><span
                    style="color:#000"> </span><span style="color:#606">Stream</span><span
                    style="color:#000"> </span><span style="color:#660">[</span><span
                    style="color:#000">e</span><span style="color:#660">]</span><span
                    style="color:#000"><br>
                    tr = sequence<br>
                  </span></div>
              </code></div>
            <br>
            But I want the order of the output lists so the ones with
            early elements come first. The above starts with the first
            elements from each stream but does not treat the streams
            fairly. All the elements of the last stream are used before
            the next element of the second last stream is used, i.e.<br>
            <br>
            <div
style="background-color:rgb(250,250,250);border-color:rgb(187,187,187);border-style:solid;border-width:1px;word-wrap:break-word"><code>
                <div>> tr [[1..3], [1..2], [1..3]]<br>
                  [[1,1,1],[1,1,2],[1,1,3],[1,2,<wbr>1].....<br>
                  <span style="color:#660"></span></div>
              </code></div>
            <br>
            I don't want the third element of one stream before I have
            seen all the combinations of the first two elements of each
            stream and so on. In this case I want something like this:<br>
            <br>
            <div
style="background-color:rgb(250,250,250);border-color:rgb(187,187,187);border-style:solid;border-width:1px;word-wrap:break-word"><code>
                <div><span style="color:#606"> [[1,1,1], [1,1,2],
                    [1,2,1], [2,1,1], [1,2,2], [2,1,2], [2,2,1],
                    [2,2,2], [1,1,3]....</span><span style="color:#660"></span></div>
              </code></div>
            <br>
            Also [2,1,1] comes before [1,2,2] because the former
            contains two first element and one second element while the
            latter contains only one first element and two second
            elements.<br>
            <br>
            The number of streams are fairly low but the sequences can
            be very long (almost infinte :) and I want the first results
            without having to go though a full stream. Also the streams
            may vary in length so one stream may ran out of elements
            before the others. If that happens, the non-empty streams
            should contrbute fairly with new elements, i.e:<br>
            <br>
            <div
style="background-color:rgb(250,250,250);border-color:rgb(187,187,187);border-style:solid;border-width:1px;word-wrap:break-word"><code>
                <div><span style="color:#606">> sequence_ $ map print
                    $ tr [[1..2], [3..5]]  -- two times three equals to
                    six combinations<br>
                    [1,3]  -- 1: first and first<br>
                    [1,4]  -- 2: first and second<br>
                    [2,3]  -- 3: second and first<br>
                    [2,4]  -- 4: second and second<br>
                    [1,5]  -- 5: first and third<br>
                    [2,5]  -- 6: second and third<br>
                  </span><span style="color:#660"></span></div>
              </code></div>
            <br>
            Note, I'm not just interested to handle infinite streams
            (nor infinite number of streams) using some diagonal
            algorithm. I have looked at the Omega library. But it
            doesn't return the combinations in wanted order.<br>
            <br>
            If someone still wonder about the output ordering, the
            following program may help:<br>
            <div
style="background-color:rgb(250,250,250);border-color:rgb(187,187,187);border-style:solid;border-width:1px;word-wrap:break-word"><code>
                <div><span style="color:#008">import</span><span
                    style="color:#000"> </span><span style="color:#606">Data</span><span
                    style="color:#660">.</span><span style="color:#606">List</span><span
                    style="color:#000"> </span><span style="color:#660">(</span><span
                    style="color:#000">sort</span><span
                    style="color:#660">,</span><span style="color:#000">
                    sortBy</span><span style="color:#660">)</span><span
                    style="color:#000"><br>
                    <br>
                    type </span><span style="color:#606">Stream</span><span
                    style="color:#000"> a </span><span
                    style="color:#660">=</span><span style="color:#000">
                  </span><span style="color:#660">[</span><span
                    style="color:#000">a</span><span style="color:#660">]</span><span
                    style="color:#000"><br>
                    <br>
                    tr </span><span style="color:#660">::</span><span
                    style="color:#000"> </span><span style="color:#606">Ord</span><span
                    style="color:#000"> e </span><span
                    style="color:#660">=></span><span
                    style="color:#000"> </span><span style="color:#660">[</span><span
                    style="color:#606">Stream</span><span
                    style="color:#000"> e</span><span style="color:#660">]</span><span
                    style="color:#000"> </span><span style="color:#660">-></span><span
                    style="color:#000"> </span><span style="color:#606">Stream</span><span
                    style="color:#000"> </span><span style="color:#660">[</span><span
                    style="color:#000">e</span><span style="color:#660">]</span><span
                    style="color:#000"><br>
                    tr </span><span style="color:#660">=</span><span
                    style="color:#000"> sortBy myOrder </span><span
                    style="color:#660">.</span><span style="color:#000">
                    sequence<br>
                         </span><span style="color:#008">where</span><span
                    style="color:#000"> myOrder </span><span
                    style="color:#660">::</span><span style="color:#000">
                  </span><span style="color:#606">Ord</span><span
                    style="color:#000"> e </span><span
                    style="color:#660">=></span><span
                    style="color:#000"> </span><span style="color:#660">[</span><span
                    style="color:#000">e</span><span style="color:#660">]</span><span
                    style="color:#000"> </span><span style="color:#660">-></span><span
                    style="color:#000"> </span><span style="color:#660">[</span><span
                    style="color:#000">e</span><span style="color:#660">]</span><span
                    style="color:#000"> </span><span style="color:#660">-></span><span
                    style="color:#000"> </span><span style="color:#606">Ordering</span><span
                    style="color:#000"><br>
                           myOrder xs ys </span><span
                    style="color:#660">=</span><span style="color:#000">
                    compare </span><span style="color:#660">(</span><span
                    style="color:#000">reverse $ sort xs</span><span
                    style="color:#660">)</span><span style="color:#000">
                  </span><span style="color:#660">(</span><span
                    style="color:#000">reverse $ sort ys</span><span
                    style="color:#660">)</span><span style="color:#000"><br>
                    <br>
                    main </span><span style="color:#660">=</span><span
                    style="color:#000"> sequence_ $ map </span><span
                    style="color:#008">print</span><span
                    style="color:#000"> $ tr </span><span
                    style="color:#660">[[</span><span style="color:#066">1.</span><span
                    style="color:#660">.</span><span style="color:#066">3</span><span
                    style="color:#660">],</span><span style="color:#000">
                  </span><span style="color:#660">[</span><span
                    style="color:#066">1.</span><span style="color:#660">.</span><span
                    style="color:#066">2</span><span style="color:#660">],</span><span
                    style="color:#000"> </span><span style="color:#660">[</span><span
                    style="color:#066">1.</span><span style="color:#660">.</span><span
                    style="color:#066">3</span><span style="color:#660">],</span><span
                    style="color:#000"> </span><span style="color:#660">[</span><span
                    style="color:#066">1.</span><span style="color:#660">.</span><span
                    style="color:#066">4</span><span style="color:#660">]]</span><span
                    style="color:#000"><br>
                  </span></div>
              </code></div>
            <br>
            But this of course is not a valid solution since it inspects
            every combinations before producing any output.<br>
            <br>
            Attached the sample output.<br>
          </div>
        </div>
      </div>
    </div>
  </body>
</html>