Skip to content

Commit

Permalink
Some tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Oct 8, 2024
1 parent 00ee3e1 commit 072244d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
9 changes: 0 additions & 9 deletions src/core/quantitative-core.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,3 @@ extension [UnitsType <: Measure](inline quantity: Quantity[UnitsType])
inline def units: Map[Text, Int] = ${Quantitative.collectUnits[UnitsType]}
inline def render(using Decimalizer): Text = t"${quantity.value} ${Quantity.renderUnits(units)}"
inline def dimension: Text = ${Quantitative.describe[UnitsType]}

extension (value: Double)
@targetName("times")
infix def * [UnitsType <: Measure](quantity: Quantity[UnitsType]): Quantity[UnitsType] =
Quantity(quantity*value)

@targetName("divide")
transparent inline infix def / [UnitsType <: Measure](quantity: Quantity[UnitsType]): Any =
((1.0/value)*quantity).invert
33 changes: 24 additions & 9 deletions src/core/quantitative.Quantitative.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ object Quantitative extends Quantitative2:
given Quantity[Seconds[1]] is GenericDuration as genericDuration =
quantity => (quantity*1000.0).toLong

given [UnitsType <: Measure] => Numeric[Quantity[UnitsType]] as numeric =
summon[Numeric[Double]]

given Quantity[Seconds[1]] is SpecificDuration as specificDuration =
long => Quantity(long/1000.0)

Expand All @@ -73,19 +76,31 @@ object Quantitative extends Quantitative2:
=> Quantity[LeftType] is Multiplicable by Quantity[RightType] as multiplicable =
${Quantitative.mulTypeclass[LeftType, RightType]}

given [LeftType <: Measure] => Quantity[LeftType] is Multiplicable as multiplicable2:
type Operand = Double
type Result = Quantity[LeftType]
inline def multiply(left: Quantity[LeftType], right: Double): Quantity[LeftType] = left*right

given [LeftType <: Measure]
=> Quantity[LeftType] is Multiplicable by Double into Quantity[LeftType] as multiplicable2 =
new Multiplicable:
type Self = Quantity[LeftType]
type Operand = Double
type Result = Quantity[LeftType]

inline def multiply(left: Quantity[LeftType], right: Double): Quantity[LeftType] =
left*right

inline given [RightType <: Measure]
=> Double is Multiplicable by Quantity[RightType] into
Quantity[RightType] as multiplicable3 =
_*_

transparent inline given [LeftType <: Measure, RightType <: Measure]
=> Quantity[LeftType] is Divisible by Quantity[RightType] as divisible =
${Quantitative.divTypeclass[LeftType, RightType]}

given [LeftType <: Measure] => Quantity[LeftType] is Divisible:
type Result = Quantity[LeftType]
type Operand = Double
inline def divide(left: Quantity[LeftType], right: Double): Quantity[LeftType] = left/right
given [LeftType <: Measure] => Quantity[LeftType] is Divisible by Double as divisibleDouble =
new Divisible:
type Self = Quantity[LeftType]
type Result = Quantity[LeftType]
type Operand = Double
inline def divide(left: Quantity[LeftType], right: Double): Quantity[LeftType] = left/right

transparent inline given [ValueType <: Measure]
=> Quantity[ValueType] is Rootable[2] as squareRoot =
Expand Down

0 comments on commit 072244d

Please sign in to comment.