Typed Array Function
Applies a function to each typed array element.
Installation
$ npm install compute-typed-array-function
For use in the browser, use browserify.
Usage
var arrayfun = ;
arrayfun( fcn, ...array[, options] )
Applies a function
to each typed array
element.
var arr = 12345 ;{return val + 5;}var out = ;// returns Float64Array( [6,7,8,9,10] )
The function accepts the following options
:
- dtype: output data type. Default:
float64
. - out:
boolean
indicating whether an outputtyped array
has been provided. Default:false
.
By default, the output typed array
data type is float64
in order to preserve precision. To specify a different data type, set the dtype
option (see compute-array-constructors
for a list of acceptable data types).
var out =;// return Int8Array( [6,7,8,9,10] )
By default, the function
returns a new typed array
. To mutate a typed array
(e.g., when input values can be discarded or when optimizing memory usage), set the out
option to true
to indicate that an output typed array
has been provided as the first typed array
argument.
var out = Uint8Array 5 ;;// returns Uint8Array( [6,7,8,9,10] )// Works with generic arrays, as well...out = 0 0 0 0 0 ;;// returns [ 6, 7, 8, 9, 10 ]
===
Factory
The main exported function
does not make any assumptions regarding the number of input typed arrays
. To create a reusable typed array
function where the number of input typed arrays
is known, a factory method is provided.
arrayfun.factory( [fcn,] num[, options] )
Creates an apply function
to apply a function
to each typed array
element.
var afun = arrayfun;{return x + y;}var arr1 = 5arr2 = 5 ;for var i = 0; i < 5; i++arr1 i = 5;arr2 i = i + 5;// arr1 = Int16Array( [5,5,5,5,5] )// arr2 = Uint32Array( [5,6,7,8,9] )var out = ;// returns Float64Array( [10,11,12,13,14] )
An apply function
may be provided during function
creation.
var aadd = arrayfun;var out = ;// returns Float64Array( [10,11,12,13,14] )
The function accepts the following options
:
- dtype: output data type. Default:
float64
.
By default, the output typed array
data type is float64
. To specify a different data type, set the dtype
option.
var aadd = arrayfun;var out = ;// returns Int32Array( [10,11,12,13,14] )// ...and for all subsequent calls...out = ;// returns Int32Array( [10,11,12,13,14] )
Note: a factory function
always returns a new typed array
.
===
Create
To facilitate using typed array
functions within an application where input arguments are of known types and where memory management occurs externally, a method to create minimal typed array
functions is provided.
arrayfun.create( [fcn,] num )
Creates an apply function
to apply a function
to each typed array
element, where num
is the number of input typed arrays
excluding the output typed array
.
var afcn = arrayfunout = 5 ;out = ;// returns [ 10, 11, 12, 13, 14 ]{return x - y;}out = ;// returns [ 0, -1, -2, -3, -4 ]
An apply function
may be provided during function
creation.
var aadd = arrayfun;var out = ;// returns [ 10, 11, 12, 13, 14 ]
===
Raw
Lower-level APIs are provided which forgo some of the guarantees of the above APIs, such as input argument validation. While use of the above APIs is encouraged in REPL environments, use of the lower-level interfaces may be warranted when arguments are of a known type or when performance is paramount.
arrayfun.raw( fcn, ...array[, options] )
Applies a function
to each typed array
element.
var arr = 5 ;var out = arrayfun;// returns Float64Array( [5,5,5,5,5] )
The function accepts the same options
as the main exported function.
arrayfun.rawFactory( [fcn,] num[, options] )
Creates an apply function
to apply a function
to each typed array
element.
var afun = arrayfun;var out = ;// returns Float64Array( [10,11,12,13,14] )
The function accepts the same options
as arrayfun.factory()
.
Notes
- Both factory methods, as well as the
.create()
method, use dynamic code evaluation. Beware when using these methods in the browser as they may violate your content security policy (CSP).
Examples
var arrayfun = ;var arr1arr2outi;arr1 = 25 ;for i = 0; i < arr1length; i++arr1 i = i;arr2 = 25 ;for i = 0; i < arr2length; i++arr2 i = 5;{return x + y;}out = ;console;
To run the example code from the top-level application directory,
$ node ./examples/index.js
Tests
Unit
Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
Test Coverage
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
License
Copyright
Copyright © 2015. The Compute.io Authors.