-
Notifications
You must be signed in to change notification settings - Fork 613
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
Conversation
def pure[A](x: A): Segment[A, Unit] = Segment(x) | ||
} | ||
|
||
def segmentRMonad[T]: Monad[Segment[T, ?]] = new Monad[Segment[T, ?]]{ |
There was a problem hiding this comment.
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
???
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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]]{ |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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(_ => ()) |
There was a problem hiding this comment.
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] = |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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, ?]]{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about def resultMonad
2.11 build failed due to differences |
What's the reason for a |
Includes:
Traverse
andMonad
implicitly for anySegment[A, Unit]
Monad[Segment[A, ?]]