Skip to content

Commit 264da2b

Browse files
committed
move generic method to IterableResult
1 parent 2a3f077 commit 264da2b

3 files changed

Lines changed: 26 additions & 26 deletions

File tree

src/compound.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@ export class IterableResult<T> {
137137
) as IterableResult<T>;
138138
}
139139

140+
static combinationOnTwo<T>(
141+
array: ReadonlyArray<T>,
142+
): IterableResult<readonly [ReadonlyArray<T>, ReadonlyArray<T>]> {
143+
if (array.length == 0) {
144+
return IterableResult.single([[], []]);
145+
} else {
146+
const init = array.slice(0, array.length - 1);
147+
const last = array[array.length - 1];
148+
return IterableResult.combinationOnTwo(init)
149+
.flatMap(([left, right]) =>
150+
IterableResult.fromArray([
151+
[[...left, last], right],
152+
[left, [...right, last]],
153+
])
154+
);
155+
}
156+
}
157+
140158
static handleThrows<T>(
141159
iterableResult: () => IterableResult<T>,
142160
): IterableResult<T> {

src/translator2/adjective.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { nullableAsArray } from "../misc/misc.ts";
44
import * as TokiPona from "../parser/ast.ts";
55
import * as English from "./ast.ts";
66
import { UntranslatableError } from "./error.ts";
7-
import { combinationOnTwo } from "./modifier.ts";
87
import { noEmphasis, word } from "./word.ts";
98

109
export type AdjectiveWithInWay = Readonly<{
@@ -113,12 +112,13 @@ export function shareAdverb(
113112
> {
114113
switch (adjective.type) {
115114
case "simple":
116-
return combinationOnTwo(adjective.adverbs).map<
117-
readonly [English.AdjectivePhrase, ReadonlyArray<English.Adverb>]
118-
>(([forAdjective, forAdverb]) => [
119-
{ ...adjective, adverbs: forAdjective },
120-
forAdverb,
121-
]);
115+
return IterableResult.combinationOnTwo(adjective.adverbs)
116+
.map<readonly [English.AdjectivePhrase, ReadonlyArray<English.Adverb>]>(
117+
([forAdjective, forAdverb]) => [
118+
{ ...adjective, adverbs: forAdjective },
119+
forAdverb,
120+
],
121+
);
122122
case "compound":
123123
return IterableResult.single<
124124
readonly [English.AdjectivePhrase, ReadonlyArray<English.Adverb>]

src/translator2/modifier.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ export function multipleModifiers(
273273
modifiers: ReadonlyArray<TokiPona.Modifier>,
274274
): IterableResult<MultipleModifierTranslation> {
275275
return IterableResult.combine(...modifiers.map(modifier))
276-
.flatMap(combinationOnTwo)
276+
.flatMap(IterableResult.combinationOnTwo)
277277
.filterMap(([forAdjectival, forAdverbial]) => {
278278
const adjectival = adjectivalModifier(forAdjectival);
279279
const adverbial = adverbialModifier(forAdverbial);
@@ -287,21 +287,3 @@ export function multipleModifiers(
287287
new ExhaustedError(modifiers.map(Composer.modifier).join(" "))
288288
);
289289
}
290-
// TODO: move this as IterableResult static method
291-
export function combinationOnTwo<T>(
292-
array: ReadonlyArray<T>,
293-
): IterableResult<readonly [ReadonlyArray<T>, ReadonlyArray<T>]> {
294-
if (array.length == 0) {
295-
return IterableResult.single([[], []]);
296-
} else {
297-
const init = array.slice(0, array.length - 1);
298-
const last = array[array.length - 1];
299-
return combinationOnTwo(init)
300-
.flatMap(([left, right]) =>
301-
IterableResult.fromArray([
302-
[[...left, last], right],
303-
[left, [...right, last]],
304-
])
305-
);
306-
}
307-
}

0 commit comments

Comments
 (0)