Skip to content

Include a snippet on the power of .NET #1005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
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
File renamed without changes.
1 change: 0 additions & 1 deletion _snippets/oop.md → _snippets/10 oop.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@
---
## Objects Made Simple

F# is **functional first** and **immutable by default**, but it also provides pragmatic support for object programming.

Check failure on line 43 in _snippets/10 oop.md

View workflow job for this annotation

GitHub Actions / lint

Line length

_snippets/10 oop.md:43:101 MD013/line-length Line length [Expected: 100; Actual: 119] https://github.com/DavidAnson/markdownlint/blob/v0.37.4/doc/md013.md
<!--more-->
- **Seamless .NET integration** lets you work with existing .NET libraries and frameworks
- **Rich interface system** allows you to define clear contracts for your components
- **Object expressions** provide lightweight implementation of interfaces without defining full classes
- **Concise member syntax** keeps methods and properties clean and readable
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
---
order: 15
order: 12
title: SequenceExpressions.fs
excerpt_separator: <!--more-->
code: |
let rec fizzBuzzSeq n = seq {
match n with
| x when x % 15 = 0 -> "fizzbuzz"
| x when x % 3 = 0 -> "fizz"
| x when x % 5 = 0 -> "buzz"
| _ -> n.ToString()
if n % 15 = 0 then "fizzbuzz"
elif n % 3 = 0 then "fizz"
elif n % 5 = 0 then "buzz"
else n.ToString()

// Tail recursion makes this as efficient as a "while" loop
yield! fizzBuzzSeq (n + 1)
Expand All @@ -21,7 +20,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 23 in _snippets/12 sequence expressions.md

View workflow job for this annotation

GitHub Actions / lint

Line length

_snippets/12 sequence expressions.md:23: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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 16
order: 13
title: AsyncExpressions.fs
excerpt_separator: <!--more-->
code: |
Expand All @@ -20,12 +20,12 @@
return $"Validation error: {msg}"
}

processPersonAsync { Name = "Snowdrop"; Age = 13}
processPersonAsync { Name = "Snowdrop"; Age = 13 }
|> Async.RunSynchronously
---
## Async Programming made Easy

F# async expressions provide a powerful way to handle asynchronous programming, making it more readable and maintainable. They allow you to write non-blocking code that looks like synchronous code, which is particularly useful for I/O-bound operations.

Check failure on line 28 in _snippets/13 async expressions.md

View workflow job for this annotation

GitHub Actions / lint

Line length

_snippets/13 async expressions.md:28:101 MD013/line-length Line length [Expected: 100; Actual: 252] https://github.com/DavidAnson/markdownlint/blob/v0.37.4/doc/md013.md
<!--more-->
- **Async expressions** provide a clean syntax for defining asynchronous workflows
- **Integration with existing libraries** makes it easy to use async expressions with other F# features
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 17
order: 14
title: ComputationExpressions.fs
excerpt_separator: <!--more-->
code: |
Expand Down Expand Up @@ -33,10 +33,10 @@
---
## 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/14 computation expressions.md

View workflow job for this annotation

GitHub Actions / lint

Line length

_snippets/14 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/14 computation expressions.md

View workflow job for this annotation

GitHub Actions / lint

Line length

_snippets/14 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 19
order: 15
title: UnitsOfMeasure.fs
excerpt_separator: <!--more-->
code: |
Expand All @@ -18,9 +18,9 @@
---
## Safe Numbers through Units of Measure

F# offers world-class compile-time unit safety without runtime overhead, giving you the power to express your domain in a type-safe way. This is particularly useful in scientific, engineering and financial applications where unit errors can lead to catastrophic results.

Check failure on line 21 in _snippets/15 units of measure.md

View workflow job for this annotation

GitHub Actions / lint

Line length

_snippets/15 units of measure.md:21:101 MD013/line-length Line length [Expected: 100; Actual: 270] https://github.com/DavidAnson/markdownlint/blob/v0.37.4/doc/md013.md
<!--more-->
- **Compile-time dimensional safety** catches errors before running, preventing scientific and engineering mistakes

Check failure on line 23 in _snippets/15 units of measure.md

View workflow job for this annotation

GitHub Actions / lint

Line length

_snippets/15 units of measure.md:23:101 MD013/line-length Line length [Expected: 100; Actual: 115] https://github.com/DavidAnson/markdownlint/blob/v0.37.4/doc/md013.md
- **Domain-specific units** express quantities that directly reflect your problem space
- **Automatic unit conversions** maintain type safety while handling complex calculations
- **Seamless interoperability** works with normal numeric types when needed
Expand Down
6 changes: 3 additions & 3 deletions _snippets/typeproviders.md → _snippets/16 type providers.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
order: 14
order: 16
title: TypeProviders.fs
excerpt_separator: <!--more-->
code: |
open FSharp.Data
open FSharp.Data // dotnet package add FSharp.Data

type PeopleDB = CsvProvider<"people.csv">

Expand All @@ -12,7 +12,7 @@ code: |

for person in people.Rows do
// Access the CSV fields with intellisense and type safety!
printfn $"Name: %s{person.Name}, Id: %i{person.Id}"
printfn $"Name: {person.Name: string}, Id: {person.Id: int}"
---
## Type-Safe, Integrated Data

Expand Down
45 changes: 45 additions & 0 deletions _snippets/21 ecosystems.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
order: 21
title: Ecosystems.fs
excerpt_separator: <!--more-->
code: |
// JavaScript Example: Image processing
open Fable.Core
open Fable.Core.JS
// Read documentation, then define the JavaScript type
type [<Import("Image", "image-js")>] Image = // npm install image-js
static member load(path: string): Promise<Image> = jsNative
member _.flipX(): Image = jsNative
member _.flipY(): Image = jsNative
[<ParamObject>] member _.resize(?width: float, ?height: float): Image = jsNative
member _.save(path: string): Promise<unit> = jsNative
promise {
let! image = Image.load "input.png"
let image = image.resize(width=300, height=200).flipX().flipY()
do! image.save "output.jpg"
} |> _.catch(fun x -> console.error x) |> ignore

// .NET Example: Image processing
// C# types have seamless integration with F#.
open SixLabors.ImageSharp // dotnet package add SixLabors.ImageSharp
open SixLabors.ImageSharp.Processing
use image = Image.Load "input.png"
image.Mutate(_.Resize(300, 200).Flip(FlipMode.Horizontal ||| FlipMode.Vertical) >> ignore)
image.Save "output.jpg"

// Code sharing Example: The same code works across JavaScript and .NET
open System.Text.RegularExpressions // Specific System types are usable anywhere.
let input = "Emails: [email protected], [email protected], invalid-email"
let pattern = @"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b"
for matched in Regex.Matches(input, pattern) do
printfn $"Found email: {matched.Value}" // [email protected] and [email protected]
---
## Full access to ecosystems of libraries

