You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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-matrixexpects 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.
The text was updated successfully, but these errors were encountered:
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.
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 withNothing
s.The text was updated successfully, but these errors were encountered: