added files

This commit is contained in:
Nick Leeman 2023-04-12 23:03:51 +02:00
parent afe595f403
commit 28571e1891
2 changed files with 223 additions and 78 deletions

46
etc.ts Normal file
View File

@ -0,0 +1,46 @@
/*
type Fun<a,b> = {
(input:a) : b
then<c>(input2: Fun<b,c>) : Fun<a,c>;
}
const Functie = function<a,b>(lambda: ((_:a) => b)) : Fun<a,b> {
const fun = lambda as Fun<a,b>
fun.then = function<c>(this: Fun<a,b>, otherFunc: Fun<b,c>) : Fun<a,c> {
return Functie<a,c>(x => otherFunc(this(x)))
}
return fun
}
// fun as monad
// fun functor
type Fun_n<a> = Fun<number, a>
let map_Fun_n = <a,b>(f: Fun<a,b>, p: Fun_n<a>) : Fun_n<b> => p.then(f)
// fun monoid
let unit_Fun_n = <a>() => Fun<a, Fun_n<a>>(x => Functie(i => x))
let join_Fun_n = <a>() => Fun<Fun_n<Fun_n<a>>, Fun_n<a>> (nestedFun_N => Functie(numberInput => nestedFun_N(numberInput)(numberInput)))
let join_Fun_beter: Fun<Fun_n<Fun_n<number>>, Fun_n<number>> = Functie((nestedFun_N: Fun_n<Fun_n<number>>) => {
return Functie((numberInput: number) => {
let tmp1 = nestedFun_N(numberInput);
console.log(tmp1);
let tmp2 = tmp1(numberInput);
console.log(tmp2);
return tmp2;
});
});
let incrDounbleFun: Fun_n<Fun_n<number>> = Functie((numberInput: number) => {
return Functie((numberInput2: number) => {
return numberInput + numberInput2;
});
});

View File

@ -38,17 +38,6 @@ const Fun = <input, output>(inputLamba: (_: input) => output): FunType<input, ou
return func;
}
const Conditional = function<input, output>(_if: FunType<input, boolean>, _then: FunType<input, output>, _else: FunType<input, output>): FunType<input, output> {
return Fun((x) => {
if (_if(x)) {
return _then(x);
} else {
return _else(x);
}
})
}
//
// Some FunType lamdas
//
@ -58,27 +47,28 @@ const incr: FunType<number, number> = Fun((x: number) => x + 1);
const double: FunType<number, number> = Fun((x: number) => x * 2);
const decr: FunType<number, number> = Fun((x: number) => x - 1);
const convert: FunType<number, string> = Fun((x: number) => String(x));
const exclaim: FunType<string, string> = Fun((x: string) => x + "!");
const exclaim: FunType<string, string> = Fun((x: string) => {
let y = x + "!";
return y;
});
const question: FunType<string, string> = Fun((x: string) => {
let y = x + "?";
return y;
});
const saveSqrt = Fun((a: number) => a > 0 ? Math.sqrt(a) : Math.abs(Math.sqrt(a)));
let xxx = incr.next(convert) (4)
//
// Generic id functions
//
// Id functions do basically nothing but initialize a base where we can work from (initializing a pipeline for example)
// Eg. Id functions are the base of all other functions
// const id: <a>() => FunType<a, a> = () => Fun(x => x);
function id<a>(): FunType<a, a> {
return Fun(_ => _);
}
// Another way of writing this functions
const genericId = <a>(): FunType<a, a> => Fun(_ => _);
// Even one more way of writing this function
const genericId2 = function<a>(): FunType<a, a> {
return Fun(_ => _);
}
const id: <a>() => FunType<a, a> = () => Fun(x => x);
// function id<a>(): FunType<a, a> {
// return Fun(_ => _);
// }
// Example usage 1
let exampleId = id<number>().next(incr).next(incr)(0) // -> result would be 2
@ -286,7 +276,6 @@ let monoidTest3 = id<number>().next(incr.next(incr)).next(incr) (0);
let monoidTest4 = incr.next(id<number>()) (0);
let monoidTest5 = id<number>().next(incr) (0);
// Therefore: monoidTest4 == monoidTest5
// A monoido or triple is
@ -303,79 +292,189 @@ let monoidTest5 = id<number>().next(incr) (0);
// Monads (NOT Monoid / Monodial, etc) are Functors and Monoids combined!
//
//
// Pairs
//
type Pair<a, b> = {
fst: a,
snd: b
a: a,
b: b
};
const pairA = <a, b>(): FunType<Pair<a, b>, a> => Fun(c => c.a);
const pairB = <a, b>(): FunType<Pair<a, b>, b> => Fun(c => c.b);
const mapPair = <a, b, c, d>(f1: FunType<a, c>, f2: FunType<b, d>): FunType<Pair<a, b>, Pair<c, d>> => Fun(c => {
return {
a: f1(c.a),
b: f2(c.b),
}
})
//
// Options
//
type Option<a> = ({ kind: "empty" } | { kind: "full", content: a }) & {
then <b>(cont: (_: a) => Option<b>): Option<b>
};
const emptyOption = <a>(): Option<a> => ({
kind: "empty",
then: function<b>(this: Option<a>, cont: (_: a) => Option<b>) { return bindOption(this, Fun(cont)) }
});
const fullOption = <a>(input: a): Option<a> => ({
kind: "full",
content: input,
then: function<b>(this: Option<a>, cont: (_: a) => Option<b>) { return bindOption(this, Fun(cont)) }
});
class OptionFunctors {
static join = <a>(nestedOption: Option<Option<a>>): Option<a> => {
if (nestedOption.kind == "empty") return emptyOption<a>();
if (nestedOption.content.kind == "empty") return emptyOption<a>();
return fullOption<a>(nestedOption.content.content);
}
static funJoin = <a>(): FunType<Option<Option<a>>, Option<a>> => {
return Fun((nestedOption: Option<Option<a>>) => {
if (nestedOption.kind == "empty") return emptyOption<a>();
if (nestedOption.content.kind == "empty") return emptyOption<a>();
return fullOption<a>(nestedOption.content.content);
})
}
static unit = <a>(unstructuredValue: a) => {
return fullOption(unstructuredValue);
}
static zero = <a>(): Option<a> => {
return emptyOption<a>();
}
}
const myPair: Pair<number, string> = {
fst: 1,
snd: "Hello"
}
function pairFst<a, b>(): FunType<Pair<a, b>, a> {
return Fun((pair: Pair<a, b>) => {
return pair.fst;
})
}
const pairSnd = function<a, b>(): FunType<Pair<a, b>, b> {
return Fun((pair: Pair<a, b>) => {
return pair.snd;
})
}
const pairMap = function<a, b, a1, b1>(f1: FunType<a, a1>, f2: FunType<b, b1>): FunType<Pair<a, b>, Pair<a1, b1>> {
return Fun((pair: Pair<a, b>) => {
return {
fst: f1(pair.fst),
snd: f2(pair.snd)
const mapOption = <a, b>(f: FunType<a, b>): FunType<Option<a>, Option<b>> => {
return Fun((option: Option<a>) => {
if (option.kind == "empty") {
return emptyOption<b>();
}
return fullOption(f(option.content));
})
}
const bindOption = <a, b>(source: Option<a>, cont: FunType<a, Option<b>>): Option<b> => {
return mapOption(cont).next(OptionFunctors.funJoin()) (source);
}
const saveDivide = (x: Option<number>, y: Option<number>): Option<number> => {
return x.then((xValue) => {
return y.then((yValue) => {
if (yValue == 0) {
return emptyOption<number>();
}
return fullOption(xValue / yValue);
})
})
}
type WithNum<a> = Pair<a, number>;
// let exmp = saveDivide(fullOption(3), saveDivide(fullOption(3), fullOption(0)));
const withNumSingle: WithNum<string> = {
fst: "yeey",
snd: 3
//
// Either
//
type Either<a, b> = {
kind: "left",
value: a
} | {
kind: "right",
value: b
}
const withNumDouble: WithNum<WithNum<string>> = {
fst: {
fst: "Hoi!",
snd: 3,
},
snd: 1,
}
const eitherL = <a, b>(): FunType<a, Either<a, b>> => Fun((input: a) => ({ kind: "left", value: input }));
const eitherR = <a, b>(): FunType<b, Either<a, b>> => Fun((input: b) => ({ kind: "right", value: input }));
const mapWithNum = function<a, b>(f: FunType<a, b>): FunType<WithNum<a>, WithNum<b>> {
return pairMap(f, id<number>());
}
const joinWithNum = function<a>(): FunType<WithNum<WithNum<a>>, WithNum<a>> {
return Fun(num => {
return {
fst: num.fst.fst,
snd: num.snd + num.fst.snd
const mapEither = <a1, b1, a2, b2>(fL: FunType<a1, a2>, fR: FunType<b1, b2>): FunType<Either<a1, b1>, Either<a2, b2>> => {
return Fun((input: Either<a1, b1>) => {
if (input.kind === "left") {
return fL.next(eitherL<a2, b2>())(input.value);
}
return fR.next(eitherR<a2, b2>())(input.value);
})
}
const incrementWithNum = function<a>(): FunType<WithNum<a>, WithNum<a>> {
return Fun((num: WithNum<a>) => {
return {
fst: num.fst,
snd: num.snd + 1
type MyException = {
msg: string;
}
interface ServerConnection {
ip: string; //ip address
hello: string; //hello message
}
const servers: Array<ServerConnection> = [
{ ip: "11.11.11.11", hello: "Connected to EU server!" },
{ ip: "22.22.22.22", hello: "Connected to Asia Server" },
{ ip: "33.33.33.33", hello: "Connected to US Server" },
]
const connect = (): FunType<string, Either<ServerConnection, MyException>> => {
return Fun((ip: string) => {
const prob: number = Math.random();
if (prob < 0.8) {
return eitherL<ServerConnection, MyException>()({
ip: "11",
hello: "2323"
});
} else {
return eitherR<ServerConnection, MyException>()({
msg: "Could not connect to server"
});
}
});
}
//
// Process
//
type Process<s, a> = FunType<s, [s, a]>;
const mapProcess = <s, a, b>(f: FunType<a, b>): FunType<Process<s, a>, Process<s, b>> => {
return Fun((initialprocess) => {
return Fun((initialState) => {
const [newState, result] = initialprocess(initialState);
const mappedResult = f(result);
return [newState, mappedResult];
})
})
}
class ProcessFunctors {
static join = <S, T>(outerProcess: Process<S, Process<S, T>>): Process<S, T> => {
return Fun((outerState) => {
const [innerState, innerProcess] = outerProcess(outerState);
return innerProcess(innerState);
})
}
console.log(
joinWithNum().next(incrementWithNum()).next(incrementWithNum()) (withNumDouble)
);
// static funJoin = <a>(): FunType<Option<Option<a>>, Option<a>> => {
// return Fun((nestedOption: Option<Option<a>>) => {
// if (nestedOption.kind == "empty") return emptyOption<a>();
// if (nestedOption.content.kind == "empty") return emptyOption<a>();
// return fullOption<a>(nestedOption.content.content);
// })
// }
const test123 = "sdsdds";
static unit = <S, T>(unstructuredValue: T): Process<S, T> => {
return Fun((state) => {
return [state, unstructuredValue];
})
}
}
type ProcessWithError<s, a> = FunType<s, Option<[s, a]>>;