Skip to content

Latest commit

 

History

History
45 lines (42 loc) · 3.13 KB

File metadata and controls

45 lines (42 loc) · 3.13 KB
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: 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 .NET libraries and frameworks. Anything written in JavaScript or C# can be used from F# and vice versa.