Skip to content

Instantly share code, notes, and snippets.

@EduardoRFS
Created May 18, 2024 21:28
Show Gist options
  • Save EduardoRFS/13120dde8d46215d63dee6b932dfd753 to your computer and use it in GitHub Desktop.
Save EduardoRFS/13120dde8d46215d63dee6b932dfd753 to your computer and use it in GitHub Desktop.
type User = {
id: number;
name: string;
};
type Data = {
user: User;
date: Date;
instance: number;
};
const get_user_name = (ctx: Data) => {
console.log(ctx.user.name);
};
const set_user_name = (ctx: Data): [string, Data] => {
const original_user = ctx.user;
const user = { ...original_user, name: "Sp34d" };
return [original_user.name, { ...ctx, user }];
};
type Context<A> = (ctx: Data) => [A, Data];
// const pure =
// <A>(x: A): Context<A> =>
// (ctx) =>
// [x, ctx];
// const bind =
// <A, B>(m: Context<A>, f: (x: A) => Context<B>): Context<B> =>
// (ctx) => {
// const [value, ctx2] = m(ctx);
// return f(value)(ctx2);
// };
// const get_user = () => null as any as Context<User>;
// const update_user = (user: User) => null as any as Context<void>;
// const set_user_name_monadic = (): Context<string> =>
// get_user().bind((original_user) =>
// update_user({ ...original_user, name: "Sp34d" }).bind(() =>
// pure(original_user.name)
// )
// );
// type Result<T, E> = "a";
type World = {
_tag: "world";
};
type Result<T, E> = { tag: "error"; error: E } | { tag: "ok"; value: T };
type IO<A> = (world: World) => Promise<Result<[A, World], string>>;
// const pure =
// <A>(x: A): IO<A> =>
// (world) =>
// Promise.resolve({ tag: "ok", value: [x, world] });
const bind =
<A, B>(m: IO<A>, f: (x: A) => IO<B>): IO<B> =>
async (world) => {
try {
const result = await m(world);
if (result.tag == "error") {
return result;
}
const [value, world2] = result.value;
return f(value)(world2);
} catch (e) {
return Promise.resolve({ tag: "error", error: e.toString() });
}
};
const log: (msg: string) => IO<void> = null as any;
const read: (file: string) => IO<string> = null as any;
const write: (file: string, data: string) => IO<void> = null as any;
const parse_data = (data: string): Result<number, string> => {
const value = parseInt(data);
if (isNaN(value)) {
return { tag: "error", error: "not a number" };
}
return { tag: "ok", value };
};
const read_data = (file: string): IO<Result<number, string>> =>
bind(read("tuturu.txt"), (data) => {
return pure(parse_data(data));
});
const main = function* () {
const data = yield read("tuturu.txt");
const x = parse_data(data);
yield log(data);
yield write("tuturu.txt", "TUTURU");
};
let acc = 0;
const next_mut = () => {
const current = acc;
acc++;
return current;
};
const next_naive = (acc: number) => {
const current = acc;
acc = 1 + acc;
return [current, acc];
};
type State<T> = {
bind<U>(f: (x: T) => State<U>): State<U>;
};
const pure: <A>(x : A) => State<A> = null as any;
const get_acc: () => State<number> = null as any;
const set_acc: (acc: number) => State<void> = null as any;
const fetch_and_add: (add: number) => State<number> = null as any;
const next_dot = () =>
get_acc().bind(current =>
set_acc(1 + current).bind(() =>
pure(current)));
const next = function* () {
const current = yield fetch_and_add(4);
return current;
};
console.log("a");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment