Skip to content

Commit

Permalink
added exp() method
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Nov 5, 2017
1 parent 1ab6d48 commit 7a20b0d
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 3.6.0 (November 5th, 2017; methods)

* added `defaultArrayIfEmpty()` method
* added `exp()` method
* added `root()` method

## 3.5.2 (November 5th, 2017; methods)
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,11 @@ Enumerable.create(11, 22, 33)
Enumerable.create(11, 22, 33)
.cosH(); // complement: arcCosH()

// exp()
// 2.72, 7.39, 20.09
Enumerable.create(1, 2, 3)
.exp();

// floor()
// -1, 23, 444, NaN, -334, NaN
Enumerable.create(-1, 22.47, 444.0, undefined, -333.85, true)
Expand Down
14 changes: 14 additions & 0 deletions demo/js/enumerable.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,15 @@ declare namespace Enumerable {
* @return {IEnumerable<T>} The new sequence.
*/
except(second: Sequence<T>, comparer?: EqualityComparer<T> | true): IEnumerable<T>;
/**
* Handles current items as numbers and calculates e (the base of natural logarithms) raised to each value.
*
* @param {boolean} [handleAsInt] Handle as integer values (true) or floats (false).
* Default: (false)
*
* @return {IEnumerable<number>} The new sequence.
*/
exp(handleAsInt?: boolean): IEnumerable<number>;
/**
* Returns the first element of that sequence.
*
Expand Down Expand Up @@ -1217,6 +1226,8 @@ declare namespace Enumerable {
*/
protected exceptInner(second: Array<T>, comparer: EqualityComparer<T>): IterableIterator<T>;
/** @inheritdoc */
exp(handleAsInt?: boolean): IEnumerable<number>;
/** @inheritdoc */
first(predicate?: Predicate<T>): T;
/** @inheritdoc */
firstOrDefault<U = symbol>(predicateOrDefaultValue?: Predicate<T> | T, defaultValue?: U): T | U;
Expand Down
15 changes: 15 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,15 @@ namespace Enumerable {
*/
except(second: Sequence<T>,
comparer?: EqualityComparer<T> | true): IEnumerable<T>;
/**
* Handles current items as numbers and calculates e (the base of natural logarithms) raised to each value.
*
* @param {boolean} [handleAsInt] Handle as integer values (true) or floats (false).
* Default: (false)
*
* @return {IEnumerable<number>} The new sequence.
*/
exp(handleAsInt?: boolean): IEnumerable<number>;
/**
* Returns the first element of that sequence.
*
Expand Down Expand Up @@ -1761,6 +1770,12 @@ namespace Enumerable {
}
}
/** @inheritdoc */
public exp(handleAsInt?: boolean): IEnumerable<number> {
return this.select(x => invokeForValidNumber(x,
y => Math.exp(y),
handleAsInt));
}
/** @inheritdoc */
public first(predicate?: Predicate<T>): T {
predicate = toPredicateSafe(predicate);

Expand Down
14 changes: 14 additions & 0 deletions js/enumerable.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

188 changes: 188 additions & 0 deletions test/IEnumerable/exp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// The MIT License (MIT)
//
// node-enumerable (https://github.com/mkloubert/node-enumerable)
// Copyright (c) Marcel Joachim Kloubert <marcel.kloubert@gmx.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

import Assert = require('assert');
import Enumerable = require('../../');
import Helpers = require('../helpers');

const MAX_ARRAY_SIZE = 100;

Helpers.execute(
'Testing numbers (floats)...',
(ctx) => {
for (let i = 0; i <= MAX_ARRAY_SIZE; i++) {
const ARR: number[] = [];
const EXPECTED: number[] = [];
for (let j = 0; j < i; j++) {
ARR.push( j );
EXPECTED.push( Math.exp(j) );
}

const SEQ = Enumerable.from(ARR)
.exp();

const ACTUAL: number[] = [];
for (let item of SEQ) {
ACTUAL.push(item);
}

Assert.equal( ACTUAL.length, EXPECTED.length );
Assert.strictEqual( ACTUAL.length, EXPECTED.length );
Assert.equal( '' + ACTUAL.length, EXPECTED.length );
Assert.equal( ACTUAL.length, '' + EXPECTED.length );
Assert.equal( '' + ACTUAL.length, '' + EXPECTED.length );
Assert.strictEqual( '' + ACTUAL.length, '' + EXPECTED.length );

for (let k = 0; k < EXPECTED.length; k++) {
const A = ACTUAL[k];
const E = EXPECTED[k];

Assert.equal( A, E );
Assert.strictEqual( A, E );
Assert.equal( '' + A, E );
Assert.equal( A, '' + E );
Assert.equal( '' + A, '' + E );
Assert.strictEqual( '' + A, '' + E );
}
}
});

Helpers.execute(
'Testing strings (floats)...',
(ctx) => {
for (let i = 0; i <= MAX_ARRAY_SIZE; i++) {
const ARR: string[] = [];
const EXPECTED: number[] = [];
for (let j = 0; j < i; j++) {
ARR.push( '' + j );
EXPECTED.push( Math.exp(j) );
}

const SEQ = Enumerable.from(ARR)
.exp();

const ACTUAL: number[] = [];
for (let item of SEQ) {
ACTUAL.push(item);
}

Assert.equal( ACTUAL.length, EXPECTED.length );
Assert.strictEqual( ACTUAL.length, EXPECTED.length );
Assert.equal( '' + ACTUAL.length, EXPECTED.length );
Assert.equal( ACTUAL.length, '' + EXPECTED.length );
Assert.equal( '' + ACTUAL.length, '' + EXPECTED.length );
Assert.strictEqual( '' + ACTUAL.length, '' + EXPECTED.length );

for (let k = 0; k < EXPECTED.length; k++) {
const A = ACTUAL[k];
const E = EXPECTED[k];

Assert.equal( A, E );
Assert.strictEqual( A, E );
Assert.equal( '' + A, E );
Assert.equal( A, '' + E );
Assert.equal( '' + A, '' + E );
Assert.strictEqual( '' + A, '' + E );
}
}
});

Helpers.execute(
'Testing numbers (ints)...',
(ctx) => {
for (let i = 0; i <= MAX_ARRAY_SIZE; i++) {
const ARR: number[] = [];
const EXPECTED: number[] = [];
for (let j = 0; j < i; j++) {
ARR.push( j );
EXPECTED.push( Math.exp(j) );
}

const SEQ = Enumerable.from(ARR)
.exp(true);

const ACTUAL: number[] = [];
for (let item of SEQ) {
ACTUAL.push(item);
}

Assert.equal( ACTUAL.length, EXPECTED.length );
Assert.strictEqual( ACTUAL.length, EXPECTED.length );
Assert.equal( '' + ACTUAL.length, EXPECTED.length );
Assert.equal( ACTUAL.length, '' + EXPECTED.length );
Assert.equal( '' + ACTUAL.length, '' + EXPECTED.length );
Assert.strictEqual( '' + ACTUAL.length, '' + EXPECTED.length );

for (let k = 0; k < EXPECTED.length; k++) {
const A = ACTUAL[k];
const E = EXPECTED[k];

Assert.equal( A, E );
Assert.strictEqual( A, E );
Assert.equal( '' + A, E );
Assert.equal( A, '' + E );
Assert.equal( '' + A, '' + E );
Assert.strictEqual( '' + A, '' + E );
}
}
});

Helpers.execute(
'Testing strings (ints)...',
(ctx) => {
for (let i = 0; i <= MAX_ARRAY_SIZE; i++) {
const ARR: string[] = [];
const EXPECTED: number[] = [];
for (let j = 0; j < i; j++) {
ARR.push( '' + j );
EXPECTED.push( Math.exp(j) );
}

const SEQ = Enumerable.from(ARR)
.exp(true);

const ACTUAL: number[] = [];
for (let item of SEQ) {
ACTUAL.push(item);
}

Assert.equal( ACTUAL.length, EXPECTED.length );
Assert.strictEqual( ACTUAL.length, EXPECTED.length );
Assert.equal( '' + ACTUAL.length, EXPECTED.length );
Assert.equal( ACTUAL.length, '' + EXPECTED.length );
Assert.equal( '' + ACTUAL.length, '' + EXPECTED.length );
Assert.strictEqual( '' + ACTUAL.length, '' + EXPECTED.length );

for (let k = 0; k < EXPECTED.length; k++) {
const A = ACTUAL[k];
const E = EXPECTED[k];

Assert.equal( A, E );
Assert.strictEqual( A, E );
Assert.equal( '' + A, E );
Assert.equal( A, '' + E );
Assert.equal( '' + A, '' + E );
Assert.strictEqual( '' + A, '' + E );
}
}
});

0 comments on commit 7a20b0d

Please sign in to comment.