diff --git a/samples/simple/FSharpSample/FSharpSample.fsproj b/samples/simple/FSharpSample/FSharpSample.fsproj new file mode 100644 index 00000000..4c289ee3 --- /dev/null +++ b/samples/simple/FSharpSample/FSharpSample.fsproj @@ -0,0 +1,21 @@ + + + + Exe + net8.0 + + + + + + + + + + + + + + + + diff --git a/samples/simple/FSharpSample/Program.fs b/samples/simple/FSharpSample/Program.fs new file mode 100644 index 00000000..88eb6eef --- /dev/null +++ b/samples/simple/FSharpSample/Program.fs @@ -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() +|> quickDemo +|> dictDemo +|> kmeans +|> ignore \ No newline at end of file diff --git a/samples/simple/QuickConsoleTest/Program.cs b/samples/simple/QuickConsoleTest/Program.cs index 2e93f779..bf4c78c7 100644 --- a/samples/simple/QuickConsoleTest/Program.cs +++ b/samples/simple/QuickConsoleTest/Program.cs @@ -60,10 +60,8 @@ static void RunKmeansDemo(IPythonEnvironment env) (10, 2), (10, 4), (10, 0) ]; - var inertiaResult = kmeansExample.CalculateKmeansInertia(data, 4); - var centroids = JsonSerializer.Serialize(inertiaResult.Item1); - var inertia = inertiaResult.Item2; - Console.WriteLine($"KMeans inertia for 4 clusters is {centroids}, inertia is {inertia}"); + var (centroids, inertia)= kmeansExample.CalculateKmeansInertia(data, 4); + Console.WriteLine($"KMeans inertia for 4 clusters is {JsonSerializer.Serialize(centroids)}, inertia is {inertia}"); } static void RunAIDemo(IPythonEnvironment env) diff --git a/samples/simple/SimpleSamples.sln b/samples/simple/SimpleSamples.sln index 1390983e..58f204a8 100644 --- a/samples/simple/SimpleSamples.sln +++ b/samples/simple/SimpleSamples.sln @@ -14,6 +14,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApp", "WebApp\WebApp.csproj", "{13504634-1D1A-4BB7-99D4-533D738330CC}" EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FSharpSample", "FSharpSample\FSharpSample.fsproj", "{C5A7FCEB-57D9-4873-A2B5-4F246C37E020}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -32,6 +34,10 @@ Global {13504634-1D1A-4BB7-99D4-533D738330CC}.Debug|Any CPU.Build.0 = Debug|Any CPU {13504634-1D1A-4BB7-99D4-533D738330CC}.Release|Any CPU.ActiveCfg = Release|Any CPU {13504634-1D1A-4BB7-99D4-533D738330CC}.Release|Any CPU.Build.0 = Release|Any CPU + {C5A7FCEB-57D9-4873-A2B5-4F246C37E020}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C5A7FCEB-57D9-4873-A2B5-4F246C37E020}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C5A7FCEB-57D9-4873-A2B5-4F246C37E020}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C5A7FCEB-57D9-4873-A2B5-4F246C37E020}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE