7 people like it.
Like the snippet!
Point of Sale
10 line Point of Sale (POS) application takes barcodes from a USB barcode scanner or keyboard adding matching products. Entering an empty string completes the action and gives the total.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
|
let products = ["5000171002525","Tuna", 0.59M; "54491014", "Coke", 0.33M]
let stream = Seq.initInfinite (fun _ -> printf ">"; System.Console.ReadLine())
let scans = stream |> Seq.takeWhile (fun s -> s.Length > 0)
let items =
scans |> Seq.fold (fun items scan ->
match products |> List.tryFind (fun (code,_,_) -> scan = code) with
| Some((_,n,p) as product) -> printfn "%s\t@ %A" n p; product::items
| None -> printfn "Not found"; items
) []
items |> List.sumBy (fun (_,_,price) -> price) |> printf "Total %A"
|
val products : (string * string * decimal) list
Full name: Script.products
val stream : seq<string>
Full name: Script.stream
module Seq
from Microsoft.FSharp.Collections
val initInfinite : initializer:(int -> 'T) -> seq<'T>
Full name: Microsoft.FSharp.Collections.Seq.initInfinite
val printf : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
namespace System
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
System.Console.ReadLine() : string
val scans : seq<string>
Full name: Script.scans
val takeWhile : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Collections.Seq.takeWhile
val s : string
property System.String.Length: int
val items : (string * string * decimal) list
Full name: Script.items
val fold : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State
Full name: Microsoft.FSharp.Collections.Seq.fold
val items : (string * string * decimal) list
val scan : string
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 tryFind : predicate:('T -> bool) -> list:'T list -> 'T option
Full name: Microsoft.FSharp.Collections.List.tryFind
val code : string
union case Option.Some: Value: 'T -> Option<'T>
val n : string
val p : decimal
val product : string * string * decimal
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
union case Option.None: Option<'T>
val sumBy : projection:('T -> 'U) -> list:'T list -> 'U (requires member ( + ) and member get_Zero)
Full name: Microsoft.FSharp.Collections.List.sumBy
val price : decimal
More information