| 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 |
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.
- 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.
- Incremental adoption is possbile by mixing Javascript or C# with F#.