exercism / elixir-analyzer

GNU Affero General Public License v3.0
30 stars 32 forks source link

Add flatten-array analysis #231

Closed angelikatyborska closed 2 years ago

angelikatyborska commented 2 years ago

Closes https://github.com/exercism/elixir-analyzer/issues/60

I couldn't figure out any solutions that would use Stream only or list comprehensions only.

Website copy PR: https://github.com/exercism/website-copy/pull/2131

jiegillet commented 2 years ago

The implementation looks fine, but I looked at the problem description and nothing indicates that the problem should be solved with recursion only:

Take a nested list and return a single flattened list with all values except nil/null. The challenge is to write a function that accepts an arbitrarily-deep nested list-like structure and returns a flattened structure without any nil/null values.

So before merging this, we should at least add an appendix to the instructions. I should have caught that on the opened issue, sorry. That being said, I'm not 100% convinced that we should prohibit using Enum et al. Maybe we can simply assert that flatten calls flatten, and call that recursion. For the record, here is my solution:

  def flatten(element) when not(is_list(element)), do: [element]
  def flatten(list) do
    list
    |> Enum.map(&flatten/1)
    |> Enum.concat()
    |> Enum.reject(&is_nil/1)
  end
angelikatyborska commented 2 years ago

I looked at the problem description and nothing indicates that the problem should be solved with recursion only

Right... I'm not entirely sure why I opened the issue in the first place.

Maybe we can simply assert that flatten calls flatten, and call that recursion.

I don't think we have a good way of asserting a function is recursive. This simple check wouldn't work if you have a private helper that's recursive.

I'm just going to let this exercise be as it is and close both the PR and issue.