Prototype of dynamic units and measurements algebra #217

Closed
CommanderTvis wants to merge 1 commits from commandertvis/units-poc into dev
CommanderTvis commented 2021-03-02 22:48:05 +03:00 (Migrated from github.com)
No description provided.
knok16 (Migrated from github.com) reviewed 2021-08-18 12:38:27 +03:00
@ -0,0 +84,4 @@
}
public fun divide(a: Measure, b: Measure): Measure =
multiply(a, b.copy(chain = b.chain.mapValues { (_, v) -> -v }))
knok16 (Migrated from github.com) commented 2021-08-18 12:38:27 +03:00

should .copy also update multiplier to 1 / multiplier?

should `.copy` also update `multiplier` to `1 / multiplier`?
knok16 (Migrated from github.com) reviewed 2021-08-18 12:43:24 +03:00
@ -0,0 +8,4 @@
/**
* Represents base units of International System of Units.
*/
public enum class BaseUnits {
knok16 (Migrated from github.com) commented 2021-08-18 12:43:23 +03:00

Enums are not very extensible, so if the library will lack some base units, users will no have a choice rather than wait for an update in the library. What about converting BaseUnits into an interface, so any library client will be able to define their own base units, like:

object Bit : BaseUnit

public val bit: Measure = Measure(Bit to 1)
public val bitrate: Measure = MeasureAlgebra { bit / s }
Enums are not very extensible, so if the library will lack some base units, users will no have a choice rather than wait for an update in the library. What about converting `BaseUnits` into an interface, so any library client will be able to define their own base units, like: ``` object Bit : BaseUnit public val bit: Measure = Measure(Bit to 1) public val bitrate: Measure = MeasureAlgebra { bit / s } ```
knok16 (Migrated from github.com) reviewed 2021-08-18 13:08:01 +03:00
@ -0,0 +225,4 @@
"The units are incompatible. The chains are ${a.measure.chain} and ${b.measure.chain}"
}
return a.copy(value = algebra { a.value * a.measure.multiplier + b.value * b.measure.multiplier })
knok16 (Migrated from github.com) commented 2021-08-18 13:08:01 +03:00

I think code should convert b.value from scale used in b to scale used in a so it be something like a.value + b.value * b.measure.multiplier / a.measure.multiplier?

test cases:

  • (2 * gramm) + (0 * gramm) should be equals to (2 * gramm)
  • addition of meters and foots
I think code should convert `b.value` from scale used in `b` to scale used in `a` so it be something like `a.value + b.value * b.measure.multiplier / a.measure.multiplier`? test cases: - `(2 * gramm) + (0 * gramm)` should be equals to `(2 * gramm)` - addition of meters and foots
knok16 (Migrated from github.com) reviewed 2021-08-18 13:13:45 +03:00
@ -0,0 +215,4 @@
public fun <T> Algebra<T>.measurement(): MeasurementAlgebra<T> = MeasurementAlgebra(this)
public open class MeasurementSpace<T, out A>(public override val algebra: A) : MeasurementAlgebra<T>(algebra),
knok16 (Migrated from github.com) commented 2021-08-18 13:13:45 +03:00

What do you think about adding function to converting measurement from one unit to another, e.g.:

fun Measurement<T>.convertTo(measure: Measure): Measurement<T> {
    require(this.measure.chain == measure.chain) {...}
    return Measurement (
        measure = measure,
        value = this.value * this.measure.multiplier / measure.multiplier
    )
}
What do you think about adding function to converting measurement from one unit to another, e.g.: ``` fun Measurement<T>.convertTo(measure: Measure): Measurement<T> { require(this.measure.chain == measure.chain) {...} return Measurement ( measure = measure, value = this.value * this.measure.multiplier / measure.multiplier ) } ```
CommanderTvis commented 2021-08-18 14:15:30 +03:00 (Migrated from github.com)

@knok16 Thank you for commenting, I'll revise this branch after finishing with #402.

@knok16 Thank you for commenting, I'll revise this branch after finishing with #402.

Pull request closed

Sign in to join this conversation.
No description provided.