Phil Trelford's Array
POKE 36879, 255

Units in Cells

May 3, 2011 13:57 by phil

Last year, inspired by F#’s built-in compile-time Units of Measure feature I developed a small run-time library for units.

Last week I created a short F# spreadsheet script, harvesting code from the open source project Cellz. The script is hosted on the F# Snippets site and can be run inside your browser using the Try F# Silverlight-based web app.

Over the last few days I’ve put the 2 together. The Silverlight application below demonstrates the spreadsheet script and the run-time units library combined.

 

Inside the spreadsheet units can be appended to numbers, e.g.

  • = 1m
  • = 3m^2/s
  • = 5kW/h

Type signatures:

type UnitType =
  | Empty
  | Unit of string * int
  | CompositeUnit of UnitType list
  with
    override ToString : unit -> string
    static member ( + ) : lhs:UnitType * rhs:UnitType -> UnitType
    static member ( / ) : lhs:UnitType * rhs:UnitType -> UnitType
    static member ( * ) : lhs:UnitType * rhs:UnitType -> UnitType
    static member ( * ) : v:ValueType * u:UnitType -> UnitValue
  end
and ValueType = decimal
and UnitValue =
  class
    interface IComparable
    new : v:ValueType -> UnitValue
    new : v:ValueType * s:string -> UnitValue
    new : v:ValueType * u:UnitType -> UnitValue
    override Equals : that:obj -> bool
    override GetHashCode : unit -> int
    override ToString : unit -> string
    member Unit : UnitType
    member Value : ValueType
    static member One : UnitValue
    static member Pow : lhs:UnitValue * rhs:UnitValue -> UnitValue
    static member ( + ) : lhs:UnitValue * rhs:UnitValue -> UnitValue
    static member ( / ) : lhs:UnitValue * rhs:UnitValue -> UnitValue
    static member ( / ) : lhs:UnitValue * rhs:ValueType -> UnitValue
    static member ( / ) : v:UnitValue * u:UnitType -> UnitValue
    static member ( * ) : lhs:UnitValue * rhs:UnitValue -> UnitValue
    static member ( * ) : lhs:UnitValue * rhs:ValueType -> UnitValue
    static member ( * ) : v:UnitValue * u:UnitType -> UnitValue
    static member ( - ) : lhs:UnitValue * rhs:UnitValue -> UnitValue
    static member ( ~- ) : v:UnitValue -> UnitValue
  end

Resources:


Tags:
Categories: F# | Silverlight | .Net
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Comments

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading