README.md (3048B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # compose 22 23 > [Function composition][function-composition]. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var compose = require( '@stdlib/utils/compose' ); 41 ``` 42 43 #### compose( ...fcn ) 44 45 Returns a composite function. Starting from the right, the composite function evaluates each function and passes the result as an argument to the next function. The result of the leftmost function is the result of the whole. 46 47 ```javascript 48 function a( x ) { 49 return 2 * x; 50 } 51 52 function b( x ) { 53 return x + 3; 54 } 55 56 function c( x ) { 57 return x / 5; 58 } 59 60 var f = compose( c, b, a ); 61 62 var z = f( 6 ); // ((2*x)+3)/5 63 // returns 3 64 ``` 65 66 **Only** the rightmost function is explicitly permitted to accept multiple arguments. All other functions are evaluated as **unary** functions. 67 68 ```javascript 69 function a( x, y ) { 70 return (x*5) + (y*3); 71 } 72 73 function b( r ) { 74 return r + 12; 75 } 76 77 var f = compose( b, a ); 78 79 var z = f( 4, 6 ); 80 // returns 50 81 ``` 82 83 </section> 84 85 <!-- /.usage --> 86 87 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 88 89 <section class="notes"> 90 91 ## Notes 92 93 - The function will throw if provided fewer than `2` input arguments. 94 95 </section> 96 97 <!-- /.notes --> 98 99 <!-- Package usage examples. --> 100 101 <section class="examples"> 102 103 ## Examples 104 105 <!-- eslint no-undef: "error" --> 106 107 ```javascript 108 var compose = require( '@stdlib/utils/compose' ); 109 110 function a( x, y ) { 111 return x * y; 112 } 113 114 function b( z ) { 115 return z + 5; 116 } 117 118 function c( r ) { 119 return r / 10; 120 } 121 122 var f = compose( c, b, a ); 123 124 var v = f( 5, 3 ); 125 // returns 2 126 ``` 127 128 </section> 129 130 <!-- /.examples --> 131 132 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 133 134 <section class="references"> 135 136 </section> 137 138 <!-- /.references --> 139 140 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 141 142 <section class="links"> 143 144 [function-composition]: https://en.wikipedia.org/wiki/Function_composition_%28computer_science%29 145 146 </section> 147 148 <!-- /.links -->