Skip to content

Instantly share code, notes, and snippets.

@kekyo

kekyo/option.fs

Created Jul 3, 2017
Embed
What would you like to do?
F# option computation expression
[<Struct>]
type OptionalBuilder =
member __.Bind(opt, binder) =
match opt with
| Some value -> binder value
| None -> None
member __.Return(value) =
Some value
let optional = OptionalBuilder()
type Part = {
Type: System.Type;
Format: int option;
Name: string;
Part: int16;
Number: float32;
FileName: string option
}
type PartResult = {
ty: System.Type;
format: string;
name: string;
part: int16;
number: float32;
fileName: string
}
let truncateString length (s: string) =
if s.Length > length then s.Substring(length) else s
let part = Some { Type = typeof<byte>; Format = Some 123; Name = "ABC"; Part = 456s; Number = 123.456f; FileName = Some "aaa.txt" }
let foo = optional {
let! p = part
let! f = p.Format
let! fn = p.FileName
return { ty = p.Type; format = f |> string; name = p.Name; part = p.Part; number = p.Number; fileName = fn |> truncateString 254 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.