F#2.0 series use option (option)

Source: Internet
Author: User

Use options (option)

type 'T option =
     | None
     | Some of 'T

Let's look at an example:

> let people = [ ("Adam", None);
                  ("Eve" , None);
                  ("Cain", Some("Adam","Eve"));
                  ("Abel", Some("Adam","Eve")) ];;
val people : (string * (string *string) option) list

Use pattern matching (patterns matching) to generate option:

> let showParents (name,parents) =
       match parents with
       | Some(dad,mum) -> printfn "%s has father %s, mother %s" name dad mum
       | None          -> printfn "%s has no parents!" name;;
val showParents : (string * (string * string) option) -> unit

> showParents people.[0];;
Adam has no parents
val it : unit = ()

Some useful methods for option:

Method Type Describe
Option.get ' t option-> ' t Returns the value of a some type. or throw an exception
Option.isnone ' T option-> bool Returns whether an option is None
Option.map (' t-> ' u)-> ' t option-> ' u option If none is returned, none. If it is some (x), return some (f x), F is the given function
Option.iter (' t-> Unit]-> ' t option-> Unit Executes the specified method for option some type.

Some examples:

> Option.map(fun x->x) a;;
val it : (string * string) option = Some ("aa", "bb")
> Option.map(fun x-> match x with | (first,second) -> first) a;;
val it : string option = Some "aa"
> Option.map(fun x-> match x with | (first,second) -> second) a;;
val it : string option = Some "bb"
> 
> Option.iter(fun x-> match x with | (first:string,second) -> printfn "%s" (first+second)) a;;
aabb
val it : unit = ()

>

Use option type for control

Look at this example:

let fetch url =
     try Some (http url)
     with :? System.Net.WebException -> None

The HTTP function is the method that gets the HTML defined in the previous section. Throws a exception in the case of none. A successful access returns a some value, which is the value of the option type. We can then use option values for pattern matching:

> match (fetch "http://www.nature.com") with
   | Some text -> printfn "text = %s" text
   | None -> printfn "**** no web page found";;
text = <HTML> ... </HTML> (note: the HTML is shown here if connected to the web)
val it : unit = ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.