-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #207 from tonybaloney/fsharp
Adding F# sample
- Loading branch information
Showing
4 changed files
with
87 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="Program.fs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CSnakes.Runtime" Version="1.*-*" /> | ||
<PackageReference Include="python" Version="3.12.4" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ExamplePythonDependency\ExamplePythonDependency.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
open CSnakes.Runtime | ||
open Microsoft.Extensions.DependencyInjection | ||
open Microsoft.Extensions.Hosting | ||
open System | ||
open System.IO | ||
open System.Collections.Generic | ||
|
||
let quickDemo (env: IPythonEnvironment) = | ||
let qd = env.QuickDemo() | ||
qd.Scream("a", 99) |> printfn "Scream: %A" | ||
qd.ScreamNames(["a"; "b"; "c"], 3) |> printfn "ScreamNames: %A" | ||
|
||
env | ||
|
||
let dictDemo (env: IPythonEnvironment) = | ||
let dd = env.TypeDemos() | ||
let d = dd.ReturnDict() | ||
|
||
// not ideal as it would be better if we could use Map and not have to | ||
// work with the KVPair type | ||
d |> Seq.iter (fun (x) -> printfn "Key: %A, Value: %A" x.Key x.Value) | ||
|
||
env | ||
|
||
let kmeans (env: IPythonEnvironment) = | ||
let km = env.KmeansExample() | ||
|
||
// clunky here, as F# tuples are reference by default so we need to use the | ||
// struct keyword to make them value types (but only on the first, the rest | ||
// are inferred) | ||
let data = [struct (1L, 2L); (1L, 4L); (1L, 0L); (10L, 2L); (10L, 4L); (10L, 0L)] | ||
|
||
let struct (centroids, inertia) = km.CalculateKmeansInertia(data, 4) | ||
printfn "KMeans inertia for 4 clusters is %A, inertia is %A" centroids inertia | ||
|
||
env | ||
|
||
let builder = Host.CreateDefaultBuilder().ConfigureServices(fun services -> | ||
let home = Path.Join(Environment.CurrentDirectory, "..", "..", "..", "..", "ExamplePythonDependency") | ||
let venv = Path.Join(home, ".venv") | ||
|
||
services | ||
.WithPython() | ||
.WithHome(home) | ||
.WithVirtualEnvironment(venv) | ||
.FromNuGet("3.12.4") | ||
.FromMacOSInstallerLocator("3.12") | ||
.FromEnvironmentVariable("Python3_ROOT_DIR", "3.12") | ||
.WithPipInstaller() |> ignore | ||
) | ||
|
||
let app = builder.Build() | ||
|
||
app.Services.GetRequiredService<IPythonEnvironment>() | ||
|> quickDemo | ||
|> dictDemo | ||
|> kmeans | ||
|> ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters