Created
November 22, 2021 18:05
-
-
Save EduardoRFS/6f141a9f6d65ab97618cffc0e396c9d9 to your computer and use it in GitHub Desktop.
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
type NatModule<Nat> = { | |
zero: Nat; | |
one: Nat; | |
add: (x: Nat, y: Nat) => Nat; | |
}; | |
const NatModule = <A>(cb: <Nat>(Nat: NatModule<Nat>) => A) => { | |
const zero = 0; | |
const one = 1; | |
const add = (x: number, y: number) => x + y; | |
return cb({ zero, one, add }); | |
}; | |
type UserModule<User extends { id: number, name: string }> = { | |
make: (id: number, name: string) => User; | |
name: (user: User) => string; | |
}; | |
const UserModule = <A>(cb: <User extends { id: number, name: string }>(User: UserModule<User>) => A) => { | |
const make = (id: number, name: string) => ({ id, name }); | |
const name = (user): string => user.name; | |
return cb({ make, name }) | |
}; | |
const MyModule = UserModule(User => { | |
const eduardo = User.make(1, "Eduardo"); | |
const x = User.name(eduardo); | |
const y = eduardo.name; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment