order: 1
title: DotNet.fs
excerpt_separator:
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 = jsNative
member _.flipX(): Image = jsNative
member _.flipY(): Image = jsNative
[] member _.resize(?width: float, ?height: float): Image = jsNative
member _.save(path: string): Promise = 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: user1@test.com, user2@domain.org, 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}" // user1@test.com and user2@domain.org
---
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.
- JavaScript is the universal language of web development. It encompasses Web, Mobile, Desktop, Cloud, Microservices, Artificial Intelligence, Game Development and Internet of Things.
- .NET is Microsoft’s enterprise-grade platform for scalable applications. It also encompasses Web, Mobile, Desktop, Cloud, Microservices, Artificial Intelligence, Game Development, and Internet of Things.
- npm packages or NuGet packages can be accessed from F#, reusing existing packages to suit your needs.
- Synergy is achieved when F# is used for web development both on the front end (JavaScript) and back end (.NET), sharing essential business logic.
- Incremental adoption is possible by mixing Javascript or C# with F#.