Skip to content

Instantly share code, notes, and snippets.

View louthy's full-sized avatar
Focusing

Paul Louth louthy

Focusing
View GitHub Profile
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";
[System.Serializable]
public sealed class Just<A> : Maybe<A>, System.IEquatable<Just<A>>, System.IComparable<Just<A>>, System.IComparable
{
public readonly A Value;
public Just(A Value)
{
this.Value = Value;
}
public void Deconstruct(out A Value)
@louthy
louthy / recon.ml
Created November 6, 2018 02:02 — forked from kseo/recon.ml
A Hindley-Milner type inference implementation in OCaml
#! /usr/bin/env ocamlscript
Ocaml.ocamlflags := ["-thread"];
Ocaml.packs := [ "core" ]
--
open Core.Std
type term =
| Ident of string
| Lambda of string * term
| Apply of term * term
@louthy
louthy / CSharp Free Monad.cs
Last active April 2, 2022 16:20
C# Free Monad
//
// See https://github.com/louthy/language-ext
//
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using LanguageExt;
using static LanguageExt.Prelude;
@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)
{
[System.Serializable]
public partial struct Person : System.IEquatable<Person>, System.IComparable<Person>, System.IComparable
{
public Person(string Forename, string Surname)
{
this.Forename = Forename;
this.Surname = Surname;
}
public static Person New(string Forename, string Surname) => new Person(Forename, Surname);
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
{