Skip to content

Instantly share code, notes, and snippets.

@EduardoRFS
Last active November 22, 2021 18:05
Show Gist options
  • Save EduardoRFS/84bd13f5f2ea4025113ebd810bedfd07 to your computer and use it in GitHub Desktop.
Save EduardoRFS/84bd13f5f2ea4025113ebd810bedfd07 to your computer and use it in GitHub Desktop.
type exists<V> = <Y>(p: (v: V) => Y) => Y;
// module interface
type Unique_S<T> = {
next: () => T,
equal: (a: T, b: T) => boolean,
compare: (a: T, b: T) => number,
show: (t: T) => string,
};
type Unique<T> = exists<Unique_S<T>>;
// module
const Unique = (() => {
type T = number;
let acc = -1;
const next = () => {
acc++;
return acc;
};
const equal = (a: T, b: T) => a == b;
const compare = (a: T, b: T) => {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
};
const show = (a: T) => a.toString();
const Module = { next, equal, compare, show };
return <Y>(p: (v: Unique_S<unknown>) => Y) => p(Module as Unique_S<unknown>);
})();
// ... Another_module
const Another_module = <Unique_T>({ Unique }: { Unique: Unique_S<Unique_T> }) => {
const first = Unique.next();
console.log(Unique.show(first));
const second = Unique.next();
console.log(Unique.show(second));
console.log(Unique.equal(first, first));
console.log(Unique.equal(first, second));
console.log(Unique.compare(first, first));
console.log(Unique.compare(first, second));
console.log(Unique.compare(second, first));
};
// load modules
Unique(Unique =>
Another_module({ Unique })
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment