Benutzer:MovGP0/F♯/Pattern Matching
< Benutzer:MovGP0 | F♯
MovGP0 | Über mich | Hilfen | Artikel | Weblinks | Literatur | Zitate | Notizen | Programmierung | MSCert | Physik |
Pattern Matching[Bearbeiten | Quelltext bearbeiten]let sampleMatcher value =
match value with
| 0 -> "Zero"
| 1 -> "One"
| _ -> "Greater than one or less than zero"
Guards[Bearbeiten | Quelltext bearbeiten]let sampleMatcher value =
match value with
| 0 -> "Zero"
| 1 -> "One"
| x where x > 1 -> "Greater than one"
| _ -> "Less than one"
Active Pattern[Bearbeiten | Quelltext bearbeiten]let (|Negative|Positive|) number =
if number >= 0.0
then Positive
else Negative
let testNumber (number:float) =
match number with
| Positive -> sprintf "%f is a positive number" number
| Negative -> sprintf "%f is a negative number" number
let (|Limit|Market|Invalid|) (order:Order) =
if order.Type = OrderType.Limit && order.Price > 0.0
then Limit
else if order.Type = OrderType.Market && order.Price = 0.0
then Market
else Invalid
let TestOrder (order:Order) =
match order with
| Market -> sprintf "Market order"
| Limit -> sprintf "Limit order"
| Invalid -> sprintf "Invalid order"
Partial Active Patten[Bearbeiten | Quelltext bearbeiten]let (|Integer|_|) str =
match System.Int32.TryParse str with
| (true, number) -> Some number
| _ -> None
let (|Double|_|) str =
match System.Double.TryParse str with
| (true, number) -> Some number
| _ -> None
let testParse numberStr =
match numberStr with
| Integer number -> sprintf "Parsed an integer '%A'" number
| Double number -> sprintf "Parsed a double '%A'" number
| _ -> sprintf "Could't parse string '%A'" numberStr
|