You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/fsharp-cheatsheet.md
+31-15Lines changed: 31 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -216,10 +216,10 @@ The most common use is when you have a function that receives no parameters, but
216
216
let getCurrentDateTime = DateTime.Now
217
217
218
218
// This version evalautes DateTime.Now every time you call it with a `unit` argument.
219
-
let getCurrentDateTime2 () = DateTime.Now
219
+
let getCurrentDateTime2 () = DateTime.Now
220
220
221
221
// How to call the function:
222
-
let startTime = getCurrentDateTime2()
222
+
let startTime = getCurrentDateTime2()
223
223
```
224
224
225
225
<divid="functions-signatures"></div>
@@ -501,7 +501,7 @@ In C#, if a method has an `out` parameter (e.g. [`DateTime.TryParse`](https://le
501
501
let (success, outParsedDateTime) = System.DateTime.TryParse("2001/02/06")
502
502
```
503
503
504
-
See [Tuples (MS Learn)](https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/tuples)for learn more.
504
+
See [Tuples (MS Learn)](https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/tuples)to learn more.
505
505
506
506
<divid="data-types-records"></div>
507
507
@@ -876,20 +876,36 @@ match "yennefer@aretuza.org" with // output: "Email: yennefer@aretuza.org"
876
876
## Partial active patterns
877
877
878
878
*Partial active patterns* share the syntax of parameterized patterns, but their active recognizers accept only one argument.
879
-
A *Partial active pattern* must return an `Option<'T>`.
880
879
881
-
```fsharp
882
-
let (|DivisibleBy|_|) by n =
883
-
if n % by = 0
884
-
then Some DivisibleBy
885
-
else None
880
+
A partial active pattern typically returns an `Option<'T>`. However, as of [F# 9](https://learn.microsoft.com/en-us/dotnet/fsharp/whats-new/fsharp-9#partial-active-patterns-can-return-bool-instead-of-unit-option), where no value is being returned, and there is only one success case, you may return a `bool` instead.
886
881
887
-
let fizzBuzz = function
888
-
| DivisibleBy 3 & DivisibleBy 5 -> "FizzBuzz"
889
-
| DivisibleBy 3 -> "Fizz"
890
-
| DivisibleBy 5 -> "Buzz"
891
-
| i -> string i
892
-
```
882
+
-`Option<T>`
883
+
884
+
```fsharp
885
+
let (|DivisibleBy|_|) by n =
886
+
if n % by = 0
887
+
then Some DivisibleBy
888
+
else None
889
+
890
+
let fizzBuzz = function
891
+
| DivisibleBy 3 & DivisibleBy 5 -> "FizzBuzz"
892
+
| DivisibleBy 3 -> "Fizz"
893
+
| DivisibleBy 5 -> "Buzz"
894
+
| i -> string i
895
+
```
896
+
897
+
- `bool`
898
+
899
+
```fsharp
900
+
let (|DivisibleBy|_|) by n = n % by = 0
901
+
```
902
+
903
+
```fsharp
904
+
let (|EqualsIgnoreCase|_|) (pattern: string) (value: string) =
0 commit comments