README.md (4692B)
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 # uncurryRight 22 23 > Transform a curried function into a function invoked with multiple arguments. 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 uncurryRight = require( '@stdlib/utils/uncurry-right' ); 41 ``` 42 43 #### uncurryRight( fcn\[, arity]\[, thisArg] ) 44 45 Transforms a curried function into a function invoked with multiple arguments. 46 47 <!-- eslint-disable no-restricted-syntax --> 48 49 ```javascript 50 function add( y ) { 51 return function add( x ) { 52 return x + y; 53 }; 54 } 55 56 var fcn = uncurryRight( add ); 57 58 var sum = fcn( 3, 2 ); 59 // returns 5 60 ``` 61 62 To enforce a fixed number of parameters, provide an `arity` argument. 63 64 <!-- run throws: true --> 65 66 <!-- eslint-disable no-restricted-syntax --> 67 68 ```javascript 69 function add( y ) { 70 return function add( x ) { 71 return x + y; 72 }; 73 } 74 75 var fcn = uncurryRight( add, 2 ); 76 77 var sum = fcn( 9 ); 78 // throws <Error> 79 ``` 80 81 To specify an execution context, provide a `thisArg` argument. 82 83 <!-- eslint-disable no-invalid-this --> 84 85 ```javascript 86 function addY( y ) { 87 this.y = y; 88 return addX; 89 } 90 91 function addX( x ) { 92 return x + this.y; 93 } 94 95 var fcn = uncurryRight( addY, {} ); 96 97 var sum = fcn( 3, 2 ); 98 // returns 5 99 ``` 100 101 The function supports providing both an `arity` and execution context. 102 103 <!-- run throws: true --> 104 105 <!-- eslint-disable no-invalid-this --> 106 107 ```javascript 108 function addY( y ) { 109 this.y = y; 110 return addX; 111 } 112 113 function addX( x ) { 114 return x + this.y; 115 } 116 117 var fcn = uncurryRight( addY, 2, {} ); 118 119 var sum = fcn( 3, 2 ); 120 // returns 5 121 122 sum = fcn( 4 ); 123 // throws <Error> 124 ``` 125 126 </section> 127 128 <!-- /.usage --> 129 130 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 131 132 <section class="notes"> 133 134 ## Notes 135 136 - The difference between this function and [`uncurry`][@stdlib/utils/uncurry] is the order in which arguments are applied. This function applies arguments starting from the right. 137 138 </section> 139 140 <!-- /.notes --> 141 142 <!-- Package usage examples. --> 143 144 <section class="examples"> 145 146 ## Examples 147 148 <!-- eslint no-undef: "error" --> 149 150 ```javascript 151 var fromCodePoint = require( '@stdlib/string/from-code-point' ); 152 var curryRight = require( '@stdlib/utils/curry-right' ); 153 var uncurryRight = require( '@stdlib/utils/uncurry-right' ); 154 155 var uncurried; 156 var curried; 157 var abcs; 158 var out; 159 var a; 160 var i; 161 162 function concat() { 163 var len; 164 var out; 165 var i; 166 167 len = arguments.length; 168 out = ''; 169 for ( i = 0; i < len; i++ ) { 170 out += arguments[ i ]; 171 if ( i < len-1 ) { 172 out += ','; 173 } 174 } 175 return out; 176 } 177 178 // Character codes: 179 a = 97; 180 181 abcs = new Array( 26 ); 182 for ( i = 0; i < abcs.length; i++ ) { 183 abcs[ i ] = fromCodePoint( a + i ); 184 } 185 out = concat.apply( null, abcs ); 186 // returns 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z' 187 188 // Transform `concat` into a right curried function: 189 curried = curryRight( concat, 26 ); 190 191 out = curried; 192 for ( i = abcs.length-1; i >= 0; i-- ) { 193 out = out( abcs[ i ] ); 194 } 195 console.log( out ); 196 // => 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z' 197 198 // Uncurry a right curried function: 199 uncurried = uncurryRight( curried ); 200 201 out = uncurried.apply( null, abcs ); 202 // returns 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z' 203 ``` 204 205 </section> 206 207 <!-- /.examples --> 208 209 <!-- 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. --> 210 211 <section class="references"> 212 213 </section> 214 215 <!-- /.references --> 216 217 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 218 219 <section class="links"> 220 221 [@stdlib/utils/uncurry]: https://www.npmjs.com/package/@stdlib/utils/tree/main/uncurry 222 223 </section> 224 225 <!-- /.links -->