Skip to content

Commit f5a493b

Browse files
feat: bool active patterns section; polish (#43)
1 parent aa998d3 commit f5a493b

2 files changed

Lines changed: 33 additions & 17 deletions

File tree

docs/fsharp-cheatsheet.md

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,10 @@ The most common use is when you have a function that receives no parameters, but
216216
let getCurrentDateTime = DateTime.Now
217217
218218
// This version evalautes DateTime.Now every time you call it with a `unit` argument.
219-
let getCurrentDateTime2 () = DateTime.Now
219+
let getCurrentDateTime2 () = DateTime.Now
220220
221221
// How to call the function:
222-
let startTime = getCurrentDateTime2()
222+
let startTime = getCurrentDateTime2 ()
223223
```
224224

225225
<div id="functions-signatures"></div>
@@ -501,7 +501,7 @@ In C#, if a method has an `out` parameter (e.g. [`DateTime.TryParse`](https://le
501501
let (success, outParsedDateTime) = System.DateTime.TryParse("2001/02/06")
502502
```
503503

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.
505505

506506
<div id="data-types-records"></div>
507507

@@ -876,20 +876,36 @@ match "yennefer@aretuza.org" with // output: "Email: yennefer@aretuza.org"
876876
## Partial active patterns
877877

878878
*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>`.
880879

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.
886881

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) =
905+
String.Equals(value, pattern, StringComparison.OrdinalIgnoreCase)
906+
```
907+
908+
See [Active Patterns (MS Learn)](https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/active-patterns) to learn more.
893909
894910
<div id="asynchronous-programming"></div>
895911

docs/template.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<h1>F# Cheatsheet</h1>
1616

1717
<p>
18-
This cheatsheet aims to succinctly cover the most important aspects of F# 8.0.
18+
This cheatsheet aims to succinctly cover the most important aspects of F# 10.0.
1919
</p>
2020

2121
<p>
@@ -42,4 +42,4 @@ <h1>F# Cheatsheet</h1>
4242
{{{content}}}
4343
</main>
4444
</body>
45-
</html>
45+
</html>

0 commit comments

Comments
 (0)