Skip to content

Instantly share code, notes, and snippets.

View dimitris-papadimitriou-chr's full-sized avatar
📖
Cloud Design Patterns

Dimitris Papadimitriou dimitris-papadimitriou-chr

📖
Cloud Design Patterns
View GitHub Profile
public abstract class Either<TLeft, TRight>
{
public abstract void IfLeft(Action<TLeft> action);
public abstract void IfRight(Action<TRight> action);
public abstract Either<TLeft, T1Right> Select<T1Right>(Func<TRight, T1Right> mapping);
public abstract TResult Match<TResult>(Func<TLeft, TResult> Left, Func<TRight, TResult> Right);
}
@dimitris-papadimitriou-chr
dimitris-papadimitriou-chr / .cs
Created June 25, 2019 19:30
Free Monad in C#
public interface IFunctor<T>
{
IFunctor<T1> Map<T1>(Func<T, T1> map);
}
public class Id<T> : IFunctor<T>
{
private T source;
public Id(T v) => source = v;
@dimitris-papadimitriou-chr
dimitris-papadimitriou-chr / .cs
Created June 25, 2019 19:32
Free Monad as Functor
public abstract class Free<T>
{
public abstract Free<T1> Map<T1>(Func<T, T1> map);
}
public class Pure<T> : Free<T>
{
private T source;
Func<int, ILazyComonad<int>> CofreeAna = null;
CofreeAna = n => new LazyCofree(n + 1, new Lazy(() => new IdentiyF(CofreeAna(n + 1))));
Func<int, ILazyComonad<int>> CofreeAna = null;
CofreeAna = n => new LazyCofree(n + 1, new Lazy(() => new IdentiyF(CofreeAna(n + 1))));
var stream = CofreeAna(0);
var t = stream.Next().Map(x => x * 2).Next().Next().Map(x => x * 2);
var node = (left, v, right) => ({
left: left,
v: v,
right: right,
map: f => node(left.map(f), f(v), right.map(f)),
});
var leaf = v => ({ v: v, map: f => leaf(f(v)), });
var tree = node(leaf(1),3, node(leaf(5), 7, leaf(13)));
Object.prototype.getIterator = function () {
var self = this;
return {
*[Symbol.iterator]() {
{
var content = self.fold([],(acc,e)=>acc.concat( e ) )
for (var element of content) yield element;
}
}
}
//https://medium.com/@dimpapadim3
var node = (left, value, right) => ({
left: left,
value: value,
right: right,
map: f => node(left.map(f), f(value), right.map(f)),
fold: (mempty, mappend) =>
fetch("https://api.github.com/users")
.map(response => response.json())
.toEither()
.map(users => users.map(u => u.login))
.cata({
ok: v => console.log(v),
error: v => console.log("error" + v)
});
var EitherAsync = function (actions, g) {
this.cata = function (alg) {
actions(alg.ok , alg.error)
}
}
Promise.prototype.toEither = function () {
var promise = this;
var either = new EitherCoyoAsync(function (resolve, reject) {
promise.then(resolve).catch(reject)