<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="264766" data-post-id="264766">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Eiji" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Eiji/120/36743_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Eiji
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Sebb" data-post="11" data-topic="51003">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sebb/48/21818_2.png" class="avatar"> Sebb:</div>
<blockquote>
<p>I wonder if there is any way to do this (passing Elji’s test cases) with a comprehension?</p>
</blockquote>
</aside>
<p>Did you challenged a senior developer? <img src="https://forum.elixirforum.com/images/emoji/apple/smiling_imp.png?v=15" title=":smiling_imp:" class="emoji" alt=":smiling_imp:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Example do
  def sample(list, opts \\ [skip: true])

  # uncomment if empty list is could be passed as an input
  def sample([], _opts), do: []

  # uncomment if empty list could be a first element
  def sample([[] | tail], opts), do: sample(tail, opts)

  # uncomment if list could contain only one element list
  def sample([list], _opts), do: Enum.map(list, &amp;List.wrap/1)

  # in any other case
  def sample(list, opts) do
    if opts[:skip], do: sample_with_skip(list), else: sample_without_skip(list)
  end

  # skip empty elements in root list
  def sample_with_skip(list) do
    for sub_list &lt;- list, reduce: [] do
      # skip empty elements in root list
      data when sub_list == [] -&gt;
        data

      # passing a first element of root list as data
      [] -&gt;
        sub_list

      data -&gt;
        for sub_data &lt;- data, element &lt;- sub_list do
          [element | List.wrap(sub_data)]
        end
    end
    |&gt; Enum.map(&amp;Enum.reverse/1)
  end

  # alternatively do not skip empty elements in root list
  def sample_without_skip(list) do
    for sub_list &lt;- list, reduce: [] do
      # when element is empty list set data to nil
      _data when sub_list == [] -&gt;
        nil

      # no matter what happens later if data is nil keep it as is
      nil -&gt;
        nil

      # passing a first element of root list as data
      [] -&gt;
        sub_list

      data -&gt;
        for sub_data &lt;- data, element &lt;- sub_list do
          [element | List.wrap(sub_data)]
        end
    end
    |&gt; case do
      nil -&gt; []
      list -&gt; Enum.map(list, &amp;Enum.reverse/1)
    end
  end
end
</code></pre>
<p>Pretty much the same as <code>Enum.reduce/3</code> version. Simply pattern-matching here is not only in function clause, but also in <code>for …, reduce: … clauseend</code> notation.</p>
<p>However this looks like an <code>art for art's sake</code>. Just look how simple is raw pattern-matching comparing to 2 other versions of my solution. <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="264766" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-generate-permutations-from-different-sets-of-elements/51003/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-264766" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="264766"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="264775" data-post-id="264775">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="gregvaughn" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gregvaughn/120/954_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  gregvaughn
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Sebb" data-post="11" data-topic="51003">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sebb/48/21818_2.png" class="avatar"> Sebb:</div>
<blockquote>
<p><a class="mention" href="/u/gregvaughn" rel="nofollow">@gregvaughn</a> to the rescue! (because it’s your fault, that I’m always looking for the <code>for</code>solution)</p>
</blockquote>
</aside>
<p>Oh, no! I suppose I’m building a reputation.</p>
<p>I was asked a variant of this at a job interview about 5 years ago and tried to make it work with a comprehension and failed. That was before the <code>reduce</code> option was added. I ultimately solved it with a recursive call to a comprehension. It needs a recursive/reduce style to handle the unknown count of lists.</p>
<p>However, now that I think about it, I wonder if a macro could generate the right count of generators in the for comprehension? Nah, only if the input is known at compile time.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="264775" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-generate-permutations-from-different-sets-of-elements/51003/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-264775" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="264775"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>