Benutzer:MovGP0/F♯/Pattern Matching
aus Wikipedia, der freien Enzyklopädie
< Benutzer:MovGP0 | F♯
MovGP0 | Über mich | Hilfen | Artikel | Weblinks | Literatur | Zitate | Notizen | Programmierung | MSCert | Physik |
Pattern Matchinglet sampleMatcher value =
match value with
| 0 -> "Zero"
| 1 -> "One"
| _ -> "Greater than one or less than zero"
Guardslet sampleMatcher value =
match value with
| 0 -> "Zero"
| 1 -> "One"
| x where x > 1 -> "Greater than one"
| _ -> "Less than one"
Active Patternlet (|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 Pattenlet (|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
|