How to get the list of all sublists?

Hi all, I’ve very new. I have the following to return a list of all the items of a given list:

  def list_of_items([]), do: []
  def list_of_items([a]), do: [[a]]
  def list_of_items([head|tail]), do: list_of_items_helper([[head]], tail)
  def list_of_items_helper(lists, [a]), do: lists ++ [[a]]
  def list_of_items_helper(lists, [head|tail]), do: list_of_items_helper(lists ++ [[head]], tail)

What I really want is the list of all sublists, e.g. [1,2,3] returns [[1],[2],[3],[1,2],[1,3],[2,3]]
I’m struggling with logic that would expand to an arbitrarily sized list.

Thanks for any guidance!

I believe you are looking for something like this.

Thanks! That helped a lot and I ended up with:

  def combinations(list), do: combinations_helper(length(list), [], list)

  defp combinations_helper(0, list_of_lists, _), do: list_of_lists
  defp combinations_helper(_, list_of_lists, []), do: list_of_lists
  defp combinations_helper(size, list_of_lists, list) do
    Enum.concat(for elem <- (for n <- 1..size, do: list_of_lists ++ simple_combos(n, list)), do: elem)
  end

A different approach by using Enum.chunk_every/3

 defmodule ListOfSublists do
    def combinations(list) do
      chunk_to_sublist(list, length(list))
    end

    defp chunk_to_sublist(list, length) do
      do_chunk_to_sublist(list, 1, length, [])
    end

    defp do_chunk_to_sublist(_list, length, length, sublists) do
      sublists
    end

    defp do_chunk_to_sublist(list, count, length, sublists) do
      sublists_for_count = Enum.chunk_every(list, count, 1)

      do_chunk_to_sublist(list, count + 1, length, sublists ++ sublists_for_count)
    end
  end