Skip to content

Instantly share code, notes, and snippets.

View Thorium's full-sized avatar

Tuomas Hietanen Thorium

View GitHub Profile
@Thorium
Thorium / cursor.sql
Last active October 2, 2025 16:57
A few useful Microsoft SQL Server T-SQL function samples
-- Procedure using a cursor to gather date-range data from another procedure that takes a date as parameter
CREATE PROCEDURE gatherdates
@StartDate DATE = '20210401', @EndDate DATE = '20210501'
AS
SET NOCOUNT ON;
DECLARE @reportDates as CURSOR;
DECLARE @selDate as DATE;
--DECLARE @lastDateValue as decimal(19,4);
@Thorium
Thorium / ollama.fsx
Created October 2, 2025 14:11
Using local LLMs from F#
// Using local LLMs from F#
// Download and install Ollama: https://ollama.com/
// Ollama is a free common host for multiple different LLM-models
// like Google Gemma, OpenApi's GPT-OSS, Deepseek, etc.
#r "nuget:OllamaSharp"
open OllamaSharp
// For better Collections.Generic.IAsyncEnumerable<_> support for F#:
#r "nuget:FSharp.Control.TaskSeq"
open FSharp.Control
@Thorium
Thorium / Client.fsx
Created September 29, 2025 10:45
ModelContextProtocol with FSharp (MCP with F#)
#r "nuget: ModelContextProtocol,0.4.0-preview.1"
open ModelContextProtocol.Client
open ModelContextProtocol.Protocol
open System
open System.Threading.Tasks
module McpClient =
let clientTransport =
StdioClientTransport(
StdioClientTransportOptions(
@Thorium
Thorium / weightedAvg.fs
Created September 15, 2025 08:54
Takes a list of (quantity/amount and price/rate) as decimals, and gives a weighted average
/// Takes a list of (quantity/amount and price/rate) as decimals, and gives a weighted average
let weightedAvg mylist =
let weightedValue = mylist |> List.sumBy(fun (isum,irate) -> isum*irate)
let weightedSum = mylist |> List.sumBy(fun (isum,irate) -> isum)
match weightedSum with
| 0m<GBP> -> 0m
| x -> weightedValue / x
@Thorium
Thorium / farmer-clockpart.fs
Created September 15, 2025 08:44
Creating Azure dashboard clock component with Farmer
// Farmer F# https://compositionalit.github.io/farmer/
// Example of shaping an anonymous-object-as-JSON structure
// for an Azure dashboard using strong typing and minimal code.
// #r "nuget: Farmer"
open Farmer
open Farmer.Builders
/// TimeZone
let timezoneInfo =

Guidance for .NET systems development

Here is some opinionated guidance maybe to save some part of your valuable lifetime. The following topics focus on: development, architecture and maintainability.

The primary advice after John Skeet: Just solve your issue in the simplest possible way. Limit the boundaries: what are the requirements? Don't try to solve meta-level problems of every software engineer ever. Draft the boundaries even if you're writing a general library: Users can compose multiple small libraries if necessary. Expect your requirement specifications, if any, could be better, that's life.

@Thorium
Thorium / textmagic.fs
Created July 2, 2024 09:46
Send text messages with F# (FSharp)
[<RequireQualifiedAccess>]
module TextMagic
//https://www.textmagic.com/docs/api/send-sms/
// #r "System.Text.Json"
// #r "FSharp.Data"
open System
open System.IO
open System.Net
open System.Text.Json
@Thorium
Thorium / ef-functional.cs
Last active July 1, 2024 09:40
DBContext independend LINQ, the functional way
/// Old post recovered from Feb 2013:
/// https://web.archive.org/web/20130510043355/social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bc0bd8f3-0e49-440c-b0bb-8d9adb593934
///
/// This shows how you can:
///
/// - Code from top-down, give high level picture first and then go to details
/// - Define LINQ outside EF-context and still convert it to SQL: Don't hammer the database!
/// - Have small independend classes (/parts)
/// - Have state-less code with no global/class-variables outside entities
/// - Easy to test: a) Integration test with EF-DbContext.
@Thorium
Thorium / AbstractProject.fs
Last active February 16, 2024 15:23
Use (or replace) existing Logary via Microsoft.Extensions.Logging.Abstractions. This can be used to abstract Logary behind Microsoft.Extensions.Logging so it's easier to remove from existing infra (or migrate to, whatever).
// This expects you have 2 projects, this first one has dependendency only to Microsoft.Extensions.Logging.Abstractions
// (and temporarily usage of Microsoft.Extensions.Logging.Console ).
module AbstractProject
let loggerFactory =
Microsoft.Extensions.Logging.LoggerFactory.Create(fun builder ->
let _ = Microsoft.Extensions.Logging.ConsoleLoggerExtensions.AddSimpleConsole builder
())
/// This is ILogger.
@Thorium
Thorium / readJson.fsx
Created November 16, 2023 23:12
Using Utf8JsonReader from System.Text.Json with FSharp
//High-performance JSON parsing with System.Text.Json and F#
// Example translated to F# from
// https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/use-utf8jsonreader
#r "nuget:System.Text.Json"
open System
open System.Text
open System.Text.Json
let options = JsonReaderOptions(