1 | /**
|
2 | * Higher order functions
|
3 | *
|
4 | * These utility functions are exported, but are subject to change without notice.
|
5 | *
|
6 | * @packageDocumentation
|
7 | */
|
8 | import { Predicate } from './common';
|
9 | /**
|
10 | * Returns a new function for [Partial Application](https://en.wikipedia.org/wiki/Partial_application) of the original function.
|
11 | *
|
12 | * Given a function with N parameters, returns a new function that supports partial application.
|
13 | * The new function accepts anywhere from 1 to N parameters. When that function is called with M parameters,
|
14 | * where M is less than N, it returns a new function that accepts the remaining parameters. It continues to
|
15 | * accept more parameters until all N parameters have been supplied.
|
16 | *
|
17 | *
|
18 | * This contrived example uses a partially applied function as an predicate, which returns true
|
19 | * if an object is found in both arrays.
|
20 | * @example
|
21 | * ```
|
22 | * // returns true if an object is in both of the two arrays
|
23 | * function inBoth(array1, array2, object) {
|
24 | * return array1.indexOf(object) !== -1 &&
|
25 | * array2.indexOf(object) !== 1;
|
26 | * }
|
27 | * let obj1, obj2, obj3, obj4, obj5, obj6, obj7
|
28 | * let foos = [obj1, obj3]
|
29 | * let bars = [obj3, obj4, obj5]
|
30 | *
|
31 | * // A curried "copy" of inBoth
|
32 | * let curriedInBoth = curry(inBoth);
|
33 | * // Partially apply both the array1 and array2
|
34 | * let inFoosAndBars = curriedInBoth(foos, bars);
|
35 | *
|
36 | * // Supply the final argument; since all arguments are
|
37 | * // supplied, the original inBoth function is then called.
|
38 | * let obj1InBoth = inFoosAndBars(obj1); // false
|
39 | *
|
40 | * // Use the inFoosAndBars as a predicate.
|
41 | * // Filter, on each iteration, supplies the final argument
|
42 | * let allObjs = [ obj1, obj2, obj3, obj4, obj5, obj6, obj7 ];
|
43 | * let foundInBoth = allObjs.filter(inFoosAndBars); // [ obj3 ]
|
44 | *
|
45 | * ```
|
46 | *
|
47 | * @param fn
|
48 | * @returns {*|function(): (*|any)}
|
49 | */
|
50 | export declare function curry(fn: Function): Function;
|
51 | /**
|
52 | * Given a varargs list of functions, returns a function that composes the argument functions, right-to-left
|
53 | * given: f(x), g(x), h(x)
|
54 | * let composed = compose(f,g,h)
|
55 | * then, composed is: f(g(h(x)))
|
56 | */
|
57 | export declare function compose(): () => any;
|
58 | /**
|
59 | * Given a varargs list of functions, returns a function that is composes the argument functions, left-to-right
|
60 | * given: f(x), g(x), h(x)
|
61 | * let piped = pipe(f,g,h);
|
62 | * then, piped is: h(g(f(x)))
|
63 | */
|
64 | export declare function pipe(...funcs: Function[]): (obj: any) => any;
|
65 | /**
|
66 | * Given a property name, returns a function that returns that property from an object
|
67 | * let obj = { foo: 1, name: "blarg" };
|
68 | * let getName = prop("name");
|
69 | * getName(obj) === "blarg"
|
70 | */
|
71 | export declare const prop: (name: string) => (obj: any) => any;
|
72 | /**
|
73 | * Given a property name and a value, returns a function that returns a boolean based on whether
|
74 | * the passed object has a property that matches the value
|
75 | * let obj = { foo: 1, name: "blarg" };
|
76 | * let getName = propEq("name", "blarg");
|
77 | * getName(obj) === true
|
78 | */
|
79 | export declare const propEq: Function;
|
80 | /**
|
81 | * Given a dotted property name, returns a function that returns a nested property from an object, or undefined
|
82 | * let obj = { id: 1, nestedObj: { foo: 1, name: "blarg" }, };
|
83 | * let getName = prop("nestedObj.name");
|
84 | * getName(obj) === "blarg"
|
85 | * let propNotFound = prop("this.property.doesnt.exist");
|
86 | * propNotFound(obj) === undefined
|
87 | */
|
88 | export declare const parse: (name: string) => any;
|
89 | /**
|
90 | * Given a function that returns a truthy or falsey value, returns a
|
91 | * function that returns the opposite (falsey or truthy) value given the same inputs
|
92 | */
|
93 | export declare const not: (fn: Predicate<any>) => Predicate<any>;
|
94 | /**
|
95 | * Given two functions that return truthy or falsey values, returns a function that returns truthy
|
96 | * if both functions return truthy for the given arguments
|
97 | */
|
98 | export declare function and(fn1: Predicate<any>, fn2: Predicate<any>): Predicate<any>;
|
99 | /**
|
100 | * Given two functions that return truthy or falsey values, returns a function that returns truthy
|
101 | * if at least one of the functions returns truthy for the given arguments
|
102 | */
|
103 | export declare function or(fn1: Predicate<any>, fn2: Predicate<any>): Predicate<any>;
|
104 | /**
|
105 | * Check if all the elements of an array match a predicate function
|
106 | *
|
107 | * @param fn1 a predicate function `fn1`
|
108 | * @returns a function which takes an array and returns true if `fn1` is true for all elements of the array
|
109 | */
|
110 | export declare const all: (fn1: Predicate<any>) => (arr: any[]) => boolean;
|
111 | export declare const any: (fn1: Predicate<any>) => (arr: any[]) => boolean;
|
112 | /** Given a class, returns a Predicate function that returns true if the object is of that class */
|
113 | export declare const is: <T>(ctor: {
|
114 | new (...args: any[]): T;
|
115 | }) => (obj: any) => obj is T;
|
116 | /** Given a value, returns a Predicate function that returns true if another value is === equal to the original value */
|
117 | export declare const eq: (comp: any) => Predicate<any>;
|
118 | /** Given a value, returns a function which returns the value */
|
119 | export declare const val: <T>(v: T) => () => T;
|
120 | export declare function invoke(fnName: string): Function;
|
121 | export declare function invoke(fnName: string, args: any[]): Function;
|
122 | /**
|
123 | * Sorta like Pattern Matching (a functional programming conditional construct)
|
124 | *
|
125 | * See http://c2.com/cgi/wiki?PatternMatching
|
126 | *
|
127 | * This is a conditional construct which allows a series of predicates and output functions
|
128 | * to be checked and then applied. Each predicate receives the input. If the predicate
|
129 | * returns truthy, then its matching output function (mapping function) is provided with
|
130 | * the input and, then the result is returned.
|
131 | *
|
132 | * Each combination (2-tuple) of predicate + output function should be placed in an array
|
133 | * of size 2: [ predicate, mapFn ]
|
134 | *
|
135 | * These 2-tuples should be put in an outer array.
|
136 | *
|
137 | * @example
|
138 | * ```
|
139 | *
|
140 | * // Here's a 2-tuple where the first element is the isString predicate
|
141 | * // and the second element is a function that returns a description of the input
|
142 | * let firstTuple = [ angular.isString, (input) => `Heres your string ${input}` ];
|
143 | *
|
144 | * // Second tuple: predicate "isNumber", mapfn returns a description
|
145 | * let secondTuple = [ angular.isNumber, (input) => `(${input}) That's a number!` ];
|
146 | *
|
147 | * let third = [ (input) => input === null, (input) => `Oh, null...` ];
|
148 | *
|
149 | * let fourth = [ (input) => input === undefined, (input) => `notdefined` ];
|
150 | *
|
151 | * let descriptionOf = pattern([ firstTuple, secondTuple, third, fourth ]);
|
152 | *
|
153 | * console.log(descriptionOf(undefined)); // 'notdefined'
|
154 | * console.log(descriptionOf(55)); // '(55) That's a number!'
|
155 | * console.log(descriptionOf("foo")); // 'Here's your string foo'
|
156 | * ```
|
157 | *
|
158 | * @param struct A 2D array. Each element of the array should be an array, a 2-tuple,
|
159 | * with a Predicate and a mapping/output function
|
160 | * @returns {function(any): *}
|
161 | */
|
162 | export declare function pattern(struct: Function[][]): Function;
|