Skip to content
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

Foldable/Reducible intercalate update #1522

Merged
merged 2 commits into from
Jan 11, 2017

Conversation

peterneyens
Copy link
Collaborator

Follow up on #1520, after @johnynek 's comment.

This should reduce the complexity for intercalate from O(N^2) to O(N).
I also overrode toList in some Foldable instances.

case one @ NonEmptyList(_, Nil) => one
case NonEmptyList(hd, tl) => NonEmptyList(hd, a :: intersperseList(tl, a))
}
Reducible[NonEmptyList].reduce(nel)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could probably skip the unnecessary reduce step for one.

  toNonEmptyList(fa) match {
    case NonEmptyList(hd, Nil) => hd
    case NonEmptyList(hd, tl) => 
      Reducible[NonEmptyList].reduce(NonEmptyList(hd, a :: intersperseList(tl, a)))
  }

@johnynek
Copy link
Contributor

johnynek commented Jan 9, 2017

👍

@codecov-io
Copy link

codecov-io commented Jan 10, 2017

Current coverage is 92.37% (diff: 100%)

Merging #1522 into master will decrease coverage by 0.03%

@@             master      #1522   diff @@
==========================================
  Files           246        246          
  Lines          3743       3763    +20   
  Methods        3622       3637    +15   
  Messages          0          0          
  Branches        121        126     +5   
==========================================
+ Hits           3459       3476    +17   
- Misses          284        287     +3   
  Partials          0          0          

Powered by Codecov. Last update fd15e07...b723dea

@travisbrown
Copy link
Contributor

👍

@johnynek
Copy link
Contributor

Actually it looks like Foldable.combineAll is just an alias for fold which does not call Monoid.combineAll. I suggest we make Foldable.combineAll be Monoid.combineAll(fa.toList)

@peterneyens
Copy link
Collaborator Author

peterneyens commented Jan 10, 2017

Updated to use the combineAll from Monoid.

It seems we were already using Semigroup.combineAllOption to implement Reducible#reduce for NonEmptyVector, so I went ahead and did the same for NonEmptyList and overrode the fold method of the Foldable instances of List, Vector, Set, Stream and Map. (Hmm, this might have deserved its own PR.)

@johnynek
Copy link
Contributor

👍

@@ -82,6 +82,10 @@ trait ListInstances extends cats.kernel.instances.ListInstances {

override def foldM[G[_], A, B](fa: List[A], z: B)(f: (B, A) => G[B])(implicit G: Monad[G]): G[B] =
Foldable.iteratorFoldM(fa.toIterator, z)(f)

override def fold[A](fa: List[A])(implicit A: Monoid[A]): A = A.combineAll(fa)
Copy link
Contributor

@kailuowang kailuowang Jan 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably missed something but I am curious what gain we get from overriding this? I mean the
In Monoid.combineAll it's

as.foldLeft(empty)(combine)

In Foldable.fold it's

 foldLeft(fa, A.empty) { (acc, a) =>
    A.combine(acc, a)
 }

Copy link
Collaborator Author

@peterneyens peterneyens Jan 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was confused about that too.

When you run the MapMonoidBench, you can see that the first using Monoid.combineAll is considerably faster than the second.

That's because Monoid.combineAll is overridden in some instances, eg Map and more general Iterable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it. that's what I suspected, but I only checked the List instance.

@kailuowang
Copy link
Contributor

👍

@kailuowang kailuowang merged commit 2e621b8 into typelevel:master Jan 11, 2017
@peterneyens peterneyens deleted the intercalate-update branch January 16, 2017 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants