Skip to content
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

fromList doesn't guarantee a rectangular matrix #1

Open
maurisvh opened this issue Sep 24, 2015 · 1 comment
Open

fromList doesn't guarantee a rectangular matrix #1

maurisvh opened this issue Sep 24, 2015 · 1 comment

Comments

@maurisvh
Copy link

As-is, the fromList implementation does not check if the given list of lists is a rectangle; this can break lots of things.

Often, of course, you know for sure that the lists you're passing in are a rectangle, but there should probably be a warning about the fact that elm-matrix expects this in the docs.

Perhaps there should also be a List (List a) -> Matrix (Maybe a) function, that pads rows shorter than the longest one with Nothings.

@sindikat
Copy link
Owner

I agree that this is bad, it's user's fault, if they create a Matrix manually by relying on implementation details, but the API should be safe, and fromList is part of API. fromList shouldn't ever return junk, whatever the data.

I think the best way is to throw away elements exceeding width of the matrix, where width is a length of the shortest sublist. Something like:

fromList : List (List a) -> Matrix a
fromList xs =
  let
    minWidth = List.minimum <| List.map List.length xs
  in
    Array.map (Array.fromList << List.take minWidth) <| Array.fromList xs

I also agree that function of type List (List a) -> Matrix (Maybe a) is a good idea. I want this library to be very inclusive.

Another idea I had is fromList of type List (List a) -> Maybe (Matrix a), which returns Nothing if gets non-rectangular list of lists. But I am against this idea for now, because from my experience with Elm so far Maybe is a pain. Yes, there is andThen and withDefault etc, but unless I can figure out how to use pervasive Maybe without constantly writing spaghetti case expressions, I don't want to rely on Maybe so much.

The biggest weakness of the library is that Matrix type is not opaque, it's just a type synonym. So anyone can create an erroneous array of arrays and pretend this is a Matrix. Implementation details should be hidden. Moreover, I find implementation of a Matrix using 1-dimensional array (with explicit tracking of width and height) more elegant and possibly more efficient. I want to defer reimplementation of Matrix until the library accumulates more functions and is more battle-tested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants