-
Notifications
You must be signed in to change notification settings - Fork 71
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
Add transpose and transpose' to NonEmptyArray #228
Conversation
Implementation courtesy of Jordan Martinez.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My comment below also applies to unsafeFromArray2D
.
and implement the functions that transform 2D arrays between Array and NonEmptyArray representations to use coerce.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think adding one more test should be done, but otherwise this looks good to go.
log "transpose' skips elements when rows don't match" | ||
assert $ NEA.transpose' (fromArray [[10,11], [20], [30,31,32]]) == | ||
(fromArray [[10,20,30], [11,31], [32]]) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can another test be added here for NEA.transpose' (fromArray [[]]) == (fromArray [[]])
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this test fails - it produces (fromArray [])
. Perhaps we need to special-case it? This works:
transpose' :: forall a. NonEmptyArray (Array a) -> NonEmptyArray (Array a)
transpose' (NonEmptyArray [[]]) = NonEmptyArray [[]]
transpose' xs = (unsafeAdapt A.transpose) xs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I think my example is just wrong. Array
has the same behavior in its test: https://github.com/purescript/purescript-arrays/blob/master/test/Test/Data/Array.purs#L280
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the test should be NEA.transpose' (fromArray [[]]) == (fromArray [])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surely fromArray []
is an empty NonEmptyArray
and thus eminently unsafe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh gosh... 🤦♂️ Thanks!
Then the implementation should be
transpose' :: forall a. NonEmptyArray (Array a) -> Maybe (NonEmptyArray (Array a))
transpose' = fromArray <<< A.transpose <<< coerce
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not entirely happy with this. From my perspective, transpose' (fromArray [[]])
is a very odd thing to do anyway and I wouldn't have thought it would be entirely incorrect for it to be an idempotent operation. But the cost of doing what you suggest is to make all the normal cases more awkward by wrapping the result in the Maybe
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awkward or not, it is correct. And if that's the definition, then it's the same as:
fromArray <<< A.transpose <<< NEA.toArray
So, I would argue now that we shouldn't implement that here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll happily do what you suggest if it is the correct definition. Unless I hear otherwise from @garyb today I'll do it this way - I don't feel strongly about it in any case.
(However just to expand my approach a bit, I'd argue that the minimal result for transpose Array (Array a)
is []
, that for NonEmptyArray (NonEmptyArray a)
is [[something]]
and that for NonEmptyArray (Array a)
is [[]]
. There seems to be no equivalent function on ```NonEmptyList```` in the Haskell library. which might indicate correctness.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The result of transposing [[]]
being [[]]
does sound pretty reasonable to me (in both non-or-empty cases), but I guess if we have evidence from elsewhere that that's not normal, then yeah having transpose'
fail with Nothing
is probably the best path forward.
so that it returns a Maybe. This is necessary in order to handle the edge case of transpose` (NonEmptyArray [ [] ]).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll merge and release this later today, writing myself a script to make the process easier, and this will be the test case 😉
Released as v7.1.0 |
Description of the change
Now that we have
transpose
inArray
we can add two variants of it toNonEmptyArray
.transpose
handles the case where both the inner and outer arrays are non-empty.transpose'
handles the case where the inner arrays are typed as a standardArray
. Thetranspose'
implementation courtesy of @JordanMartinez .For a discussion see #227.
Checklist: