Skip to content

chore: improve object equality, fix eval and tag #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/weak-melons-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tsplus/stdlib": patch
---

Improve Equals on Object, fix Tag/Eval equality
30 changes: 27 additions & 3 deletions packages/stdlib/_src/io/Eval/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,33 +49,57 @@ export function unifyEval<X extends Eval<any>>(
}

export interface Succeed<A> extends Eval<A> {}
export class Succeed<A> {
export class Succeed<A> implements Equals {
readonly _tag = "Succeed"

readonly [EvalSym]: EvalSym = EvalSym
readonly [_A]!: () => A

constructor(readonly a: Lazy<A>) {}

[Equals.sym](that: unknown) {
return this === that
}

[Hash.sym]() {
return Hash.randomCached(this)
}
}

export interface Suspend<A> extends Eval<A> {}
export class Suspend<A> {
export class Suspend<A> implements Equals {
readonly _tag = "Suspend"

readonly [EvalSym]: EvalSym = EvalSym
readonly [_A]!: () => A

constructor(readonly f: Lazy<EvalInternal<A>>) {}

[Equals.sym](that: unknown) {
return this === that
}

[Hash.sym]() {
return Hash.randomCached(this)
}
}

export interface FlatMap<A, B> extends Eval<B> {}
export class FlatMap<A, B> {
export class FlatMap<A, B> implements Equals {
readonly _tag = "FlatMap"

readonly [EvalSym]: EvalSym = EvalSym
readonly [_A]!: () => A

constructor(readonly value: EvalInternal<A>, readonly cont: (a: A) => EvalInternal<B>) {}

[Equals.sym](that: unknown) {
return this === that
}

[Hash.sym]() {
return Hash.randomCached(this)
}
}

export interface EvalF extends HKT {
Expand Down
8 changes: 7 additions & 1 deletion packages/stdlib/_src/service/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export const Tag: TagOps = Object.assign(
[Tag.sym]: identity,
toEnv(value) {
return Env(this, value)
},
[Equals.sym](that) {
return this === that
},
[Hash.sym]() {
return Hash.randomCached(this)
}
}),
{
Expand All @@ -25,7 +31,7 @@ export const Tag: TagOps = Object.assign(
/**
* @tsplus type Tag
*/
export interface Tag<in out S> {
export interface Tag<in out S> extends Equals {
readonly [Tag.sym]: (_: S) => S

toEnv(value: S): Service.Env<S>
Expand Down
9 changes: 9 additions & 0 deletions packages/stdlib/_src/structure/Equals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ const protoMap = new Map<any, (a: any, b: any) => boolean>([
[
Object.prototype,
(a: object, b: object) => {
if ("_tag" in a) {
if ("_tag" in b) {
if (a["_tag"] !== b["_tag"]) {
return false
}
} else {
return false
}
}
const keysA = Object.keys(a).sort()
const keysB = Object.keys(b).sort()
if (keysA.length !== keysB.length) {
Expand Down