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";
@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)
[System.Serializable]
public sealed class Pure<T> : FreeIO<T>, System.IEquatable<Pure<T>>, System.IComparable<Pure<T>>, System.IComparable
{
public readonly T Value;
public Pure(T Value)
{
this.Value = Value;
}
public static Pure<T> New(T Value) => new Pure<T>(Value);