Skip to content

Commit

Permalink
feat/time_directive - added #time directive. change for code clarity …
Browse files Browse the repository at this point in the history
…on partial active patterns
  • Loading branch information
⚙︎ Greg committed Jul 9, 2024
1 parent 17692d9 commit c5dddf0
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions docs/fsharp-cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,8 @@ Another way of implementing interfaces is to use *object expressions*.

### Single-case active patterns

*Single-case active patterns* can be thought of as a simple way to convert data to a new form.

// Basic
let (|EmailDomain|) email =
let match' = Regex.Match(email, "@(.*)$")
Expand All @@ -711,8 +713,6 @@ Another way of implementing interfaces is to use *object expressions*.
let (Default "random citizen" name) = None // name = "random citizen"
let (Default "random citizen" name) = Some "Steve" // name = "Steve"

*Single-case active patterns* can be thought of as a simple way to convert data to a new form.

### Complete active patterns

let (|Even|Odd|) i =
Expand All @@ -732,8 +732,13 @@ Another way of implementing interfaces is to use *object expressions*.

### Partial active patterns

*Partial active patterns* share the syntax of parameterized patterns, but their active recognizers accept only one argument.
The *Partial active pattern* should return an `Option<'T>`.

let (|DivisibleBy|_|) by n =
if n % by = 0 then Some DivisibleBy else None
if n % by = 0
then Some DivisibleBy
else None

let fizzBuzz = function
| DivisibleBy 3 & DivisibleBy 5 -> "FizzBuzz"
Expand Down Expand Up @@ -1012,10 +1017,28 @@ See [Namespaces (MS Learn)](https://learn.microsoft.com/en-us/dotnet/fsharp/lang

## Compiler Directives

### time

Available in the FSI, is `#time` for a quick view into real time, CPU time, and garbage collection information.

#time
System.Threading.Thread.Sleep (System.TimeSpan.FromSeconds 1)
#time

// Output:
// --> Timing now on
// Real: 00:00:01.001, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
// val it: unit = ()
// --> Timing now off

### load

Load another F# source file into FSI.

#load "../lib/StringParsing.fs"

### Reference

Reference a .NET assembly (`/` symbol is recommended for Mono compatibility).
Reference a .NET assembly:

Expand Down

0 comments on commit c5dddf0

Please sign in to comment.