Skip to content
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
2 changes: 1 addition & 1 deletion packages/fjl/src/function/compose.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {reduceRight} from '../list/utils/reduceRight';
import {reduceRight} from '../list/foldr';
import {Unary} from "../types";

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/fjl/src/list/breakOnList.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {negateF3} from "../function/negate";
import {findIndexWhere} from "./utils/findIndexWhere";
import {findIndex} from "./findIndex";
import {reverse} from "./reverse";
import {splitAt} from "./splitAt";
import {TernaryPred} from "../types";
Expand All @@ -23,7 +23,7 @@ export const
pred: TernaryPred<T, number, TS>,
xs: TS
): [typeof xs, typeof xs] => {
const splitPoint = findIndexWhere(negateF3(pred), xs);
const splitPoint = findIndex(negateF3(pred), xs);
return splitPoint === -1 ?
[xs.slice(0, 0) as TS, xs.slice(0) as TS] :
reverse(splitAt(splitPoint, xs));
Expand Down
2 changes: 1 addition & 1 deletion packages/fjl/src/list/complement.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {difference} from "./difference";
import {reduce} from "./utils";
import {reduce} from "./foldl";

export const

Expand Down
2 changes: 1 addition & 1 deletion packages/fjl/src/list/difference.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {reduce} from "./utils";
import {reduce} from "./foldl";


/**
Expand Down
4 changes: 2 additions & 2 deletions packages/fjl/src/list/dropWhileEnd.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {findIndexWhereRight} from "./utils";
import {findLastIndex} from "./findLastIndex";
import {TernaryPred} from "../types";
import {negateF3} from "../function";

Expand All @@ -12,7 +12,7 @@ export const
xs: TS
): typeof xs => {
const splitPoint: number =
findIndexWhereRight(negateF3(pred), xs);
findLastIndex(negateF3(pred), xs);
if (splitPoint === -1) {
return xs.constructor() as TS;
}
Expand Down
26 changes: 22 additions & 4 deletions packages/fjl/src/list/find.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
import {$findWhere, findWhere} from "./utils";
import {NumberIndexable, TernaryPred} from "../types";

export const

/**
* Find an item in structure of elements based on given predicate (`pred`).
* Finds an item by predicate or returns `undefined`.
*/
$find = $findWhere,
find = (
pred: TernaryPred,
xs: NumberIndexable
): undefined | any => {
const limit = xs.length;
if (!limit) return;
for (let ind = 0; ind < limit; ind++) {
const elm = xs[ind];
if (pred(elm, ind, xs)) return elm;
}
return undefined;
},

find = findWhere;
/**
* Curried version of `find`.
*/
$find = (pred: TernaryPred) =>
(xs: NumberIndexable): undefined | any =>
find(pred, xs)

;
22 changes: 17 additions & 5 deletions packages/fjl/src/list/findIndex.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import {$findIndexWhere, findIndexWhere} from "./utils";
import {NumberIndexable, TernaryPred} from "../types";

export const

/**
* Curried version of `findIndex`.
* Finds index in "number indexable" (string|array|etc.) that matches given predicate or -1.
*
* @todo Should support any indexable/Iterable/IterableIterator types
*/
$findIndex = $findIndexWhere,
findIndex = (
pred: TernaryPred,
xs: NumberIndexable
): number => {
const limit = xs.length;
for (let ind = 0; ind < limit; ind += 1)
if (pred(xs[ind], ind, xs)) return ind;
return -1;
},

/**
* Finds index slice that matches given predicate or -1.
* Curried version of `findIndex`.
*/
findIndex = findIndexWhere
$findIndex = (pred: TernaryPred) =>
(xs: NumberIndexable): number =>
findIndex(pred, xs)

;
24 changes: 21 additions & 3 deletions packages/fjl/src/list/findIndices.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import {$findIndicesWhere, findIndicesWhere} from "./utils";
import {NumberIndexable, TernaryPred} from "../types";

export const

$findIndices = $findIndicesWhere,
/**
* Finds indices by predicate.
*/
findIndices = (
pred: TernaryPred,
xs: NumberIndexable
): number[] | undefined => {
const limit = xs.length;
const out = [];
for (let ind = 0; ind < limit; ind++)
if (pred(xs[ind], ind, xs)) out.push(ind);
return out.length ? out : undefined;
},

findIndices = findIndicesWhere;
/**
* Curried version of `findIndices`.
*/
$findIndices = (pred: TernaryPred) =>
(xs: NumberIndexable): number[] | undefined =>
findIndices(pred, xs)

;
24 changes: 24 additions & 0 deletions packages/fjl/src/list/findLastIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {NumberIndexable, TernaryPred} from "../types";

export const

/**
* Searches from the end of the list, and returns the index of the first match, or -1.
*/
findLastIndex = (
pred: TernaryPred,
xs: NumberIndexable
): number => {
for (let ind = xs.length - 1; ind >= 0; ind -= 1)
if (pred(xs[ind], ind, xs)) return ind;
return -1;
},

/**
* Curried version of `findLastIndex`.
*/
$findLastIndex = (pred: TernaryPred) =>
(xs: NumberIndexable): number =>
findLastIndex(pred, xs)

;
33 changes: 29 additions & 4 deletions packages/fjl/src/list/foldl.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
import {$reduce, reduce} from "./utils";
import {NumberIndexable, Quaternary} from "../types";

export const

$foldl = $reduce,
/**
* Left associative fold. Reduces a "number indexable" (string|array|etc.) down by the given
* operation (same as [].reduce but also for strings and arbitrary "number indexable" objects).
*/
foldl = (op: Quaternary, agg: any, xs: NumberIndexable): ReturnType<typeof op> => {
const limit = xs.length;
let result = agg;
for (let i = 0; i < limit; i += 1)
result = op(result, xs[i], i, xs);
return result;
},

/**
* Curried version of `foldl`.
*/
$foldl = (op: Quaternary) =>
(agg: any) =>
(xs: NumberIndexable): ReturnType<typeof op> =>
foldl(op, agg, xs),

/**
* Left associative fold. Reduces a container of elements down by the given operation (same as [].reduce).
* Alias of `foldl`.
*/
foldl = reduce;
reduce = foldl,

/**
* Alias of `$foldl`.
*/
$reduce = $foldl

;
2 changes: 1 addition & 1 deletion packages/fjl/src/list/foldl1.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {uncons} from "./uncons";
import {reduce} from "./utils/reduce";
import {reduce} from "./foldl";
import {Quaternary} from "../types";

export const
Expand Down
31 changes: 27 additions & 4 deletions packages/fjl/src/list/foldr.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
import {$reduceRight, reduceRight} from "./utils";
import {NumberIndexable, Quaternary} from "../types";

export const

$foldr = $reduceRight,
/**
* Right associative fold. Reduces a "number indexable" (string|array|etc.) down by the given
* operation (same as [].reduceRight but also for strings and arbitrary "number indexable" objects).
*/
foldr = (op: Quaternary, agg: any, xs: NumberIndexable): ReturnType<typeof op> => {
const limit = xs.length;
let result = agg;
for (let ind = limit - 1; ind >= 0; ind--)
result = op(result, xs[ind], ind, xs);
return result;
},

/**
* Curried version of `foldr`.
*/
$foldr = (op: Quaternary) =>
(agg: any) =>
(xs: NumberIndexable): ReturnType<typeof op> =>
foldr(op, agg, xs),

/**
* Alias of `foldr`.
*/
reduceRight = foldr,

/**
* Right associative fold. Reduces a container of elements down by the given operation (same as [].reduceRight).
* Alias of `$foldr`.
*/
foldr = reduceRight
$reduceRight = $foldr

;
2 changes: 1 addition & 1 deletion packages/fjl/src/list/foldr1.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {reduceRight} from "./utils/reduceRight";
import {reduceRight} from "./foldr";
import {Quaternary} from "../types";
import {unconsr} from "./unconsr";

Expand Down
3 changes: 3 additions & 0 deletions packages/fjl/src/list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export * from './filter';
export * from './find';
export * from './findIndex';
export * from './findIndices';
export * from './findLastIndex';
export * from './foldl';
export * from './foldl1';
export * from './foldr';
Expand Down Expand Up @@ -64,6 +65,8 @@ export * from './product';
export * from './push';
export * from './pushN';
export * from './range';
export * from './reduceUntil';
export * from './reduceUntilRight';
export * from './remove';
export * from './removeBy';
export * from './removeFirstsBy';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {NumberIndexable, Quaternary, TernaryPred} from "../../types";
import {NumberIndexable, Quaternary, TernaryPred} from "../types";

export const

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {NumberIndexable, Quaternary, TernaryPred} from "../../types";
import {NumberIndexable, Quaternary, TernaryPred} from "../types";

export const

Expand Down
4 changes: 2 additions & 2 deletions packages/fjl/src/list/span.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {negateF3} from "../function/negate";
import {findIndexWhere} from "./utils";
import {findIndex} from "./findIndex";
import {of} from "../object/of";
import {splitAt} from "./splitAt";
import {sliceFrom} from "./utils/sliceFrom";
Expand All @@ -13,7 +13,7 @@ export const
* array of arrays; E.g., given list of 'xs' - [[matching-xs-items], [non-matching-xs-items]]
*/
span = <T, TS extends string | T[]>(pred: TernaryPred, xs: TS): [TS, TS] => {
const splitPoint = findIndexWhere(negateF3(pred), xs);
const splitPoint = findIndex(negateF3(pred), xs);
return splitPoint === -1 ?
[sliceFrom(0, xs), of(xs)] :
splitAt(splitPoint, xs);
Expand Down
30 changes: 0 additions & 30 deletions packages/fjl/src/list/utils/findIndexWhere.ts

This file was deleted.

24 changes: 0 additions & 24 deletions packages/fjl/src/list/utils/findIndexWhereRight.ts

This file was deleted.

26 changes: 0 additions & 26 deletions packages/fjl/src/list/utils/findIndicesWhere.ts

This file was deleted.

Loading
Loading