Skip to content

Latest commit

 

History

History
47 lines (43 loc) · 3.33 KB

File metadata and controls

47 lines (43 loc) · 3.33 KB

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

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.