- Notifications
You must be signed in to change notification settings - Fork 55
Divide and conquer array traverse #48
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
Divide and conquer array traverse #48
Conversation
This looks pretty great to me! @hdgarrood did you want to try this with benchotron or something? |
That would be nice, although I am struggling to find the time right now. 👍 lgtm, especially considering that this helps avoid stack overflows. |
@paf31 any comments here either? |
@S11001001 is it work for array that have length odd number, eg 3, 5 etc. Pardon me, if i wrong, It look like your implementation will fail if the array length is odd. for example:
and you divide it to |
Sorry, It look like it will only fail if the array length is 3, you maybe only need |
@syaiful6 yep you're right, thanks for the heads up. I'll see if I can fix this. |
Now that |
The current import Prelude import Data.Traversable import Data.List as L import Test.Assert (assert) import Control.Monad.Eff.Console (log) main = do let xs = L.range 1 25000 assert $ (traverse pure xs :: Unit -> L.List Int) unit == xs log "done" |
Oh, I see now, thanks. |
Also, I benchmarked this locally and |
Ok, this is not really what I would have expected, since I would have expected all the benchTraverse :: Benchmark benchTraverse = mkBenchmark { slug: "traverse" , title: "Traversable Array" , sizes: A.range 1 5 <#> (_ * 1000) , sizeInterpretation: "Length of the array" , inputsPerSize: 1 , gen: \n -> vectorOf n (arbitrary :: Gen Int) , functions: [ benchFn "current" (traverse f) , benchFn "suggested" (traverse f <<< DivideConquer) ] } where f :: forall e a. a -> MaybeT (WriterT Unit (ExceptT Unit (Eff e))) a f x = do tell unit pure x Maybe we'd only see the effect I expected with an even more expensive |
@S11001001 ok, it look like we can fix it by make sure the pivot never equals to zero, so we make progress in each recursion. We can update our pivot computation to:
|
Fixes test of parent commit, 6eb97e5.
Also use a type synonym to simplify type signatures in Test.Main
a052688
to 51b24e8
Compare #54 is a slightly further along version of this, so shall we close this? |
@hdgarrood sure. |
Port of scalaz/scalaz#1023, as explained there.
Includes test that fails the stack-safety check with previous
traverseArrayImpl
, but passes with this one.Original discussion from IRC: