-
Notifications
You must be signed in to change notification settings - Fork 158
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
Added NotNaN
predicate
#596
Changes from 2 commits
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 | ||||
---|---|---|---|---|---|---|
|
@@ -41,6 +41,9 @@ object numeric extends NumericInference { | |||||
/** Predicate that checks if an integral value modulo `N` is `O`. */ | ||||||
final case class Modulo[N, O](n: N, o: O) | ||||||
|
||||||
/** Predicate that checks if a floating-point number value is not NaN. */ | ||||||
case class NotNaN() | ||||||
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. Other negated predicates use |
||||||
|
||||||
/** Predicate that checks if a numeric value is less than or equal to `N`. */ | ||||||
type LessEqual[N] = Not[Greater[N]] | ||||||
|
||||||
|
@@ -117,6 +120,14 @@ object numeric extends NumericInference { | |||||
Modulo(wn.fst, wo.fst) | ||||||
) | ||||||
} | ||||||
|
||||||
object NotNaN { | ||||||
implicit def floatNotNaNValidate: Validate.Plain[Float, NotNaN] = fromIsNaN(_.isNaN) | ||||||
implicit def doubleNotNaNValidate: Validate.Plain[Double, NotNaN] = fromIsNaN(_.isNaN) | ||||||
|
||||||
def fromIsNaN[A](isNaN: A => Boolean): Validate.Plain[A, NotNaN] = | ||||||
Validate.fromPredicate(x => !isNaN(x), x => s"$x != NaN", NotNaN()) | ||||||
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. There should be parenthesis in the string representation of this predicate.
Suggested change
Other predicates do this, too. |
||||||
} | ||||||
} | ||||||
|
||||||
private[refined] trait NumericInference { | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package eu.timepit.refined.types | ||
|
||
import eu.timepit.refined.api.{Refined, RefinedTypeOps} | ||
import eu.timepit.refined.numeric.{Negative, NonNegative, NonPositive, Positive} | ||
import eu.timepit.refined.numeric.{Negative, NonNegative, NonPositive, NotNaN, Positive} | ||
|
||
/** Module for numeric refined types. */ | ||
object numeric { | ||
|
@@ -165,6 +165,16 @@ object numeric { | |
type NonPosBigDecimal = BigDecimal Refined NonPositive | ||
|
||
object NonPosBigDecimal extends RefinedTypeOps[NonPosBigDecimal, BigDecimal] | ||
|
||
/** A `Float` that is not NaN. */ | ||
type FloatNotNaN = Float Refined NotNaN | ||
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. Since we have |
||
|
||
object FloatNotNaN extends RefinedTypeOps[FloatNotNaN, Float] | ||
|
||
/** A `Double` that is not NaN. */ | ||
type DoubleNotNaN = Double Refined NotNaN | ||
|
||
object DoubleNotNaN extends RefinedTypeOps[DoubleNotNaN, Double] | ||
} | ||
|
||
trait NumericTypes { | ||
|
@@ -266,3 +276,11 @@ trait NumericTypesBinCompat1 { | |
final type NonPosBigDecimal = numeric.NonPosBigDecimal | ||
final val NonPosBigDecimal = numeric.NonPosBigDecimal | ||
} | ||
|
||
trait NumericTypesBinCompat2 { | ||
final type FloatNotNaN = numeric.FloatNotNaN | ||
final val FloatNotNaN = numeric.FloatNotNaN | ||
|
||
final type DoubleNotNaN = numeric.DoubleNotNaN | ||
final val DoubleNotNaN = numeric.DoubleNotNaN | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -10,7 +10,7 @@ import org.scalacheck.Gen.Choose | |||||
* Module that provides `Arbitrary` instances and generators for | ||||||
* numeric predicates. | ||||||
*/ | ||||||
object numeric extends NumericInstances | ||||||
object numeric extends NumericInstances with NumericInstancesBinCompat1 | ||||||
|
||||||
trait NumericInstances { | ||||||
|
||||||
|
@@ -111,3 +111,15 @@ trait NumericInstances { | |||||
): Arbitrary[F[T, P]] = | ||||||
arbitraryRefType(Gen.chooseNum(min, max)) | ||||||
} | ||||||
|
||||||
trait NumericInstancesBinCompat1 { | ||||||
implicit def floatNotNaNArbitrary[F[_, _]: RefType]( | ||||||
implicit arb: Arbitrary[Float] | ||||||
): Arbitrary[F[Float, NotNaN]] = | ||||||
arbitraryRefType(arb.arbitrary.suchThat(x => !x.isNaN)) | ||||||
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. Minor nitpick: Using
Suggested change
|
||||||
|
||||||
implicit def doubleNotNaNArbitrary[F[_, _]: RefType]( | ||||||
implicit arb: Arbitrary[Double] | ||||||
): Arbitrary[F[Double, NotNaN]] = | ||||||
arbitraryRefType(arb.arbitrary.suchThat(x => !x.isNaN)) | ||||||
} |
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.
This should be
final
.