F# has full integration with ecosystems of JavaScript and .NET libraries and frameworks.
Anything written in JavaScript or C# can be used from F# and vice versa.
<!--more-->
- **JavaScript** is the universal language of web development. It encompasses [Web](https://developer.mozilla.org/en-US/docs/Web/JavaScript), [Mobile](https://reactnative.dev/), [Desktop](https://www.electronjs.org/), [Cloud](https://nodejs.org/), [Microservices](https://nestjs.com/), [Artificial Intelligence](https://www.tensorflow.org/js), [Game Development](https://phaser.io/) and [Internet of Things](https://johnny-five.io/).

Check failure on line 42 in _snippets/21 ecosystems.md

View workflow job for this annotation

GitHub Actions / lint

Line length

_snippets/21 ecosystems.md:42:101 MD013/line-length Line length [Expected: 100; Actual: 433] https://github.com/DavidAnson/markdownlint/blob/v0.37.4/doc/md013.md
- **.NET** is Microsoft’s enterprise-grade platform for scalable applications. It also encompasses **[Web](https://dotnet.microsoft.com/en-us/apps/aspnet), [Mobile](https://dotnet.microsoft.com/en-us/apps/maui), [Desktop](https://dotnet.microsoft.com/en-us/apps/desktop), [Cloud](https://dotnet.microsoft.com/en-us/apps/cloud), [Microservices](https://dotnet.microsoft.com/en-us/apps/aspnet/microservices), [Artificial Intelligence](https://dotnet.microsoft.com/en-us/apps/ai), [Game Development](https://dotnet.microsoft.com/en-us/apps/games), and [Internet of Things](https://dotnet.microsoft.com/en-us/apps/iot)**.
- [**npm packages**](https://www.npmjs.com/package/image-js) or [**NuGet packages**](https://www.nuget.org) can be accessed from F#, reusing existing packages to suit your needs.
- **Incremental adoption** is possbile by mixing Javascript or C# with F#.
5 changes: 3 additions & 2 deletions _snippets/fable.md → _snippets/22 fable.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 12
order: 22
title: WebApps.fs
excerpt_separator: <!--more-->
code: |
Expand Down Expand Up @@ -32,7 +32,8 @@ code: |
---
## F# for JavaScript and the Full Stack

F# is for both client and server. With [F# web technologies]({{ '/use/web-apps/' | relative_url }}), you can target JavaScript environments directly. This means you can use F# to build web applications, mobile apps, and even serverless functions that run in the cloud.
F# is for both client and server. With [F# web technologies]({{ '/use/web-apps/' | relative_url }}), you can target JavaScript environments directly without including the rest of .NET.
This means you can use F# across both frontend and backend to build web applications, mobile apps, and even serverless functions that run in the cloud.
<!--more-->
- **Type-safe DOM manipulation** catches errors at compile time, not runtime
- **Seamless React integration** with hooks and modern patterns
Expand Down
46 changes: 46 additions & 0 deletions _snippets/ecosystem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
order: 1
title: DotNet.fs
excerpt_separator: <!--more-->
code: |
// JavaScript Example: Image processing
open Fable.Core
open Fable.Core.JS
// Read documentation, then define the JavaScript type
type [<Import("Image", "image-js")>] Image = // npm install image-js
static member load(path: string): Promise<Image> = jsNative
member _.flipX(): Image = jsNative
member _.flipY(): Image = jsNative
[<ParamObject>] member _.resize(?width: float, ?height: float): Image = jsNative
member _.save(path: string): Promise<unit> = jsNative
(promise {
let! image = Image.load "input.png"
let image = image.resize(width=300, height=200).flipX().flipY()
do! image.save "output.jpg"
}).catch(fun x -> console.error x) |> ignore

// .NET Example: Image processing
// C# types have seamless integration with F#.
open SixLabors.ImageSharp // dotnet package add SixLabors.ImageSharp
open SixLabors.ImageSharp.Processing
use image = Image.Load "input.png" // .NET types are integrated seamlessly.
image.Mutate(_.Resize(300, 200).Flip(FlipMode.Horizontal ||| FlipMode.Vertical) >> ignore)
image.Save "output.jpg"

// Code sharing Example: The same code works across JavaScript and .NET
open System.Text.RegularExpressions // Specific System types are usable anywhere.
let input = "Emails: [email protected], [email protected], invalid-email"
let pattern = @"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b"
for matched in Regex.Matches(input, pattern) do
printfn $"Found email: {matched.Value}" // [email protected] and [email protected]
---

## Full access to ecosystems of libraries

F# has full integration with ecosystems of JavaScript and Microsoft .NET libraries and frameworks.
Anything written in JavaScript or C# can be used from F# and vice versa.
<!--more-->
- **JavaScript** is the universal language of web development. It encompasses [Web](https://developer.mozilla.org/en-US/docs/Web/JavaScript), [Mobile](https://reactnative.dev/), [Desktop](https://www.electronjs.org/), [Cloud](https://nodejs.org/), [Microservices](https://nestjs.com/), [Artificial Intelligence](https://www.tensorflow.org/js), [Game Development](https://phaser.io/) and [Internet of Things](https://johnny-five.io/).
- **.NET** is Microsoft’s enterprise-grade platform for scalable applications. It also encompasses **[Web](https://dotnet.microsoft.com/en-us/apps/aspnet), [Mobile](https://dotnet.microsoft.com/en-us/apps/maui), [Desktop](https://dotnet.microsoft.com/en-us/apps/desktop), [Cloud](https://dotnet.microsoft.com/en-us/apps/cloud), [Microservices](https://dotnet.microsoft.com/en-us/apps/aspnet/microservices), [Artificial Intelligence](https://dotnet.microsoft.com/en-us/apps/ai), [Game Development](https://dotnet.microsoft.com/en-us/apps/games), and [Internet of Things](https://dotnet.microsoft.com/en-us/apps/iot)**.
- [**npm packages**](https://www.npmjs.com/package/image-js) or [**NuGet packages**](https://www.nuget.org) can be accessed from F#, reusing existing packages to suit your needs.
- **Incremental adoption** is possible by mixing Javascript or C# with F#.