Skip to content

Commit 096f2ad

Browse files
committed
Make classes smaller
1 parent f2e4952 commit 096f2ad

3 files changed

Lines changed: 24 additions & 46 deletions

File tree

src/either.js

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@ class Either {
55
}
66

77
class Left extends Either {
8-
get isLeft() {
9-
return true
10-
}
11-
get isRight() {
12-
return false
13-
}
14-
// ----- Functor (Either a)
15-
map() {
16-
return this
17-
}
18-
join() {
19-
return this
8+
constructor(x) {
9+
super(x)
10+
this.isLeft = true
11+
this.isRight = false
12+
// ----- Functor (Either a)
13+
this.map = () => this
14+
this.join = () => this
2015
}
2116
// ----- Applicative (Either a)
2217
// ap() { return this }
@@ -29,18 +24,14 @@ class Left extends Either {
2924
}
3025

3126
class Right extends Either {
32-
get isLeft() {
33-
return false
34-
}
35-
get isRight() {
36-
return true
37-
}
38-
// ----- Functor (Either a)
39-
map(fn) {
40-
return Either.of(fn(this.$value))
41-
}
42-
join() {
43-
return this.$value
27+
constructor(x) {
28+
super(x)
29+
this.$value = x
30+
this.isLeft = false
31+
this.isRight = true
32+
// ----- Functor (Either a)
33+
this.map = fn => Either.of(fn(x))
34+
this.join = () => this.$value
4435
}
4536
// ----- Applicative (Either a)
4637
// ap(f) { return f.map(this.$value) }

src/identity.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
class Identity {
22
constructor(x) {
3-
this.$value = x
4-
}
5-
// ----- Functor Identity
6-
map(fn) {
7-
return Identity.of(fn(this.$value))
8-
}
9-
// ----- Monad Identity
10-
chain(fn) {
11-
return this.map(fn).join()
12-
}
13-
join() {
14-
return this.$value
3+
// ----- Functor Identity
4+
this.map = fn => Identity.of(fn(x))
5+
// ----- Monad Identity
6+
this.chain = fn => this.map(fn).join()
7+
this.join = () => x
158
}
169
// inspect() { return `Identity(${this.$value})` }
1710
// ----- Applicative Identity

src/maybe.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
class Maybe {
2-
get nil() {
3-
return this.$value === undefined
4-
}
52
constructor(x) {
63
this.$value = x
7-
}
8-
// ----- Functor Maybe
9-
map(fn) {
10-
return this.nil ? this : Maybe.of(fn(this.$value))
11-
}
12-
join() {
13-
return this.nil ? this : this.$value
4+
this.nil = x === undefined
5+
// ----- Functor Maybe
6+
this.map = fn => (this.nil ? this : Maybe.of(fn(x)))
7+
this.join = () => (this.nil ? this : x)
148
}
159
// inspect() { return `Maybe(${this.$value}`; }
1610
// ----- Applicative Maybe

0 commit comments

Comments
 (0)