Skip to content

Foldable/Reducible intercalate update#1522

Merged
kailuowang merged 2 commits into
typelevel:masterfrom
peterneyens:intercalate-update
Jan 11, 2017
Merged

Foldable/Reducible intercalate update#1522
kailuowang merged 2 commits into
typelevel:masterfrom
peterneyens:intercalate-update

Conversation

@peterneyens

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor 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

johnynek commented Jan 9, 2017

Copy link
Copy Markdown
Contributor

👍

@codecov-io

codecov-io commented Jan 10, 2017

Copy link
Copy Markdown

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
Copy Markdown
Contributor

👍

@johnynek

Copy link
Copy Markdown
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

peterneyens commented Jan 10, 2017

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Contributor

👍

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)

@kailuowang kailuowang Jan 11, 2017

Copy link
Copy Markdown
Contributor

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)
 }

@peterneyens peterneyens Jan 11, 2017

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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
Copy Markdown
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