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

Add cats instances for segment #1016

Merged
merged 2 commits into from
Dec 9, 2017

Conversation

jmcardon
Copy link
Contributor

@jmcardon jmcardon commented Dec 9, 2017

Includes:

  • Traverse and Monad implicitly for any Segment[A, Unit]
  • A function to create a Monad[Segment[A, ?]]

def pure[A](x: A): Segment[A, Unit] = Segment(x)
}

def segmentRMonad[T]: Monad[Segment[T, ?]] = new Monad[Segment[T, ?]]{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm a bit iffy on this name but I Wasn't sure what to call it...

rightAppliedSegmentMonad???

Copy link
Member

@mpilquist mpilquist left a comment

Choose a reason for hiding this comment

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

Thanks for adding these instances! Added a few comments.

@@ -1431,3 +1432,50 @@ object Segment {
def defer(t: => Unit): Unit = deferred.addLast(() => t)
}
}

trait SegmentInstances {
Copy link
Member

Choose a reason for hiding this comment

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

Let's put these directly in the companion instead of in a trait.

@@ -1431,3 +1432,50 @@ object Segment {
def defer(t: => Unit): Unit = deferred.addLast(() => t)
}
}

trait SegmentInstances {
implicit def segmentMonoidInstance[A]: Monoid[Segment[A, Unit]] = new Monoid[Segment[A, Unit]]{
Copy link
Member

Choose a reason for hiding this comment

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

We could define a Semigroup[Segment[A,R]] too, which might necessitate instance prioritization (though perhaps not).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could the Semigroup be non-implicit?

The implicit monoid allows for runFoldMonoid easily on the default case of segment.

Traverse[List].traverse(fa.force.toList)(f).map(Segment.seq)

def foldLeft[A, B](fa: Segment[A, Unit], b: B)(f: (B, A) => B): B =
fa.force.toList.foldLeft(b)(f)
Copy link
Member

Choose a reason for hiding this comment

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

Would be more efficient to implement this as fa.fold(b)(f).force.run I think

Foldable[List].foldRight(fa.force.toList, lb)(f)

def flatMap[A, B](fa: Segment[A, Unit])(f: (A) => Segment[B, Unit]): Segment[B, Unit] =
fa.flatMap(f).mapResult(_ => ())
Copy link
Member

Choose a reason for hiding this comment

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

Can use .voidResult as short cut for .mapResult(_ => ())

}

def segmentRMonad[T]: Monad[Segment[T, ?]] = new Monad[Segment[T, ?]]{
def flatMap[A, B](fa: Segment[T, A])(f: (A) => Segment[T, B]): Segment[T, B] =
Copy link
Member

Choose a reason for hiding this comment

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

Extra parens on (A) here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry 'twas my editor

def pure[A](x: A): Segment[A, Unit] = Segment(x)
}

def segmentRMonad[T]: Monad[Segment[T, ?]] = new Monad[Segment[T, ?]]{
Copy link
Member

Choose a reason for hiding this comment

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

How about def resultMonad

@mpilquist
Copy link
Member

2.11 build failed due to differences Either syntax being available from both cats and fs2. Change import cats.implicits._ to import cats.implicits.{ catsSyntaxEither => _, _ } to fix.

@mpilquist mpilquist merged commit 52f1b57 into typelevel:series/0.10 Dec 9, 2017
@aeons
Copy link
Member

aeons commented Dec 11, 2017

What's the reason for a Semigroup when we also have a Monoid?

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.

3 participants