Skip to content

Commit 3be414a

Browse files
authored
Merge pull request #72 from mkantor/leave-it-to-either
Improve usage of `@matt.kantor/either` package
2 parents 456f713 + 75ad9b8 commit 3be414a

File tree

6 files changed

+52
-73
lines changed

6 files changed

+52
-73
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"typescript": "^5.8.3"
2222
},
2323
"dependencies": {
24-
"@matt.kantor/either": "^1.0.0",
24+
"@matt.kantor/either": "^1.2.0",
2525
"@matt.kantor/option": "^1.0.0",
2626
"@matt.kantor/parsing": "^2.0.0",
2727
"kleur": "^4.1.5"

src/language/compiling/semantics/keyword-handlers/function-handler.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,8 @@ const apply = (
8989
),
9090
)
9191

92-
if (either.isLeft(result)) {
93-
return either.makeLeft({
94-
kind: 'panic',
95-
message: result.value.message,
96-
})
97-
} else {
98-
return result
99-
}
92+
return either.mapLeft(result, error => ({
93+
kind: 'panic',
94+
message: error.message,
95+
}))
10096
}

src/language/runtime/keywords.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,12 @@ export const keywordHandlers: KeywordHandlers = {
160160
const result = runtimeFunction(
161161
runtimeContext(runtimeFunction.parameterName),
162162
)
163-
if (either.isLeft(result)) {
164-
// The runtime function panicked or had an unavailable dependency (which results in a panic
165-
// anyway in this context).
166-
return either.makeLeft({
167-
kind: 'panic',
168-
message: result.value.message,
169-
})
170-
} else {
171-
return result
172-
}
163+
return either.mapLeft(result, error => ({
164+
// The runtime function panicked or had an unavailable dependency (which results in a
165+
// panic anyway in this context).
166+
kind: 'panic',
167+
message: error.message,
168+
}))
173169
}
174170
},
175171
),

src/language/semantics/stdlib/atom.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,14 @@ export const atom = {
6161
},
6262
serializeOnceAppliedFunction(['atom', 'prepend'], atomToPrepend),
6363
option.none,
64-
atomToPrependTo => {
65-
if (
66-
typeof atomToPrepend !== 'string' ||
67-
typeof atomToPrependTo !== 'string'
68-
) {
69-
return either.makeLeft({
70-
kind: 'panic',
71-
message: 'prepend received a non-atom argument',
72-
})
73-
} else {
74-
return either.makeRight(atomToPrepend + atomToPrependTo)
75-
}
76-
},
64+
atomToPrependTo =>
65+
typeof atomToPrepend !== 'string' ||
66+
typeof atomToPrependTo !== 'string'
67+
? either.makeLeft({
68+
kind: 'panic',
69+
message: 'prepend received a non-atom argument',
70+
})
71+
: either.makeRight(atomToPrepend + atomToPrependTo),
7772
),
7873
),
7974
),

src/language/unparsing/plz-utilities.ts

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,9 @@ export const moleculeUnparser =
6262
value,
6363
unparseAtomOrMolecule,
6464
)
65-
if (either.isLeft(result)) {
66-
return unparseSugarFreeMolecule(value, unparseAtomOrMolecule)
67-
} else {
68-
return result
69-
}
65+
return either.flatMapLeft(result, _ =>
66+
unparseSugarFreeMolecule(value, unparseAtomOrMolecule),
67+
)
7068
} else {
7169
return unparseSugarFreeMolecule(value, unparseAtomOrMolecule)
7270
}
@@ -156,34 +154,30 @@ const unparseSugaredApply = (
156154
unparseAtomOrMolecule: UnparseAtomOrMolecule,
157155
) => {
158156
const { closeParenthesis, openParenthesis } = punctuation(kleur)
159-
const functionUnparseResult = either.map(
160-
either.flatMap(
161-
serializeIfNeeded(expression[1].function),
162-
unparseAtomOrMolecule,
157+
return either.flatMap(
158+
either.map(
159+
either.flatMap(
160+
serializeIfNeeded(expression[1].function),
161+
unparseAtomOrMolecule,
162+
),
163+
unparsedFunction =>
164+
either.isRight(readFunctionExpression(expression[1].function))
165+
? // Immediately-applied function expressions need parentheses.
166+
openParenthesis.concat(unparsedFunction).concat(closeParenthesis)
167+
: unparsedFunction,
163168
),
164169
unparsedFunction =>
165-
either.isRight(readFunctionExpression(expression[1].function))
166-
? // Immediately-applied function expressions need parentheses.
167-
openParenthesis.concat(unparsedFunction).concat(closeParenthesis)
168-
: unparsedFunction,
169-
)
170-
if (either.isLeft(functionUnparseResult)) {
171-
return functionUnparseResult
172-
}
173-
174-
const argumentUnparseResult = either.flatMap(
175-
serializeIfNeeded(expression[1].argument),
176-
unparseAtomOrMolecule,
177-
)
178-
if (either.isLeft(argumentUnparseResult)) {
179-
return argumentUnparseResult
180-
}
181-
182-
return either.makeRight(
183-
functionUnparseResult.value
184-
.concat(openParenthesis)
185-
.concat(argumentUnparseResult.value)
186-
.concat(closeParenthesis),
170+
either.map(
171+
either.flatMap(
172+
serializeIfNeeded(expression[1].argument),
173+
unparseAtomOrMolecule,
174+
),
175+
unparsedArgument =>
176+
unparsedFunction
177+
.concat(openParenthesis)
178+
.concat(unparsedArgument)
179+
.concat(closeParenthesis),
180+
),
187181
)
188182
}
189183

@@ -209,12 +203,10 @@ const unparseSugaredIndex = (
209203
serializeIfNeeded(expression[1].object),
210204
unparseAtomOrMolecule,
211205
)
212-
if (either.isLeft(objectUnparseResult)) {
213-
return objectUnparseResult
214-
} else {
206+
return either.flatMap(objectUnparseResult, unparsedObject => {
215207
if (typeof expression[1].query !== 'object') {
216208
// TODO: It would be nice if this were provably impossible.
217-
return either.makeLeft({
209+
return either.makeLeft<UnserializableValueError>({
218210
kind: 'unserializableValue',
219211
message: 'Invalid index expression',
220212
})
@@ -248,13 +240,13 @@ const unparseSugaredIndex = (
248240
} else {
249241
const { dot } = punctuation(kleur)
250242
return either.makeRight(
251-
objectUnparseResult.value
243+
unparsedObject
252244
.concat(dot)
253245
.concat(keyPath.map(quoteKeyPathComponentIfNecessary).join(dot)),
254246
)
255247
}
256248
}
257-
}
249+
})
258250
}
259251

260252
const unparseSugaredLookup = (

0 commit comments

Comments
 (0)