README.md (4575B)
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 # curryRight 22 23 > Transform a function into a sequence of functions each accepting a single argument. 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 curryRight = require( '@stdlib/utils/curry-right' ); 41 ``` 42 43 #### curryRight( fcn\[, arity]\[, thisArg] ) 44 45 Transforms a function into a sequence of functions each accepting a single argument. 46 47 ```javascript 48 function add( x, y ) { 49 return x + y; 50 } 51 52 var fcn = curryRight( add ); 53 54 var sum = fcn( 2 )( 3 ); 55 // returns 5 56 ``` 57 58 By default, `arity` is equal to `fcn.length`. For functions without explicit parameters, provide an `arity` argument. 59 60 ```javascript 61 function add() { 62 return arguments[ 0 ] + arguments[ 1 ]; 63 } 64 65 var fcn = curryRight( add, 2 ); 66 67 var sum = fcn( 2 )( 3 ); 68 // returns 5 69 ``` 70 71 To specify the curried function execution context, provide a `thisArg` argument. 72 73 <!-- eslint-disable no-restricted-syntax --> 74 75 ```javascript 76 var obj = { 77 'name': 'Ada', 78 'greet': function greet( word1, word2 ) { 79 return word1 + ' ' + word2 + ', ' + this.name + '!'; 80 } 81 }; 82 83 var fcn = curryRight( obj.greet, obj ); 84 85 var str = fcn( 'there' )( 'Hello' ); 86 // returns 'Hello there, Ada!' 87 ``` 88 89 The function supports providing both an `arity` and execution context. 90 91 <!-- eslint-disable no-restricted-syntax --> 92 93 ```javascript 94 var obj = { 95 'name': 'Ada', 96 'greet': function greet() { 97 return arguments[ 0 ] + ' ' + arguments[ 1 ] + ', ' + this.name + '!'; 98 } 99 }; 100 101 var fcn = curryRight( obj.greet, 2, obj ); 102 103 var str = fcn( 'there' )( 'Hello' ); 104 // returns 'Hello there, Ada!' 105 ``` 106 107 </section> 108 109 <!-- /.usage --> 110 111 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 112 113 <section class="notes"> 114 115 ## Notes 116 117 - Until return value resolution, each invocation returns a new partially applied curry function. 118 119 ```javascript 120 function add( x, y, z ) { 121 return x + y + z; 122 } 123 124 var fcn = curryRight( add ); 125 126 var s0 = fcn( 1 )( 2 )( 3 ); 127 // returns 6 128 129 s0 = fcn( -1 )( -2 )( -3 ); 130 // returns -6 131 132 s0 = fcn( 10 )( 20 )( 30 ); 133 // returns 60 134 135 // Return a partially applied curry function: 136 var f1 = fcn( 3 ); 137 138 var s1 = f1( 4 )( 5 ); 139 // returns 12 140 141 s1 = f1( 6 )( 7 ); 142 // returns 16 143 144 s1 = f1( 8 )( 9 ); 145 // returns 20 146 147 // Return a partially applied curry function: 148 var f2 = fcn( 4 )( 5 ); 149 150 var s2 = f2( 6 ); 151 // returns 15 152 153 s2 = f2( 70 ); 154 // returns 79 155 156 s2 = f2( 700 ); 157 // returns 709 158 ``` 159 160 - The difference between this function and [`curry`][@stdlib/utils/curry] is the order in which arguments are applied. This function applies arguments starting from the right. 161 162 </section> 163 164 <!-- /.notes --> 165 166 <!-- Package usage examples. --> 167 168 <section class="examples"> 169 170 ## Examples 171 172 <!-- eslint no-undef: "error" --> 173 174 ```javascript 175 var curryRight = require( '@stdlib/utils/curry-right' ); 176 177 var fcn; 178 var out; 179 var i; 180 181 function add( x, y, z, w, t, s ) { 182 return x + y + z + w + t + s; 183 } 184 185 fcn = curryRight( add ); 186 out = fcn; 187 for ( i = 0; i < add.length; i++ ) { 188 out = out( i*10 ); 189 } 190 console.log( out ); 191 ``` 192 193 </section> 194 195 <!-- /.examples --> 196 197 <!-- 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. --> 198 199 <section class="references"> 200 201 </section> 202 203 <!-- /.references --> 204 205 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 206 207 <section class="links"> 208 209 [@stdlib/utils/curry]: https://www.npmjs.com/package/@stdlib/utils/tree/main/curry 210 211 </section> 212 213 <!-- /.links -->