-
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 to NonEmptyArray #227
Comments
Oh, I was thinking something much simpler using transpose :: forall a. NonEmptyArray (Array a) -> NonEmptyArray (Array a)
transpose = unsafeAdapt Array.transpose |
OK - I'll add this to my tests and see how it compares. |
Oh, I hadn't spotted that our signatures are different. Mine handles |
And your implementation does suggest another implementation for my signature: import Prelude
import Data.Array.NonEmpty (toArray)
import Data.Array.NonEmpty.Internal (NonEmptyArray(..))
import Data.Array (transpose) as A
transpose :: forall a. NonEmptyArray (NonEmptyArray a) -> NonEmptyArray (NonEmptyArray a)
transpose = unsafeFromArray2D <<< A.transpose <<< toArray2D
toArray2D :: forall a. NonEmptyArray (NonEmptyArray a) -> Array (Array a)
toArray2D =
(map toArray) <<< toArray
unsafeFromArray2D :: forall a. Array (Array a) -> NonEmptyArray (NonEmptyArray a)
unsafeFromArray2D =
(map unsafeFromArray) <<< unsafeFromArray
unsafeFromArray :: forall a. Array a -> NonEmptyArray a
unsafeFromArray = NonEmptyArray This has identical performance behaviour to the better of my two implementations above and I think has the virtue of simplcity. |
Having both varieties would probably be good actually! |
OK. What about naming? |
That works for me, we tend to prime the other |
Fixed by #228 |
I've had a look at modifying both implementations we have for the normal
Array
case detailed in #225 for use with aNonEmptyArray
. I came up with this one based on my implementation:and this one based on the implementation from @JordanMartinez :
All the tests pass for whichever version, but performance comparisons show the same pattern as for
Array
- Jordan's is again about three times faster than mine and is only a little slower than forArray
. I'm slightly concerned about the use ofunsafeFromArray
at the top level, but I think it's legitimate and many other functions in the module rely on it.If you're agreeable, I'll raise a PR using this.
The text was updated successfully, but these errors were encountered: