Skip to content

Update on Enum.split#9436

Merged
josevalim merged 2 commits into
elixir-lang:masterfrom
QuentinDanjou:fix-enum_split
Nov 3, 2019
Merged

Update on Enum.split#9436
josevalim merged 2 commits into
elixir-lang:masterfrom
QuentinDanjou:fix-enum_split

Conversation

@QuentinDanjou
Copy link
Copy Markdown
Contributor

@QuentinDanjou QuentinDanjou commented Oct 21, 2019

Split was allowing values like 2.0, and higher but throwing on 1.0, 0.0, and -1.0 which was not coherent. Even it does not make any sense to give floating values, it feels weird that it allows some of them and not others.
This fix now allow these values.

- Add more coherence on arguments taken by Enum.split by allowing splits by 1.0 and 0.0
@josevalim
Copy link
Copy Markdown
Member

Good catch @QuentinDanjou! I believe it would be better though to change each clause of the original split function: https://github.com/elixir-lang/elixir/pull/9436/files#diff-6881431a92cd4e3ea0de82bf2338f8eaR2457

Can you please change your PR accordingly and send a test? Thank you!

Change each clause of the original split function to limit the function call to integers
@josevalim
Copy link
Copy Markdown
Member

josevalim commented Oct 21, 2019

The update looks good. Now we only need a test and we should be good to go. The readme has instructions on how to run tests but feel free to ask if you have any questions!

@QuentinDanjou
Copy link
Copy Markdown
Contributor Author

@josevalim Sorry I didn't get if you mean adding a test to the test suite that test this behavior or simply run the test locally as explained in the readme.

Here is the specific test on my local computer:

> bin/elixirc lib/elixir/lib/enum.ex -o lib/elixir/ebin 
> bin/elixir lib/elixir/test/elixir/enum_test.exs  
Excluding tags: [windows: true]

..........................................................................................................................................................................................................................................................................................................................................................

Finished in 1.5 seconds (1.3s on load, 0.1s on tests)
183 doctests, 163 tests, 0 failures

Randomized with seed 83240

@josevalim
Copy link
Copy Markdown
Member

I meant adding a test. :)

@fskinner
Copy link
Copy Markdown

I'm having trouble to understand how this PR allows those values since is_integer(count) will always return false when the count variable is set to -1.0, 0.0 or 1.0.

It seems this fix does not allow this values, it removes every float value instead. Am I right? Or am I missing something?

@fskinner
Copy link
Copy Markdown

fskinner commented Oct 27, 2019

If that's the case, I believe adding the code below to the split/2 test would allow this PR to be merged.

    assert_raise FunctionClauseError, fn ->
      Enum.split([1, 2, 3], -2.0)
    end

    assert_raise FunctionClauseError, fn ->
      Enum.split([1, 2, 3], -1.0)
    end

    assert_raise FunctionClauseError, fn ->
      Enum.split([1, 2, 3], 0.0)
    end

    assert_raise FunctionClauseError, fn ->
      Enum.split([1, 2, 3], 1.0)
    end

    assert_raise FunctionClauseError, fn ->
      Enum.split([1, 2, 3], 2.0)
    end

    assert_raise FunctionClauseError, fn ->
      Enum.split([1, 2, 3], 3.0)
    end

I can't do it on the forked branch because of no access :)

@josevalim josevalim merged commit 0170e12 into elixir-lang:master Nov 3, 2019
@josevalim
Copy link
Copy Markdown
Member

❤️ 💚 💙 💛 💜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants