Skip to content

Instantly share code, notes, and snippets.

@kekyo
Created July 3, 2017 23:51
Show Gist options
  • Save kekyo/cadc0ec4b016368a0cee81d87fbee63a to your computer and use it in GitHub Desktop.
Save kekyo/cadc0ec4b016368a0cee81d87fbee63a to your computer and use it in GitHub Desktop.
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