Skip to content

Commit

Permalink
move refined syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
erikerlandson committed Apr 23, 2024
1 parent 5853697 commit a612230
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 71 deletions.
12 changes: 7 additions & 5 deletions refined/src/main/scala/coulomb/integrations/refined/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
* limitations under the License.
*/

package coulomb.policy.overlay.refined
package coulomb.integrations.refined

object algebraic:
export coulomb.ops.algebra.refined.all.given
export coulomb.conversion.refined.value.given
export coulomb.conversion.refined.unit.given
object all:
export coulomb.integrations.refined.algebra.positive.given
export coulomb.integrations.refined.algebra.nonnegative.given
export coulomb.integrations.refined.algebra.either.given
export coulomb.integrations.refined.conversion.value.given
export coulomb.integrations.refined.conversion.unit.given
54 changes: 54 additions & 0 deletions refined/src/main/scala/coulomb/integrations/refined/syntax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2022 Erik Erlandson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package coulomb.integrations.refined

import coulomb.*
import coulomb.syntax.*

import eu.timepit.refined.*
import eu.timepit.refined.api.*

object syntax:
extension [V](v: V)
inline def withRefinedUnit[P, U](using
Validate[V, P]
): Quantity[Either[String, Refined[V, P]], U] =
refineV[P](v).withUnit[U]

/**
* Lift a raw value into a unit quantity with a Refined value type
* @tparam P
* the Refined predicate type (e.g. Positive, NonNegative)
* @tparam U
* the desired unit type
* @param v
* the raw value to lift
* @return
* a unit quantity whose value is refined by P
* {{{
* val dist = refineVU[NonNegative, Meter](1.0)
* }}}
*/
def refineVU[P, U]: infra.ApplyRefineVU[P, U] =
new infra.ApplyRefineVU[P, U]

object infra:
class ApplyRefineVU[P, U]:
def apply[V](v: V)(using
Validate[V, P]
): Quantity[Either[String, Refined[V, P]], U] =
refineV[P](v).withUnit[U]
53 changes: 0 additions & 53 deletions refined/src/main/scala/coulomb/syntax/refined.scala

This file was deleted.

16 changes: 3 additions & 13 deletions refined/src/test/scala/coulomb/quantity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ class RefinedQuantityAlgebraicSuite extends CoulombSuite:

import coulomb.*
import coulomb.syntax.*
import coulomb.syntax.refined.*

import algebra.instances.all.given

import coulomb.integrations.refined.all.given
import coulomb.integrations.refined.syntax.*

import coulomb.units.si.{*, given}
import coulomb.units.si.prefixes.{*, given}
import coulomb.units.mksa.{*, given}
Expand Down Expand Up @@ -98,8 +100,6 @@ class RefinedQuantityAlgebraicSuite extends CoulombSuite:
}

test("toValue") {
import coulomb.policy.overlay.refined.algebraic.given

1.withRP[Positive]
.withUnit[Meter]
.toValue[Refined[Double, Positive]]
Expand All @@ -126,8 +126,6 @@ class RefinedQuantityAlgebraicSuite extends CoulombSuite:
}

test("toUnit") {
import coulomb.policy.overlay.refined.algebraic.given

1d.withRP[Positive]
.withUnit[Kilo * Meter]
.toUnit[Meter]
Expand Down Expand Up @@ -155,8 +153,6 @@ class RefinedQuantityAlgebraicSuite extends CoulombSuite:
}

test("add") {
import coulomb.policy.overlay.refined.algebraic.given

(1d.withRP[Positive]
.withUnit[Meter] + 2d.withRP[Positive].withUnit[Meter])
.assertQ[Refined[Double, Positive], Meter](3d.withRP[Positive])
Expand Down Expand Up @@ -215,8 +211,6 @@ class RefinedQuantityAlgebraicSuite extends CoulombSuite:
}

test("multiply") {
import coulomb.policy.overlay.refined.algebraic.given

(2d.withRP[Positive]
.withUnit[Meter] * 3d.withRP[Positive].withUnit[Meter])
.assertQ[Refined[Double, Positive], Meter ^ 2](6d.withRP[Positive])
Expand Down Expand Up @@ -269,8 +263,6 @@ class RefinedQuantityAlgebraicSuite extends CoulombSuite:
}

test("divide") {
import coulomb.policy.overlay.refined.algebraic.given

(12d.withRP[Positive]
.withUnit[Meter] / 3d.withRP[Positive].withUnit[Second])
.assertQ[Refined[Double, Positive], Meter / Second](
Expand Down Expand Up @@ -331,8 +323,6 @@ class RefinedQuantityAlgebraicSuite extends CoulombSuite:
}

test("power") {
import coulomb.policy.overlay.refined.algebraic.given

// FractionalPower (algebras supporting rational exponents)
2d.withRP[Positive]
.withUnit[Meter]
Expand Down

0 comments on commit a612230

Please sign in to comment.