|
| 1 | +--- |
| 2 | +order: 1 |
| 3 | +title: DotNet.fs |
| 4 | +excerpt_separator: <!--more--> |
| 5 | +code: | |
| 6 | + // JavaScript Example: Image processing |
| 7 | + open Fable.Core |
| 8 | + open Fable.Core.JS |
| 9 | + // Read documentation, then define the JavaScript type |
| 10 | + type [<Import("Image", "image-js")>] Image = // npm install image-js |
| 11 | + static member load(path: string): Promise<Image> = jsNative |
| 12 | + member _.flipX(): Image = jsNative |
| 13 | + member _.flipY(): Image = jsNative |
| 14 | + [<ParamObject>] member _.resize(?width: float, ?height: float): Image = jsNative |
| 15 | + member _.save(path: string): Promise<unit> = jsNative |
| 16 | + (promise { |
| 17 | + let! image = Image.load "input.png" |
| 18 | + let image = image.resize(width=300, height=200).flipX().flipY() |
| 19 | + do! image.save "output.jpg" |
| 20 | + }).catch(fun x -> console.error x) |> ignore |
| 21 | +
|
| 22 | + // .NET Example: Image processing |
| 23 | + // C# types have seamless integration with F#. |
| 24 | + open SixLabors.ImageSharp // dotnet package add SixLabors.ImageSharp |
| 25 | + open SixLabors.ImageSharp.Processing |
| 26 | + use image = Image.Load "input.png" // .NET types are integrated seamlessly. |
| 27 | + image.Mutate(_.Resize(300, 200).Flip(FlipMode.Horizontal ||| FlipMode.Vertical) >> ignore) |
| 28 | + image.Save "output.jpg" |
| 29 | +
|
| 30 | + // Code sharing Example: The same code works across JavaScript and .NET |
| 31 | + open System.Text.RegularExpressions // Specific System types are usable anywhere. |
| 32 | + let input = "Emails: [email protected], [email protected], invalid-email" |
| 33 | + let pattern = @"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b" |
| 34 | + for matched in Regex.Matches(input, pattern) do |
| 35 | + printfn $"Found email: {matched.Value}" // [email protected] and [email protected] |
| 36 | + --- |
| 37 | +
|
| 38 | +## Full access to ecosystems of libraries |
| 39 | + |
| 40 | +F# has full integration with ecosystems of JavaScript and Microsoft .NET libraries and frameworks. |
| 41 | +Anything written in JavaScript or C# can be used from F# and vice versa. |
| 42 | +<!--more--> |
| 43 | +- **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/). |
| 44 | +- **.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)**. |
| 45 | +- [**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. |
| 46 | +- **Incremental adoption** is possible by mixing Javascript or C# with F#. |
0 commit comments