README.md (2115B)
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 # Constant Function 22 23 > Constant function. 24 25 <section class="intro"> 26 27 A [constant function][constant-function] is a `function` whose output value is the same for every input value. 28 29 </section> 30 31 <!-- /.intro --> 32 33 <section class="usage"> 34 35 ## Usage 36 37 ```javascript 38 var constantFunction = require( '@stdlib/utils/constant-function' ); 39 ``` 40 41 #### constantFunction( x ) 42 43 Returns a [constant function][constant-function] which always returns `x`. 44 45 ```javascript 46 var fcn = constantFunction( 'beep' ); 47 // returns <Function> 48 49 var v = fcn(); 50 // returns 'beep' 51 52 v = fcn(); 53 // returns 'beep' 54 ``` 55 56 </section> 57 58 <!-- /.usage --> 59 60 <section class="notes"> 61 62 ## Notes 63 64 - When provided an object reference, the returned `function` always returns the same reference. 65 66 ```javascript 67 var obj = {}; 68 var fcn = constantFunction( obj ); 69 70 var bool = ( fcn() === obj ); 71 // returns true 72 ``` 73 74 </section> 75 76 <!-- /.notes --> 77 78 <section class="examples"> 79 80 ## Examples 81 82 <!-- eslint no-undef: "error" --> 83 84 ```javascript 85 var constantFunction = require( '@stdlib/utils/constant-function' ); 86 87 var bool; 88 var fcn; 89 var arr; 90 var v; 91 var i; 92 93 fcn = constantFunction( 3.14 ); 94 for ( i = 0; i < 10; i++ ) { 95 v = fcn(); 96 // returns 3.14 97 } 98 99 arr = [ 1, 2, 3 ]; 100 fcn = constantFunction( arr ); 101 for ( i = 0; i < 10; i++ ) { 102 v = fcn(); 103 bool = ( v === arr ); 104 // returns true 105 } 106 ``` 107 108 </section> 109 110 <!-- /.examples --> 111 112 <section class="links"> 113 114 [constant-function]: https://en.wikipedia.org/wiki/Constant_function 115 116 </section> 117 118 <!-- /.links -->