Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Feb 27, 2024
1 parent c40e645 commit 15bbb0d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
36 changes: 23 additions & 13 deletions src/core/inequality.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ import language.experimental.captureChecking
object NumericallyComparable:
inline given numeric: Inequality[Boolean, Int | Double | Char | Byte | Short | Float | Long] with
inline def compare
(inline left: Boolean, inline right: Int | Double | Char | Byte | Short | Float | Long,
inline strict: Boolean, inline greaterThan: Boolean)
: Boolean =
( inline left: Boolean,
inline right: Int | Double | Char | Byte | Short | Float | Long,
inline strict: Boolean,
inline greaterThan: Boolean )
: Boolean =

${Hypotenuse2.inequality('left, 'right, 'strict, 'greaterThan)}

given inequality: Inequality[ByteSize, ByteSize] with
inline def compare
(inline left: ByteSize, inline right: ByteSize, inline strict: Boolean, inline greaterThan: Boolean)
: Boolean =
: Boolean =

!strict && left.long == right.long || (left.long < right.long) ^ greaterThan

trait NumericallyComparable
Expand All @@ -52,13 +56,15 @@ trait CompareGreaterEqual[-LeftType, -RightType, +ResultType] extends Numericall


trait Inequality[-LeftType, -RightType]
extends CompareGreaterEqual[LeftType, RightType, Boolean], CompareLess[LeftType, RightType, Boolean],
CompareLessEqual[LeftType, RightType, Boolean], CompareGreater[LeftType, RightType, Boolean]:
extends
CompareGreaterEqual[LeftType, RightType, Boolean],
CompareLess[LeftType, RightType, Boolean],
CompareLessEqual[LeftType, RightType, Boolean],
CompareGreater[LeftType, RightType, Boolean]:

inline def compare
(inline left: LeftType, inline right: RightType, inline strict: Boolean,
inline greaterThan: Boolean)
: Boolean
(inline left: LeftType, inline right: RightType, inline strict: Boolean, inline greaterThan: Boolean)
: Boolean

inline def lessThan(inline left: LeftType, inline right: RightType): Boolean =
compare(left, right, true, false)
Expand All @@ -76,24 +82,28 @@ extension [LeftType](inline left: LeftType)
@targetName("lt")
inline infix def < [RightType, ResultType](inline right: RightType)
(using inline compareLess: CompareLess[LeftType, RightType, ResultType])
: ResultType =
: ResultType =

compareLess.lessThan(left, right)

@targetName("lte")
inline infix def <= [RightType, ResultType](inline right: RightType)
(using inline compareLessEqual: CompareLessEqual[LeftType, RightType, ResultType])
: ResultType =
: ResultType =

compareLessEqual.lessThanOrEqual(left, right)

@targetName("gt")
inline infix def > [RightType, ResultType](inline right: RightType)
(using inline compareGreater: CompareGreater[LeftType, RightType, ResultType])
: ResultType =
: ResultType =

compareGreater.greaterThan(left, right)

@targetName("gte")
inline infix def >= [RightType, ResultType](inline right: RightType)
(using inline compareGreaterEqual: CompareGreaterEqual[LeftType, RightType, ResultType])
: ResultType =
: ResultType =

compareGreaterEqual.greaterThanOrEqual(left, right)

9 changes: 6 additions & 3 deletions src/core/macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ object Hypotenuse2:
Expr(int.toByte)

def inequality
(expr: Expr[Boolean], bound: Expr[Int | Double | Char | Byte | Short | Long | Float],
strict: Expr[Boolean], greaterThan: Expr[Boolean])
( expr: Expr[Boolean],
bound: Expr[Int | Double | Char | Byte | Short | Long | Float],
strict: Expr[Boolean],
greaterThan: Expr[Boolean] )
(using Quotes)
: Expr[Boolean] =
: Expr[Boolean] =

val errorMessage = msg"this cannot be written as a range expression"

Expand All @@ -108,6 +110,7 @@ object Hypotenuse2:
case '{($bound: Float) > ($value: Float)} => value
case '{($bound: Float) >= ($value: Float)} => value
case _ => fail(errorMessage)

else expr match
case '{($bound: Int) < ($value: Int)} => value
case '{($bound: Int) <= ($value: Int)} => value
Expand Down

0 comments on commit 15bbb0d

Please sign in to comment.