-
Notifications
You must be signed in to change notification settings - Fork 114
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 all the typeclasses instance for the zio.prelude.data.Optional
type (copied from the Option
instances)
#1415
Changes from 8 commits
3dc9c9e
8e487dd
38ddf20
c170927
3f69470
ce44b2a
f21766f
3cbfdcc
6afb261
120dcd9
f5c8dbf
17fd13e
b54a7c4
0d843e8
9b2713a
c6469a0
e2af41b
a4033de
1227a73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
package zio.prelude | ||
|
||
import zio.prelude.data.Optional | ||
import zio.{Cause, Chunk, Exit, NonEmptyChunk} | ||
|
||
import scala.util.Try | ||
|
@@ -100,6 +101,15 @@ object Derive { | |
Equal.OptionEqual | ||
} | ||
|
||
/** | ||
* The [[DeriveEqual]] instance for [[Optional]]. | ||
*/ | ||
implicit val OptionalDeriveEqual: DeriveEqual[Optional] = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sideeffffect I didn't find the tests for this typeclass 🤔 |
||
new DeriveEqual[Optional] { | ||
def derive[A: Equal]: Equal[Optional[A]] = | ||
Equal.OptionalEqual | ||
} | ||
|
||
/** | ||
* The `DeriveEqual` instance for `ParSeq`. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,8 @@ package zio.prelude | |
|
||
import zio.Exit.{Failure, Success} | ||
import zio.prelude.coherent.{HashOrd, HashPartialOrd} | ||
import zio.{Cause, Chunk, Duration => ZIODuration, Exit, FiberId, NonEmptyChunk, StackTrace} | ||
import zio.prelude.data.Optional | ||
import zio.{Cause, Chunk, Exit, FiberId, NonEmptyChunk, StackTrace, Duration => ZIODuration} | ||
|
||
import scala.annotation.{implicitNotFound, nowarn} | ||
import scala.concurrent.duration.{Duration => ScalaDuration} | ||
|
@@ -353,6 +354,16 @@ object Equal extends EqualVersionSpecific { | |
case _ => false | ||
} | ||
|
||
/** | ||
* Derives an `Equal[Optional[A]]` given an `Equal[A]`. | ||
*/ | ||
implicit def OptionalEqual[A: Equal]: Equal[Optional[A]] = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sideeffffect I didn't find the tests for this typeclass 🤔 |
||
make { | ||
case (Optional.Absent, Optional.Absent) => true | ||
case (Optional.Present(a1), Optional.Present(a2)) => a1 === a2 | ||
case _ => false | ||
} | ||
|
||
/** | ||
* `PartialOrd` and `Hash` (and thus also `Equal`) instance for `Set[A]` values. | ||
* Due to the limitations of Scala's `Set`, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,7 @@ sealed trait Optional[+A] { self => | |
case Optional.Absent => ifEmpty | ||
} | ||
|
||
final def flatten[B](implicit ev: A <:< Option[B]): Option[B] = | ||
final def flatten[B](implicit ev: A <:< Optional[B]): Optional[B] = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't understand why the evidence what proving that |
||
self match { | ||
case Optional.Present(get) => ev(get) | ||
case Optional.Absent => None | ||
|
@@ -119,11 +119,8 @@ sealed trait Optional[+A] { self => | |
case Optional.Absent => Optional.Absent | ||
} | ||
|
||
final def orElse[B >: A](other: Optional[B]): Optional[B] = | ||
self match { | ||
case Optional.Present(_) => self | ||
case Optional.Absent => other | ||
} | ||
final def orElse[B >: A](alternative: => Optional[B]): Optional[B] = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
if (isEmpty) alternative else this | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code copied from |
||
|
||
final def iterator: Iterator[A] = | ||
self match { | ||
|
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.
Just optimizing the code here