time-to-botec

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

README.md (4250B)


      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 # curry
     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 curry = require( '@stdlib/utils/curry' );
     41 ```
     42 
     43 #### curry( 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 = curry( 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 = curry( 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 = curry( obj.greet, obj );
     84 
     85 var str = fcn( 'Hello' )( 'there' );
     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 = curry( obj.greet, 2, obj );
    102 
    103 var str = fcn( 'Hello' )( 'there' );
    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 = curry( 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 </section>
    161 
    162 <!-- /.notes -->
    163 
    164 <!-- Package usage examples. -->
    165 
    166 <section class="examples">
    167 
    168 ## Examples
    169 
    170 <!-- eslint no-undef: "error" -->
    171 
    172 ```javascript
    173 var curry = require( '@stdlib/utils/curry' );
    174 
    175 var fcn;
    176 var out;
    177 var i;
    178 
    179 function add( x, y, z, w, t, s ) {
    180     return x + y + z + w + t + s;
    181 }
    182 
    183 fcn = curry( add );
    184 out = fcn;
    185 for ( i = 0; i < add.length; i++ ) {
    186     out = out( i*10 );
    187 }
    188 console.log( out );
    189 ```
    190 
    191 </section>
    192 
    193 <!-- /.examples -->
    194 
    195 <!-- 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. -->
    196 
    197 <section class="references">
    198 
    199 </section>
    200 
    201 <!-- /.references -->
    202 
    203 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    204 
    205 <section class="links">
    206 
    207 </section>
    208 
    209 <!-- /.links -->