-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from functional-jslib/dev
Remove the use `unknown` as generic defaults and build improvements.
- Loading branch information
Showing
25 changed files
with
153 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"files": [ | ||
"./src/index.ts" | ||
], | ||
"exclude": [ | ||
"dist/", | ||
"node_modules/", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,8 @@ | |
"name": "fjl", | ||
"version": "2.0.0-alpha.5", | ||
"description": "Functional Javascript Library.", | ||
"main": "./dist/cjs/index.js", | ||
"module": "./dist/esm/index.js", | ||
"main": "./dist/cjs/index.cjs", | ||
"module": "./dist/esm/index.mjs", | ||
"typings": "./dist/esm/index.d.ts", | ||
"files": [ | ||
"./dist", | ||
|
@@ -23,16 +23,6 @@ | |
"prelude", | ||
"combinators" | ||
], | ||
"exports": { | ||
".": { | ||
"require": "./dist/cjs/index.js", | ||
"module": "./dist/esm/index.js" | ||
}, | ||
"./*": { | ||
"require": "./dist/cjs/*.js", | ||
"module": "./dist/esm/*.js" | ||
} | ||
}, | ||
"author": "Ely De La Cruz <[email protected]>", | ||
"license": "BSD-3-Clause", | ||
"bugs": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,25 @@ | ||
import {concat} from "../_platform/slice"; | ||
import {map} from "./map"; | ||
import {MapOp, Slice} from "../types"; | ||
import type {Ternary} from "../types"; | ||
|
||
export const | ||
|
||
/** | ||
* Map a function over all the elements of a container of containers and concatenate the results. | ||
*/ | ||
concatMap = <T = any>(fn: MapOp, arr: Slice<T>[]): any[] => | ||
concatMap = <T=any, TS extends any[]=any[]>( | ||
fn: Ternary<T, number, TS[]>, | ||
arr: TS[] | ||
): ReturnType<typeof fn>[] => | ||
concat(map(fn, arr)), | ||
|
||
$concatMap = <T = any>(fn: MapOp) => | ||
(arr: T[][]): any[] => | ||
/** | ||
* Curried version of `concatMap`. | ||
*/ | ||
$concatMap = <T=any, TS extends any[]=any[]>( | ||
fn: Ternary<T, number, TS[]> | ||
) => | ||
(arr: TS[]): ReturnType<typeof fn>[] => | ||
concatMap(fn, arr) | ||
|
||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
import {ForEachOp} from "../types"; | ||
import {Ternary} from "../types"; | ||
|
||
export const | ||
|
||
/** | ||
* "For each" function - same as `[].forEach` except for iterables (strings, `Map`s, ...) in general. | ||
*/ | ||
forEach = <T>(fn: ForEachOp, iter: Iterable<T>): void => { | ||
forEach = <T, TS extends Iterable<T>>(fn: Ternary<T, number, TS>, iter: TS): void => { | ||
if (!iter) return; | ||
let ind = 0; | ||
for (const x of iter) { | ||
fn(x, ind++, iter); | ||
} | ||
}, | ||
|
||
$forEach = <T>(fn: ForEachOp) => | ||
(iter: Iterable<T>): void => forEach(fn, iter) | ||
$forEach = <T, TS extends Iterable<T>>(fn: Ternary<T, number, TS>) => | ||
(iter: TS): void => forEach(fn, iter) | ||
|
||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,27 @@ | ||
import {Constructable, MapOp} from "../types"; | ||
import {ArrayTypeConstructor, Ternary} from "../types"; | ||
|
||
export const | ||
|
||
/** | ||
* Maps a function onto a ListLike (string or array) or a functor (value containing a map method). | ||
* Maps a function over an "array type" container of values. | ||
*/ | ||
map = <T = any>( | ||
fn: MapOp, | ||
xs: T[] | ||
map = <T=any, TS extends any[]=any[]>( | ||
fn: Ternary<T, number, TS>, | ||
xs: TS | ||
): ReturnType<typeof fn>[] => { | ||
const limit = xs.length, | ||
out = new (xs.constructor as Constructable)(limit); | ||
out = new (xs.constructor as ArrayTypeConstructor)(limit); | ||
for (let i = 0; i < limit; i += 1) { | ||
out[i] = fn(xs[i], i, xs); | ||
} | ||
return out; | ||
return out as ReturnType<typeof fn>[]; | ||
}, | ||
|
||
$map = <T = any> | ||
(fn: MapOp) => | ||
(xs: T[]): ReturnType<typeof fn>[] => | ||
/** | ||
* Curried version of `map` method. | ||
*/ | ||
$map = <T=any, TS extends any[]=any[]> | ||
(fn: Ternary<T, number, TS>) => | ||
(xs: TS): ReturnType<typeof fn>[] => | ||
map(fn, xs) | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,39 @@ | ||
import {MapAccumOp, Slice} from "../types"; | ||
import {Quaternary, Slice} from "../types"; | ||
|
||
export const | ||
|
||
/** | ||
* Performs a map and a reduce, all in one (from right-to-left), on each item, and returns the result of the reduce, | ||
* and the map, as a tuple. | ||
* Performs a map and a reduce, all in one (from right-to-left), on each item in the given array, and | ||
* returns the result of the reduce, and the map, as a tuple, respectively. | ||
*/ | ||
mapAccumR = <A, B, C>( | ||
op: MapAccumOp<A, B, C>, | ||
zero: A, | ||
xs: Slice<B> | ||
): [A, Slice<C>] => { | ||
const limit = xs.length; | ||
mapAccumR = <AccumVal, B, MapOfB, Bs extends Slice<B>, MapOfBs extends Slice<MapOfB>>( | ||
op: Quaternary<AccumVal, B, number, Bs, [AccumVal, MapOfB]>, | ||
zero: AccumVal, | ||
bs: Bs | ||
): [AccumVal, MapOfBs] => { | ||
const limit = bs.length; | ||
|
||
if (!limit) return [zero, xs.slice(0) as unknown as Slice<C>]; | ||
if (!limit) return [zero, bs.slice(0) as unknown as MapOfBs]; | ||
|
||
const mapped = []; | ||
|
||
let agg = zero; | ||
|
||
for (let i = limit - 1; i >= 0; i--) { | ||
const tuple = op(agg, xs[i] as B, i); | ||
const tuple = op(agg, bs[i] as B, i, bs); | ||
agg = tuple[0]; | ||
mapped.push(tuple[1]); | ||
} | ||
|
||
return [agg, mapped]; | ||
return [agg, mapped as unknown as MapOfBs]; | ||
}, | ||
|
||
/** | ||
* Curried version of `mapAccumR`. | ||
*/ | ||
$mapAccumR = <A, B, C>(op: MapAccumOp<A, B, C>) => | ||
(zero: A) => | ||
(xs: Slice<B>): [A, Slice] => | ||
mapAccumR(op, zero, xs); | ||
$mapAccumR = <AccumVal, B, MapOfB, Bs extends Slice<B>, MapOfBs extends Slice<MapOfB>>( | ||
op: Quaternary<AccumVal, B, number, Bs, [AccumVal, MapOfB]>, | ||
) => | ||
(zero: AccumVal) => | ||
(bs: Bs): [AccumVal, MapOfBs] => | ||
mapAccumR(op, zero, bs); |
Oops, something went wrong.