time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (3789B)


      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 # uncurry
     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 uncurry = require( '@stdlib/utils/uncurry' );
     41 ```
     42 
     43 #### uncurry( 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( x ) {
     51     return function add( y ) {
     52         return x + y;
     53     };
     54 }
     55 
     56 var fcn = uncurry( add );
     57 
     58 var sum = fcn( 2, 3 );
     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( x ) {
     70     return function add( y ) {
     71         return x + y;
     72     };
     73 }
     74 
     75 var fcn = uncurry( 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 addX( x ) {
     87     this.x = x;
     88     return addY;
     89 }
     90 
     91 function addY( y ) {
     92     return this.x + y;
     93 }
     94 
     95 var fcn = uncurry( addX, {} );
     96 
     97 var sum = fcn( 2, 3 );
     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 addX( x ) {
    109     this.x = x;
    110     return addY;
    111 }
    112 
    113 function addY( y ) {
    114     return this.x + y;
    115 }
    116 
    117 var fcn = uncurry( addX, 2, {} );
    118 
    119 var sum = fcn( 2, 3 );
    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 </section>
    135 
    136 <!-- /.notes -->
    137 
    138 <!-- Package usage examples. -->
    139 
    140 <section class="examples">
    141 
    142 ## Examples
    143 
    144 <!-- eslint no-undef: "error" -->
    145 
    146 ```javascript
    147 var curry = require( '@stdlib/utils/curry' );
    148 var uncurry = require( '@stdlib/utils/uncurry' );
    149 
    150 var uncurried;
    151 var curried;
    152 var bool;
    153 var out;
    154 var i;
    155 
    156 function add( x, y, z, w, t, s ) {
    157     return x + y + z + w + t + s;
    158 }
    159 
    160 out = add( 0, 10, 20, 30, 40, 50 );
    161 // returns 150
    162 
    163 // Transform `add` into a curried function:
    164 curried = curry( add );
    165 out = curried;
    166 for ( i = 0; i < add.length; i++ ) {
    167     out = out( i*10 );
    168 }
    169 bool = ( out === 150 );
    170 // returns true
    171 
    172 // Uncurry a curried function:
    173 uncurried = uncurry( curried );
    174 
    175 out = uncurried( 0, 10, 20, 30, 40, 50 );
    176 // returns 150
    177 ```
    178 
    179 </section>
    180 
    181 <!-- /.examples -->
    182 
    183 <!-- 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. -->
    184 
    185 <section class="references">
    186 
    187 </section>
    188 
    189 <!-- /.references -->
    190 
    191 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    192 
    193 <section class="links">
    194 
    195 </section>
    196 
    197 <!-- /.links -->