GenUnits


Create a ValueUnit

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
open Informedica.GenUnits.Lib
open Informedica.GenUnits.Lib.Unit.Units

module CU = CombiUnit
module VU = ValueUnit
module UG = UnitGroup

let ``500 mg`` = VU.create 500N (1N |> CU.withUnit milliGram)
let ``2 dd``   = VU.create 2N  (1N  |> CU.withUnit count |> CU.per 1N day)

Print out the value units

1: 
2: 
``500 mg`` |> VU.toString
``2 dd``   |> VU.toString
  • hide *
1: 
2: 
``500 mg`` |> VU.toString |> print
``2 dd``   |> VU.toString |> print

Output:

500 mg[Mass]
2 X[Count]/day[Time]

Perform a calculation: 2 X/day * 500 mg = 1000 mg/day

1: 
2: 
let tot = ``2 dd`` * ``500 mg`` 
tot |> VU.toString
  • hide *
1: 
tot |> VU.toString |> print

Output:

1000 mg[Mass]/day[Time]

Convert 1000 mg/day to g/week

1: 
2: 
let ``gram/week`` = 1N |> CU.withUnit gram |> CU.per 1N week
tot |> VU.convertTo ``gram/week`` |> VU.toString
  • hide *
1: 
tot |> VU.convertTo ``gram/week`` |> VU.toString |> print

Output:

7 g[Mass]/week[Time]

Create a combi unit directly from a string

1: 
"mg[Mass]/kg[Weight]/2 day[Time]" |> CU.fromString
 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
Combi
(1N,{Group = Name "Mass"; 
        Name = (Name "MilliGram", []); 
        Abbreviation = (Name "mg", []); 
        Multiplier = 1/1000N;}, 
    [(Per, 1N, {Group = Name "Kg"; 
                Name = (Name "kg", []); 
                Abbreviation = (Name "kg", []); 
                Multiplier = 1N;}); 
    (Per, 2N, {Group = Name "Time"; 
                Name = (Name "Day", []); 
                Abbreviation = (Name "day", []); 
                Multiplier = 86400N;})])

And a value with a unit

1: 
2: 
3: 
"20 mg[Mass]/kg[Weight]/2 day[Time]" |> VU.fromString
tot |> VU.convertTo ``gram/week`` |> VU.toString |> VU.fromString
``2 dd`` |> VU.toString |> VU.fromString
1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
ValueUnit 
(2N, 
    Combi (1N,{Group = Name "Count"; 
            Name = (Name "Times", []);  
            Abbreviation = (Name "X", []); 
            Multiplier = 1N;},[(Per, 1N, {Group = Name "Time"; 
                                            Name = (Name "Day", []); 
                                            Abbreviation = (Name "day", []); 
                                            Multiplier = 86400N;})])) 

Evaluate an expression

1: 
"2 mg[Mass] * 3 X[Count]/day[Time]" |> Api.eval

"6 mg[Mass]/day[Time]"

1: 
2: 
3: 
4: 
5: 
6: 
let conc = "200 mg[Mass] / 50 ml[Volume]" |> Api.eval
let rate = "2 mL[Mass]/hour[Time]"      |> Api.eval
let dose = 
    rate + " * " + conc + " / 60 kg[Weight]" 
    |> Api.eval
    |> Api.convert "mcg[Mass]/kg[Weight]/min[Time]"

val conc : string = "4 mg[Mass]/ml[Volume]"
val rate : string = "2 ml[Ml]/hr[Time]"
val dose : string = "20000/9 mcg[Mass]/kg[Kg]/min[Time]"

Calculating with Unit Groups

It is also possible to perform 'unit group calculations'

1: 
2: 
3: 
4: 
5: 
6: 
let ug1 = "Mass" |> UG.fromString
let ug2 = "Weight" |> UG.fromString
let ug3 = "Time" |> UG.fromString

let cg = ug1 / ug2 / ug3
cg |> UG.toString

Results in:

val it : string = "Mass/Weight/Time"

From the combined unit group a list of possible units can be generated

1: 
2: 
3: 
cg |> UG.getUnits
|> List.map CU.toString
|> List.iter (printfn "> %s </br>") 

Results in:

kg[Mass]/kg[Weight]/sec[Time]
kg[Mass]/kg[Weight]/min[Time]
kg[Mass]/kg[Weight]/hr[Time]

....

nanog[Mass]/g[Weight]/week[Time]
nanog[Mass]/g[Weight]/mo[Time]
nanog[Mass]/g[Weight]/yr[Time]

When a unit is 'unknown', i.e. not defined in the library, a special unit group is created.

