Skip to content

Commit 19d69b5

Browse files
committed
New Feature: generate setoid instance, closes #12
1 parent 4901787 commit 19d69b5

30 files changed

+1081
-790
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
**Note**: Gaps between patch versions are faulty/broken releases.
1414
**Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice.
1515

16+
# 0.2.2
17+
18+
- **New Feature**
19+
- generate setoid instance, closes #12 (@gcanti)
20+
1621
# 0.2.1
1722

1823
- **New Feature**

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,27 @@ export function _none<A>(): Prism<Option<A>, Option<A>> {
8383
export function _some<A>(): Prism<Option<A>, Option<A>> {
8484
return Prism.fromPredicate(s => s.type === 'Some')
8585
}
86+
87+
/** Setoid instance */
88+
89+
import { Setoid } from 'fp-ts/lib/Setoid'
90+
91+
export function getSetoid<A>(setoidSomeValue0: Setoid<A>): Setoid<Option<A>> {
92+
return {
93+
equals: (x, y) => {
94+
if (x === y) {
95+
return true
96+
}
97+
if (x.type === 'None' && y.type === 'None') {
98+
return true
99+
}
100+
if (x.type === 'Some' && y.type === 'Some') {
101+
return setoidSomeValue0.equals(x.value0, y.value0)
102+
}
103+
return false
104+
}
105+
}
106+
}
86107
```
87108

88109
# Records
@@ -251,14 +272,16 @@ export interface Options {
251272
*/
252273
handlersStyle: { type: 'positional' } | { type: 'record'; handlersName: string }
253274
encoding: 'literal' | 'fp-ts'
275+
version: '1.13' | '1.14'
254276
}
255277
256278
export const defaultOptions: Options = {
257279
tagName: 'type',
258280
foldName: 'fold',
259281
matcheeName: 'fa',
260282
handlersStyle: { type: 'positional' },
261-
encoding: 'literal'
283+
encoding: 'literal',
284+
version: '1.13'
262285
}
263286
```
264287

docs/bundle.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/Constrained.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@ export function _fetching<A extends string>(): Prism<Constrained<A>, Constrained
2525

2626
export function _gotData<A extends string>(): Prism<Constrained<A>, Constrained<A>> { return Prism.fromPredicate(s => s.type === "GotData"); }
2727

28+
import { Setoid } from "fp-ts/lib/Setoid";
29+
30+
export function getSetoid<A extends string>(setoidGotDataValue0: Setoid<A>): Setoid<Constrained<A>> { return { equals: (x, y) => { if (x === y) {
31+
return true;
32+
} if (x.type === "Fetching" && y.type === "Fetching") {
33+
return true;
34+
} if (x.type === "GotData" && y.type === "GotData") {
35+
return setoidGotDataValue0.equals(x.value0, y.value0);
36+
} return false; } }; }
37+

examples/Either.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,13 @@ export function _left<L, R>(): Prism<Either<L, R>, Either<L, R>> { return Prism.
2121

2222
export function _right<L, R>(): Prism<Either<L, R>, Either<L, R>> { return Prism.fromPredicate(s => s.type === "Right"); }
2323

24+
import { Setoid } from "fp-ts/lib/Setoid";
25+
26+
export function getSetoid<L, R>(setoidLeftValue0: Setoid<L>, setoidRightValue0: Setoid<R>): Setoid<Either<L, R>> { return { equals: (x, y) => { if (x === y) {
27+
return true;
28+
} if (x.type === "Left" && y.type === "Left") {
29+
return setoidLeftValue0.equals(x.value0, y.value0);
30+
} if (x.type === "Right" && y.type === "Right") {
31+
return setoidRightValue0.equals(x.value0, y.value0);
32+
} return false; } }; }
33+

examples/FooBar.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

examples/FooBarBaz.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export type FooBarBaz = {
2+
readonly type: "Foo";
3+
} | {
4+
readonly type: "Bar";
5+
} | {
6+
readonly type: "Baz";
7+
};
8+
9+
export const foo: FooBarBaz = { type: "Foo" };
10+
11+
export const bar: FooBarBaz = { type: "Bar" };
12+
13+
export const baz: FooBarBaz = { type: "Baz" };
14+
15+
export function fold<R>(fa: FooBarBaz, onFoo: R, onBar: R, onBaz: R): R { switch (fa.type) {
16+
case "Foo": return onFoo;
17+
case "Bar": return onBar;
18+
case "Baz": return onBaz;
19+
} }
20+
21+
export function foldL<R>(fa: FooBarBaz, onFoo: () => R, onBar: () => R, onBaz: () => R): R { switch (fa.type) {
22+
case "Foo": return onFoo();
23+
case "Bar": return onBar();
24+
case "Baz": return onBaz();
25+
} }
26+
27+
import { Prism } from "monocle-ts";
28+
29+
export const _Foo: Prism<FooBarBaz, FooBarBaz> = Prism.fromPredicate(s => s.type === "Foo");
30+
31+
export const _Bar: Prism<FooBarBaz, FooBarBaz> = Prism.fromPredicate(s => s.type === "Bar");
32+
33+
export const _Baz: Prism<FooBarBaz, FooBarBaz> = Prism.fromPredicate(s => s.type === "Baz");
34+
35+
import { Setoid } from "fp-ts/lib/Setoid";
36+
37+
export function getSetoid(): Setoid<FooBarBaz> { return { equals: (x, y) => { if (x === y) {
38+
return true;
39+
} if (x.type === "Foo" && y.type === "Foo") {
40+
return true;
41+
} if (x.type === "Bar" && y.type === "Bar") {
42+
return true;
43+
} if (x.type === "Baz" && y.type === "Baz") {
44+
return true;
45+
} return false; } }; }
46+

examples/Maybe.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@ export function _nothing<A>(): Prism<Maybe<A>, Maybe<A>> { return Prism.fromPred
2525

2626
export function _just<A>(): Prism<Maybe<A>, Maybe<A>> { return Prism.fromPredicate(s => s.type === "Just"); }
2727

28+
import { Setoid } from "fp-ts/lib/Setoid";
29+
30+
export function getSetoid<A>(setoidJustValue: Setoid<A>): Setoid<Maybe<A>> { return { equals: (x, y) => { if (x === y) {
31+
return true;
32+
} if (x.type === "Nothing" && y.type === "Nothing") {
33+
return true;
34+
} if (x.type === "Just" && y.type === "Just") {
35+
return setoidJustValue.equals(x.value, y.value);
36+
} return false; } }; }
37+

examples/Option.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@ export function _none<A>(): Prism<Option<A>, Option<A>> { return Prism.fromPredi
2525

2626
export function _some<A>(): Prism<Option<A>, Option<A>> { return Prism.fromPredicate(s => s.type === "Some"); }
2727

28+
import { Setoid } from "fp-ts/lib/Setoid";
29+
30+
export function getSetoid<A>(setoidSomeValue0: Setoid<A>): Setoid<Option<A>> { return { equals: (x, y) => { if (x === y) {
31+
return true;
32+
} if (x.type === "None" && y.type === "None") {
33+
return true;
34+
} if (x.type === "Some" && y.type === "Some") {
35+
return setoidSomeValue0.equals(x.value0, y.value0);
36+
} return false; } }; }
37+

examples/State.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ export type State<S, A> = {
55

66
export function state<S, A>(value0: (s: S) => [A, S]): State<S, A> { return { type: "State", value0 }; }
77

8+
import { Setoid } from "fp-ts/lib/Setoid";
9+
10+
export function getSetoid<S, A>(setoidValue0: Setoid<(s: S) => [A, S]>): Setoid<State<S, A>> { return { equals: (x, y) => { if (x === y) {
11+
return true;
12+
} return setoidValue0.equals(x.value0, y.value0); } }; }
13+

0 commit comments

Comments
 (0)