0 people like it.
Like the snippet!
Retail Domain
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
|
open System
// Retail domain on a single page
type Name = string
type Barcode = string
type Price = decimal
type Quantity = decimal
type Product = Product of Name * Barcode * Price
type LineItem =
| SaleLineItem of int * Product * Quantity
| CancelLine of int
type Basket private (items) =
member basket.AddItem(lineId, product, quantity) =
let item = SaleLineItem(lineId, product, quantity)
Basket(item::items)
member basket.CancelLine(id) =
let item = CancelLine(id)
Basket(item::items)
member basket.Total =
items
|> List.map (fun line ->
match line with
| SaleLineItem(_, Product(_, _, price), quantity) ->
price * quantity
| CancelLine id ->
items |> List.pick (fun line ->
match line with
| SaleLineItem(lineId, Product(_, _, price), quantity)
when lineId = id ->
Some(-1.0M * price * quantity)
| _ -> None ) )
|> List.sum
new() = Basket([])
// Product list and lookup
let products =
[ Product("Tea", "070177075101", 1.50M)
Product("Real-World Functional Programming", "9781933988924", 49.99M) ]
let lookup searchCode =
products |> List.tryFind (fun (Product(_, code, _)) ->
code = searchCode)
// Main loop
let rec readItems lineId (basket:Basket) =
printfn "Enter item %d: " lineId
let search = Console.ReadLine()
if String.IsNullOrEmpty search then
printfn "Total: %A" basket.Total
elif search.StartsWith("-") then
let id = -1 * (int search)
readItems lineId (basket.CancelLine(id))
else
let prod = lookup search
match prod with
| Some(prod) ->
readItems (lineId + 1) (basket.AddItem(lineId, prod, 1.0M))
| _ ->
readItems lineId basket
readItems 0 (Basket())
|
namespace System
type Name = string
Full name: Script.Name
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = String
Full name: Microsoft.FSharp.Core.string
type Barcode = string
Full name: Script.Barcode
type Price = decimal
Full name: Script.Price
Multiple items
val decimal : value:'T -> decimal (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.decimal
--------------------
type decimal = Decimal
Full name: Microsoft.FSharp.Core.decimal
--------------------
type decimal<'Measure> = decimal
Full name: Microsoft.FSharp.Core.decimal<_>
type Quantity = decimal
Full name: Script.Quantity
Multiple items
union case Product.Product: Name * Barcode * Price -> Product
--------------------
type Product = | Product of Name * Barcode * Price
Full name: Script.Product
type LineItem =
| SaleLineItem of int * Product * Quantity
| CancelLine of int
Full name: Script.LineItem
union case LineItem.SaleLineItem: int * Product * Quantity -> LineItem
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
union case LineItem.CancelLine: int -> LineItem
Multiple items
type Basket =
new : unit -> Basket
private new : items:LineItem list -> Basket
member AddItem : lineId:int * product:Product * quantity:Quantity -> Basket
member CancelLine : id:int -> Basket
member Total : decimal
Full name: Script.Basket
--------------------
new : unit -> Basket
private new : items:LineItem list -> Basket
val items : LineItem list
val basket : Basket
member Basket.AddItem : lineId:int * product:Product * quantity:Quantity -> Basket
Full name: Script.Basket.AddItem
val lineId : int
val product : Product
val quantity : Quantity
val item : LineItem
member Basket.CancelLine : id:int -> Basket
Full name: Script.Basket.CancelLine
val id : int
member Basket.Total : decimal
Full name: Script.Basket.Total
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 line : LineItem
val price : Price
val pick : chooser:('T -> 'U option) -> list:'T list -> 'U
Full name: Microsoft.FSharp.Collections.List.pick
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
val sum : list:'T list -> 'T (requires member ( + ) and member get_Zero)
Full name: Microsoft.FSharp.Collections.List.sum
val products : Product list
Full name: Script.products
val lookup : searchCode:Barcode -> Product option
Full name: Script.lookup
val searchCode : Barcode
val tryFind : predicate:('T -> bool) -> list:'T list -> 'T option
Full name: Microsoft.FSharp.Collections.List.tryFind
val code : Barcode
val readItems : lineId:int -> basket:Basket -> unit
Full name: Script.readItems
Multiple items
type Basket =
new : unit -> Basket
private new : items:LineItem list -> Basket
member AddItem : lineId:int * product:Product * quantity:Quantity -> Basket
member CancelLine : id:int -> Basket
member Total : decimal
Full name: Script.Basket
--------------------
new : unit -> Basket
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val search : string
type Console =
static member BackgroundColor : ConsoleColor with get, set
static member Beep : unit -> unit + 1 overload
static member BufferHeight : int with get, set
static member BufferWidth : int with get, set
static member CapsLock : bool
static member Clear : unit -> unit
static member CursorLeft : int with get, set
static member CursorSize : int with get, set
static member CursorTop : int with get, set
static member CursorVisible : bool with get, set
...
Full name: System.Console
Console.ReadLine() : string
Multiple items
type String =
new : value:char -> string + 7 overloads
member Chars : int -> char
member Clone : unit -> obj
member CompareTo : value:obj -> int + 1 overload
member Contains : value:string -> bool
member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
member EndsWith : value:string -> bool + 2 overloads
member Equals : obj:obj -> bool + 2 overloads
member GetEnumerator : unit -> CharEnumerator
member GetHashCode : unit -> int
...
Full name: System.String
--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
String.IsNullOrEmpty(value: string) : bool
property Basket.Total: decimal
String.StartsWith(value: string) : bool
String.StartsWith(value: string, comparisonType: StringComparison) : bool
String.StartsWith(value: string, ignoreCase: bool, culture: Globalization.CultureInfo) : bool
member Basket.CancelLine : id:int -> Basket
val prod : Product option
val prod : Product
member Basket.AddItem : lineId:int * product:Product * quantity:Quantity -> Basket
More information