Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions _snippets/computation_expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
code: |
// Define a custom computation expression for validation
type ValidationBuilder() =
member _.Bind(x, f) =
member _.Bind(x, f) = // Defines "let!"
match x with
| Ok value -> f value
| Error e -> Error e
member _.Return(x) = Ok x
member _.ReturnFrom(x) = x

member _.Return(x) = Ok x // Defines "return"
member _.ReturnFrom(x) = x // Defines "return!"
let validate = ValidationBuilder()

type Person = { Name: string; Age: int }
Expand All @@ -22,19 +21,22 @@
if age >= 0 && age < 150 then Ok age
else Error "Age must be between 0 and 150"

let! validName =
let! nonEmptyName =
if String.length name > 0 then Ok name
else Error "Name cannot be empty"

return { Name = validName; Age = validAge }
if String.length name > 100 then
return! Error "Name is too long!"
else
return { Name = nonEmptyName; Age = validAge }
}
---
## Clean Code with Computation Expressions

F# computation expressions give you an elegant syntax for compositional control flows with a clean, readable notation that some say is F#'s superpower.

Check failure on line 36 in _snippets/computation_expressions.md

View workflow job for this annotation

GitHub Actions / lint

Line length

_snippets/computation_expressions.md:36:101 MD013/line-length Line length [Expected: 100; Actual: 151] https://github.com/DavidAnson/markdownlint/blob/v0.37.4/doc/md013.md
<!--more-->
- **Computation expressions** factor out how code is composed together
- **Custom control flow abstractions** create domain-specific mini-languages
- **Seamless error handling** with [railway-oriented programming](https://fsharpforfunandprofit.com/rop/) patterns

Check failure on line 40 in _snippets/computation_expressions.md

View workflow job for this annotation

GitHub Actions / lint

Line length

_snippets/computation_expressions.md:40:101 MD013/line-length Line length [Expected: 100; Actual: 114] https://github.com/DavidAnson/markdownlint/blob/v0.37.4/doc/md013.md
- **Elegant data transformations** by hiding boilerplate and focusing on business logic
- **Composable workflows** that can be combined and nested for complex operations
15 changes: 7 additions & 8 deletions _snippets/sequence_expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
title: SequenceExpressions.fs
excerpt_separator: <!--more-->
code: |
// A function generating a sequence of numbers
let rec fizzBuzzSeq n = seq {
yield
match n with
| x when x % 15 = 0 -> "fizzbuzz"
| x when x % 3 = 0 -> "fizz"
| x when x % 5 = 0 -> "buzz"
| _ -> n.ToString()

match n with
| x when x % 15 = 0 -> "fizzbuzz"
| x when x % 3 = 0 -> "fizz"
| x when x % 5 = 0 -> "buzz"
| _ -> n.ToString()

// Tail recursion makes this as efficient as a "while" loop
yield! fizzBuzzSeq (n + 1)
}

Expand All @@ -22,7 +21,7 @@
---
## Data Pipelines with Sequence Expressions

F# sequence expressions provide compositional, functional stream processing capabilities that integrate seamlessly with every part of the language.

Check failure on line 24 in _snippets/sequence_expressions.md

View workflow job for this annotation

GitHub Actions / lint

Line length

_snippets/sequence_expressions.md:24:101 MD013/line-length Line length [Expected: 100; Actual: 147] https://github.com/DavidAnson/markdownlint/blob/v0.37.4/doc/md013.md
<!--more-->
- **Simplified data generation** through sequence expressions
- **Compositional data processing** through library routines
Expand Down
Loading