Skip to content

Instantly share code, notes, and snippets.

View louthy's full-sized avatar
Focusing

Paul Louth louthy

Focusing
View GitHub Profile
@louthy
louthy / StructFun.cs
Last active May 30, 2024 13:30
Delegates as structs in C#
var freeVar = 100;
Func<string, int> length = x => x.Length + freeVar;
var f = new Fun<string, int>(length);
var r = f.Invoke("Hello");
Console.WriteLine(r); // 105
public readonly unsafe struct Fun<A>
{
readonly object? self;
using System.Numerics;
using LanguageExt;
using LanguageExt.Common;
using LanguageExt.Traits;
using static LanguageExt.Prelude;
namespace ValidationExamples;
// Credit card number
@louthy
louthy / Discussions.1293.fs
Created February 6, 2024 21:20
How to use Validation applicative from F#
module Validation =
open LanguageExt
let successValue (ma : Validation<'f, 'a>) : 'a =
ma.IfFail(fun () -> failwith "Validation monad is in a Fail state")
let apply (mf : Validation<'f, 'a -> 'b>) (ma : Validation<'f, 'a>) : Validation<'f, 'b> =
mf.Disjunction(ma)
.Map(fun _ -> (successValue mf) (successValue ma))
@louthy
louthy / null-monad.cs
Last active December 13, 2023 15:58
Turn nullable references into an option-monad
string? name = "Paul";
string? noName = null;
// Only says hello if not null
var helloName = name.Map(n => $"Hello, {name}"); // "Hello, Paul"
var noHelloName = noName.Map(n => $"Hello, {name}"); // null
// Nullable strings ...
string? sx = "Hello";
string? sy = "World";
@louthy
louthy / AdHoc.cs
Created April 12, 2021 14:26
Ad hoc polymorphism in C#
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var xs = new[] {"one", " ", "two", " ", "three"};
var ys = new[] {1, 2, 3};
using System;
using LanguageExt;
using static LanguageExt.Prelude;
namespace RiderCandidates
{
class Program
{
static void Main(string[] args)
{
using System;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using LanguageExt.Common;
using LanguageExt.DataTypes.Serialisation;
using static LanguageExt.Prelude;
namespace LanguageExt
{
using Echo;
using LanguageExt;
using static LanguageExt.Prelude;
using static Echo.Process;
using System;
namespace StackOverflow2
{
class Program
{
using System;
using LanguageExt;
using static LanguageExt.Prelude;
namespace StackOverflow1
{
class Program
{
static void Main(string[] args)
{
using System;
using LanguageExt;
using LanguageExt.TypeClasses;
using System.Diagnostics.Contracts;
namespace Defunctionalisation
{
class Program
{
static void Main(string[] args)