-
Notifications
You must be signed in to change notification settings - Fork 70
transpose? #225
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
Comments
|
I've no idea. I thought perhaps someone else could take a look at it. You could certainly cache the result of |
Not sure if this is faster due to the allocations of arrays, but here's what I got: transpose :: forall a. Array (Array a) -> Array (Array a)
transpose xs = go 0 []
where
go :: Int -> Array (Array a) -> Array (Array a)
go idx allArrays = case buildNext idx of
Nothing -> allArrays
Just next -> go (idx + 1) (A.snoc allArrays next)
buildNext :: Int -> Maybe (Array a)
buildNext idx = do
xs # flip F.foldl (Just []) \acc nextArr -> do
el <- A.index nextArr idx
nextArray <- acc
pure $ A.snoc nextArray el Which accounts for array elements that are unequal in size:
|
Thanks for looking at this, @JordanMartinez . When I get back home next week I'll try to investigate with some tests and performance measurements of these (and any other implementations that we might pick up in the meantime) |
I've been making a start at looking at correctness. One thing I'm not sure about is your last two examples. By analogy to transpose ( (1 : Nil) : (2 : Nil) : (3 : (4 : Nil)) : Nil) == ((1 : 2 : 3 : Nil) : (4 : Nil) : Nil)
transpose ( (1 : (2 : Nil)) : (3 : (4 : Nil)) : (5: (6 : (7 : Nil))) : Nil) == ((1 : 3 : 5 : Nil) : (2 : 4 : 6 : Nil) : (7 : Nil) : Nil) |
Huh... I guess the question here is which is the correct way. I support the List version is right because it was supported first. |
Yeah - I suppose we could see what Haskell does which might confirm things. |
Haskell's My initial attempts at profiling indicate that your implementation is probably going to be considerably faster than mine! |
How about this one? https://try.purescript.org/?gist=d367a2668942b31d9a77c53050053db4 |
That passes all my tests (which are not very different from yours anyway). Many thanks! |
And you win the performance test. Transposing a 750 * 750 Int array takes about 890 ms as opposed to my 2800. And for a 1000 * 1000 Int array, mine crashes out of memory whereas yours takes about 1920 ms. If it's of interest, my final implementation was this: transpose :: forall a. Array (Array a) -> Array (Array a)
transpose [] = []
transpose x =
case (mapMaybe head x) of
[] ->
transpose (mapMaybe tail x)
start ->
start : transpose (mapMaybe tail x) |
I'm wondering if my version would be any faster if |
Anyhow, feel free to submit a PR! |
Would you accept a PR based on your implementation? |
Yeah |
Fixed by #226 |
Is there any interest in adding
transpose
to arrays? Not sure what would be a good implementation but possibly something along the lines of:The text was updated successfully, but these errors were encountered: