1 people like it.

Simple example how to use option type

For tutorial, example how to use option type.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
let echo x = "You have to work " + x.ToString() + " hours."

let ``working hours`` =
    let day = (int System.DateTime.Now.DayOfWeek)
    match day with
    | 0 | 6 -> None
    | _ -> Some(7.5)

let echoHours1 = 
    match ``working hours`` with
    | None -> ""
    | Some h -> echo h

let echoHours2 = 
    ``working hours`` 
    |> Option.map(fun h -> echo h)

let echoHours3 = 
    ``working hours`` 
    |> Option.map(echo)
val echo : x:'a -> string

Full name: Script.echo
val x : 'a
System.Object.ToString() : string
val ( working hours ) : float option

Full name: Script.( working hours )
val day : int
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<_>
namespace System
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

Full name: System.DateTime

--------------------
System.DateTime()
   (+0 other overloads)
System.DateTime(ticks: int64) : unit
   (+0 other overloads)
System.DateTime(ticks: int64, kind: System.DateTimeKind) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, calendar: System.Globalization.Calendar) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: System.DateTimeKind) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: System.Globalization.Calendar) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: System.DateTimeKind) : unit
   (+0 other overloads)
property System.DateTime.Now: System.DateTime
property System.DateTime.DayOfWeek: System.DayOfWeek
union case Option.None: Option<'T>
union case Option.Some: Value: 'T -> Option<'T>
val echoHours1 : string

Full name: Script.echoHours1
val h : float
val echoHours2 : string option

Full name: Script.echoHours2
module Option

from Microsoft.FSharp.Core
val map : mapping:('T -> 'U) -> option:'T option -> 'U option

Full name: Microsoft.FSharp.Core.Option.map
val echoHours3 : string option

Full name: Script.echoHours3
Raw view New version

More information

Link:http://fssnip.net/mk
Posted:2 years ago
Author:Tuomas Hietanen
Tags: option