Skip to content

Instantly share code, notes, and snippets.

@ruxo
Last active April 8, 2024 13:23
Show Gist options
  • Save ruxo/7ac926bab7e76c9d971e8edcaa6bb447 to your computer and use it in GitHub Desktop.
Save ruxo/7ac926bab7e76c9d971e8edcaa6bb447 to your computer and use it in GitHub Desktop.
Show how to use LanguageExt's Eff(ect) with memo to cache value.Note that `LanguageExt.Core` library is needed for running.
using System;
using LanguageExt;
using static LanguageExt.Prelude;
public static class Program {
public static void Main (string[] args) {
var program =
from v1 in Load
from _1 in eff(() => Console.WriteLine($"1st value = {v1}"))
from v2 in Load
from _2 in eff(() => Console.WriteLine($"2st value = {v2}"))
select unit;
program.RunUnit();
}
static int LoadSomething(){
Console.WriteLine("Load!");
return 123;
}
// try remove memo for comparision
static Eff<int> Load = Eff(memo(LoadSomething));
static Eff<Unit> eff(Action a) =>
Eff(() => {
a();
return unit;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment