New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug for mapping over empty array #169

Closed
wants to merge 1 commit into
base: exposing
from

Conversation

Projects
None yet
3 participants
@TheSeamau5
Contributor

TheSeamau5 commented Feb 11, 2015

Mapping a function over an empty array used to somehow create an array
of length 1. Now, mapping a function over an empty array returns an
empty array

Fixed bug for mapping over empty array
Mapping a function over an empty array used to somehow create an array
of length 1. Now, mapping a function over an empty array returns an
empty array
@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Feb 11, 2015

Member

Make sure the style of the change matches the surrounding code. More about that here.

I think this case deserves a test case as well so we don't have regressions.

Overall, I see that this fixes the bug, but I don't feel like we actually understood what's going on here. Perhaps @Xashili can give some guidance?

@TheSeamau5, can you share the minimal example that demonstrates the bug in this thread?

Member

evancz commented Feb 11, 2015

Make sure the style of the change matches the surrounding code. More about that here.

I think this case deserves a test case as well so we don't have regressions.

Overall, I see that this fixes the bug, but I don't feel like we actually understood what's going on here. Perhaps @Xashili can give some guidance?

@TheSeamau5, can you share the minimal example that demonstrates the bug in this thread?

@TheSeamau5

This comment has been minimized.

Show comment
Hide comment
@TheSeamau5

TheSeamau5 Feb 11, 2015

Contributor

This is the minimum failing test (I've added some printing so you can see it)

import Text (asText)
import Array (..)
import List

main =
  asText
    (List.length
      (toList
        (map (\x -> x * x) empty)))

The result is this:

screen shot 2015-02-11 at 11 09 28 pm

Whereas, what the code does is just mapping a function over an empty array (which should return an empty array), then convert the array to a list (which should be the empty list), then get its length (which should be 0), then display it. Instead, we get 1.

To convince yourself that there's something lurking in that array, if you do this:

import Text (asText)
import Array (..)

main =
  asText
    (toList
      (map (\x -> x * x) empty))

you get this:

screen shot 2015-02-11 at 11 02 03 pm

But, this is not toList's fault because:

import Text (asText)
import Array (..)

main =
  asText
    (toList empty)

yields:

screen shot 2015-02-11 at 11 00 45 pm

It's map's fault. Turns out, it forgot to check for the empty array. So I added that check.

Contributor

TheSeamau5 commented Feb 11, 2015

This is the minimum failing test (I've added some printing so you can see it)

import Text (asText)
import Array (..)
import List

main =
  asText
    (List.length
      (toList
        (map (\x -> x * x) empty)))

The result is this:

screen shot 2015-02-11 at 11 09 28 pm

Whereas, what the code does is just mapping a function over an empty array (which should return an empty array), then convert the array to a list (which should be the empty list), then get its length (which should be 0), then display it. Instead, we get 1.

To convince yourself that there's something lurking in that array, if you do this:

import Text (asText)
import Array (..)

main =
  asText
    (toList
      (map (\x -> x * x) empty))

you get this:

screen shot 2015-02-11 at 11 02 03 pm

But, this is not toList's fault because:

import Text (asText)
import Array (..)

main =
  asText
    (toList empty)

yields:

screen shot 2015-02-11 at 11 00 45 pm

It's map's fault. Turns out, it forgot to check for the empty array. So I added that check.

@TheSeamau5

This comment has been minimized.

Show comment
Hide comment
@TheSeamau5

TheSeamau5 Feb 11, 2015

Contributor

I have no idea how Array works exactly but I figured that height == 0 is the strongest guarantee that you have an empty array, i think.

Contributor

TheSeamau5 commented Feb 11, 2015

I have no idea how Array works exactly but I figured that height == 0 is the strongest guarantee that you have an empty array, i think.

@xash

This comment has been minimized.

Show comment
Hide comment
@xash

xash Feb 12, 2015

height is 0 if and only if the array is a leaf, i. e. a tree, that has actual items in its .table-array. If height is greater then 0, then the array is a branch, i. e. a tree, that has other trees in its .table-array. So your method would break every map over a leaf. :-)

I'm not able to make a commit right now (or the next days), but I guess the error lays around line 327 in the map function, when creating the new .table-array: instead of table: new Array(a.table) it should be table: new Array(a.table.length), as the former gives an array as an argument to the array constructor. This creates an array with the size of 1.

Same bug and fix should be in the indexedMap function. Thanks for the catch!

xash commented Feb 12, 2015

height is 0 if and only if the array is a leaf, i. e. a tree, that has actual items in its .table-array. If height is greater then 0, then the array is a branch, i. e. a tree, that has other trees in its .table-array. So your method would break every map over a leaf. :-)

I'm not able to make a commit right now (or the next days), but I guess the error lays around line 327 in the map function, when creating the new .table-array: instead of table: new Array(a.table) it should be table: new Array(a.table.length), as the former gives an array as an argument to the array constructor. This creates an array with the size of 1.

Same bug and fix should be in the indexedMap function. Thanks for the catch!

@TheSeamau5

This comment has been minimized.

Show comment
Hide comment
@TheSeamau5

TheSeamau5 Feb 12, 2015

Contributor

Oops. Sorry about that.

Yeah, just push the fix whenever you can. In the meantime I'll try setting up tests with Array. I use it a lot so I'd like to make sure everything works well. I'll let you know if I find something else that's funny.

Contributor

TheSeamau5 commented Feb 12, 2015

Oops. Sorry about that.

Yeah, just push the fix whenever you can. In the meantime I'll try setting up tests with Array. I use it a lot so I'd like to make sure everything works well. I'll let you know if I find something else that's funny.

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Feb 13, 2015

Member

Fix suggested by @Xashili is done with #171 by @brown-dragon. Can someone add the relevant tests?

Member

evancz commented Feb 13, 2015

Fix suggested by @Xashili is done with #171 by @brown-dragon. Can someone add the relevant tests?

@evancz evancz closed this Feb 13, 2015

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