This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Free<T> | |
{ | |
public abstract Free<T1> Map<T1>(Func<T, T1> map); | |
} | |
public class Pure<T> : Free<T> | |
{ | |
private T source; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Func<int, ILazyComonad<int>> CofreeAna = null; | |
CofreeAna = n => new LazyCofree(n + 1, new Lazy(() => new IdentiyF(CofreeAna(n + 1)))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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) => |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer