Skip to content

Commit

Permalink
Merge pull request #207 from tonybaloney/fsharp
Browse files Browse the repository at this point in the history
Adding F# sample
  • Loading branch information
tonybaloney authored Sep 24, 2024
2 parents 2b4ba5d + 2bc780b commit e7f270f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
21 changes: 21 additions & 0 deletions samples/simple/FSharpSample/FSharpSample.fsproj
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>
58 changes: 58 additions & 0 deletions samples/simple/FSharpSample/Program.fs
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
6 changes: 2 additions & 4 deletions samples/simple/QuickConsoleTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions samples/simple/SimpleSamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e7f270f

Please sign in to comment.