Starbucks

Simple DSL for describing cups of Starbucks coffee and computing prices (in dollars).

Copy Source
Copy Link
Tools:
 1: module Starbucks
 2: 
 3: type size = Tall | Grande | Venti
 4: type drink = Latte | Cappuccino | Mocha | Americano
 5: type extra = Shot | Syrup
 6: 
 7: type Cup = { Size:size; Drink:drink; Extras:extra list } with
 8:     static member (+) (cup:Cup,extra:extra) =
 9:         { cup with Extras = extra :: cup.Extras }
10:     static member Of size drink =
11:         { Size=size; Drink=drink; Extras=[] }
12:     
13: let Price (cup:Cup) =
14:     let tall, grande, venti = 
15:         match cup.Drink with
16:         | Latte      -> 2.69, 3.19, 3.49
17:         | Cappuccino -> 2.69, 3.19, 3.49
18:         | Mocha      -> 2.99, 3.49, 3.79
19:         | Americano  -> 1.89, 2.19, 2.59
20:     let basePrice =
21:         match cup.Size with
22:         | Tall -> tall 
23:         | Grande -> grande
24:         | Venti -> venti
25:     let extras =
26:         cup.Extras |> List.sumBy (function
27:             | Shot -> 0.59
28:             | Syrup -> 0.39
29:         )
30:     basePrice + extras
31: 
32: let myCup = Cup.Of Venti Latte + Syrup
33: let price = Price myCup
module Starbucks
type size =
  | Tall
  | Grande
  | Venti

Full name: Starbucks.size

  type: size
  implements: System.IEquatable<size>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<size>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
union case size.Tall: size
union case size.Grande: size
union case size.Venti: size
type drink =
  | Latte
  | Cappuccino
  | Mocha
  | Americano

Full name: Starbucks.drink

  type: drink
  implements: System.IEquatable<drink>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<drink>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
union case drink.Latte: drink
union case drink.Cappuccino: drink
union case drink.Mocha: drink
union case drink.Americano: drink
type extra =
  | Shot
  | Syrup

Full name: Starbucks.extra

  type: extra
  implements: System.IEquatable<extra>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<extra>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
union case extra.Shot: extra
union case extra.Syrup: extra
type Cup =
  {Size: size;
   Drink: drink;
   Extras: extra list;}
  with
    static member Of : size:size -> drink:drink -> Cup
    static member ( + ) : cup:Cup * extra:extra -> Cup
  end

Full name: Starbucks.Cup

  type: Cup
  implements: System.IEquatable<Cup>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Cup>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
Cup.Size: size
Cup.Drink: drink
Cup.Extras: extra list
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>

  type: 'T list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'T>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'T>
  implements: System.Collections.IEnumerable
val cup : Cup

  type: Cup
  implements: System.IEquatable<Cup>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Cup>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
Multiple items
val extra : extra

  type: extra
  implements: System.IEquatable<extra>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<extra>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable


--------------------

type extra =
  | Shot
  | Syrup

Full name: Starbucks.extra

  type: extra
  implements: System.IEquatable<extra>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<extra>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
static member Cup.Of : size:size -> drink:drink -> Cup

Full name: Starbucks.Cup.Of
Multiple items
val size : size

  type: size
  implements: System.IEquatable<size>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<size>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable


--------------------

type size =
  | Tall
  | Grande
  | Venti

Full name: Starbucks.size

  type: size
  implements: System.IEquatable<size>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<size>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
Multiple items
val drink : drink

  type: drink
  implements: System.IEquatable<drink>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<drink>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable


--------------------

type drink =
  | Latte
  | Cappuccino
  | Mocha
  | Americano

Full name: Starbucks.drink

  type: drink
  implements: System.IEquatable<drink>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<drink>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
val Price : Cup -> float

Full name: Starbucks.Price
val tall : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val grande : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val venti : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val basePrice : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
val extras : float

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------

type List<'T> =
  | ( [] )
  | ( :: ) of 'T * 'T list
  with
    interface System.Collections.IEnumerable
    interface System.Collections.Generic.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
  end

Full name: Microsoft.FSharp.Collections.List<_>

  type: List<'T>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'T>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'T>
  implements: System.Collections.IEnumerable
val sumBy : ('T -> 'U) -> 'T list -> 'U (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.List.sumBy
val myCup : Cup

Full name: Starbucks.myCup

  type: Cup
  implements: System.IEquatable<Cup>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<Cup>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
static member Cup.Of : size:size -> drink:drink -> Cup
val price : float

Full name: Starbucks.price

  type: float
  implements: System.IComparable
  implements: System.IFormattable
  implements: System.IConvertible
  implements: System.IComparable<float>
  implements: System.IEquatable<float>
  inherits: System.ValueType

More information

Link: http://fssnip.net/9w
Posted: 5 months ago
Author: Phillip Trelford (website)
Tags: DSL