1: 
2: 
"tablet(Shape)" 
|> CU.fromString
1: 
2: 
3: 
4: 
 CombiUnit.CombiUnit = Combi (1N,{Group = Name "Shape"; 
                                  Name = (Name "tablet", []); 
                                  Abbreviation = (Name "tablet", []); 
                                  Multiplier = 1N;},[]) 
namespace Informedica
namespace Informedica.GenUtils
namespace Informedica.GenUtils.Lib
namespace Informedica.GenUtils.Lib.BCL
namespace Informedica.GenUnits
namespace Informedica.GenUnits.Lib
module Constants

from Informedica.GenUnits.Lib
val print : s:string -> unit

Full name: Tutorial.print
val s : string
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
module Unit

from Informedica.GenUnits.Lib
module Units

from Informedica.GenUnits.Lib.UnitModule
module CombiUnit

from Informedica.GenUnits.Lib
module ValueUnit

from Informedica.GenUnits.Lib
module UnitGroup

from Informedica.GenUnits.Lib
val ( 500 mg ) : ValueUnit.ValueUnit

Full name: Tutorial.( 500 mg )
val create : v:BigRational -> u:CombiUnit.CombiUnit -> ValueUnit.ValueUnit

Full name: Informedica.GenUnits.Lib.ValueUnitModule.create
val withUnit : u:Unit.Unit -> v:BigRational -> CombiUnit.CombiUnit

Full name: Informedica.GenUnits.Lib.CombiUnitModule.withUnit
val milliGram : Unit.Unit

Full name: Informedica.GenUnits.Lib.UnitModule.Units.milliGram
val ( 2 dd ) : ValueUnit.ValueUnit

Full name: Tutorial.( 2 dd )
val count : Unit.Unit

Full name: Informedica.GenUnits.Lib.UnitModule.Units.count
val per : (BigRational -> Unit.Unit -> CombiUnit.CombiUnit -> CombiUnit.CombiUnit)

Full name: Informedica.GenUnits.Lib.CombiUnitModule.per
val day : Unit.Unit

Full name: Informedica.GenUnits.Lib.UnitModule.Units.day
val toString : vu:ValueUnit.ValueUnit -> string

Full name: Informedica.GenUnits.Lib.ValueUnitModule.toString
val tot : ValueUnit.ValueUnit

Full name: Tutorial.tot
val ( gram/week ) : CombiUnit.CombiUnit

Full name: Tutorial.( gram/week )
val gram : Unit.Unit

Full name: Informedica.GenUnits.Lib.UnitModule.Units.gram
val week : Unit.Unit

Full name: Informedica.GenUnits.Lib.UnitModule.Units.week
val convertTo : cu:CombiUnit.CombiUnit -> vu:ValueUnit.ValueUnit -> ValueUnit.ValueUnit

Full name: Informedica.GenUnits.Lib.ValueUnitModule.convertTo
val fromString : s:System.String -> CombiUnit.CombiUnit

Full name: Informedica.GenUnits.Lib.CombiUnitModule.fromString
val fromString : s:string -> ValueUnit.ValueUnit

Full name: Informedica.GenUnits.Lib.ValueUnitModule.fromString
module Api

from Informedica.GenUnits.Lib
val eval : s:System.String -> string

Full name: Informedica.GenUnits.Lib.Api.eval
val conc : string

Full name: Tutorial.conc
val rate : string

Full name: Tutorial.rate
val dose : string

Full name: Tutorial.dose
val convert : s2:System.String -> s1:string -> string

Full name: Informedica.GenUnits.Lib.Api.convert
val ug1 : UnitGroup.UnitGroup

Full name: Tutorial.ug1
val fromString : s:System.String -> UnitGroup.UnitGroup

Full name: Informedica.GenUnits.Lib.UnitGroupModule.fromString
val ug2 : UnitGroup.UnitGroup

Full name: Tutorial.ug2
val ug3 : UnitGroup.UnitGroup

Full name: Tutorial.ug3
val cg : UnitGroup.UnitGroup

Full name: Tutorial.cg
val toString : ug:UnitGroup.UnitGroup -> string

Full name: Informedica.GenUnits.Lib.UnitGroupModule.toString
val getUnits : ug:UnitGroup.UnitGroup -> CombiUnit.CombiUnit list

Full name: Informedica.GenUnits.Lib.UnitGroupModule.getUnits


 Get all possible `CombiUnit` unit combinations
 belonging to a `UnitGroup` **ung**
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val toString : cu:CombiUnit.CombiUnit -> string

Full name: Informedica.GenUnits.Lib.CombiUnitModule.toString
val iter : action:('T -> unit) -> list:'T list -> unit

Full name: Microsoft.FSharp.Collections.List.iter
Fork me on GitHub