Skip to content

Commit 7a20b0d

Browse files
committed
added exp() method
1 parent 1ab6d48 commit 7a20b0d

File tree

7 files changed

+248
-0
lines changed

7 files changed

+248
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 3.6.0 (November 5th, 2017; methods)
44

55
* added `defaultArrayIfEmpty()` method
6+
* added `exp()` method
67
* added `root()` method
78

89
## 3.5.2 (November 5th, 2017; methods)

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,11 @@ Enumerable.create(11, 22, 33)
626626
Enumerable.create(11, 22, 33)
627627
.cosH(); // complement: arcCosH()
628628

629+
// exp()
630+
// 2.72, 7.39, 20.09
631+
Enumerable.create(1, 2, 3)
632+
.exp();
633+
629634
// floor()
630635
// -1, 23, 444, NaN, -334, NaN
631636
Enumerable.create(-1, 22.47, 444.0, undefined, -333.85, true)

demo/js/enumerable.js

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,15 @@ declare namespace Enumerable {
513513
* @return {IEnumerable<T>} The new sequence.
514514
*/
515515
except(second: Sequence<T>, comparer?: EqualityComparer<T> | true): IEnumerable<T>;
516+
/**
517+
* Handles current items as numbers and calculates e (the base of natural logarithms) raised to each value.
518+
*
519+
* @param {boolean} [handleAsInt] Handle as integer values (true) or floats (false).
520+
* Default: (false)
521+
*
522+
* @return {IEnumerable<number>} The new sequence.
523+
*/
524+
exp(handleAsInt?: boolean): IEnumerable<number>;
516525
/**
517526
* Returns the first element of that sequence.
518527
*
@@ -1217,6 +1226,8 @@ declare namespace Enumerable {
12171226
*/
12181227
protected exceptInner(second: Array<T>, comparer: EqualityComparer<T>): IterableIterator<T>;
12191228
/** @inheritdoc */
1229+
exp(handleAsInt?: boolean): IEnumerable<number>;
1230+
/** @inheritdoc */
12201231
first(predicate?: Predicate<T>): T;
12211232
/** @inheritdoc */
12221233
firstOrDefault<U = symbol>(predicateOrDefaultValue?: Predicate<T> | T, defaultValue?: U): T | U;

index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,15 @@ namespace Enumerable {
559559
*/
560560
except(second: Sequence<T>,
561561
comparer?: EqualityComparer<T> | true): IEnumerable<T>;
562+
/**
563+
* Handles current items as numbers and calculates e (the base of natural logarithms) raised to each value.
564+
*
565+
* @param {boolean} [handleAsInt] Handle as integer values (true) or floats (false).
566+
* Default: (false)
567+
*
568+
* @return {IEnumerable<number>} The new sequence.
569+
*/
570+
exp(handleAsInt?: boolean): IEnumerable<number>;
562571
/**
563572
* Returns the first element of that sequence.
564573
*
@@ -1761,6 +1770,12 @@ namespace Enumerable {
17611770
}
17621771
}
17631772
/** @inheritdoc */
1773+
public exp(handleAsInt?: boolean): IEnumerable<number> {
1774+
return this.select(x => invokeForValidNumber(x,
1775+
y => Math.exp(y),
1776+
handleAsInt));
1777+
}
1778+
/** @inheritdoc */
17641779
public first(predicate?: Predicate<T>): T {
17651780
predicate = toPredicateSafe(predicate);
17661781

js/enumerable.js

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/IEnumerable/exp.ts

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
// The MIT License (MIT)
2+
//
3+
// node-enumerable (https://github.com/mkloubert/node-enumerable)
4+
// Copyright (c) Marcel Joachim Kloubert <marcel.kloubert@gmx.net>
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to
8+
// deal in the Software without restriction, including without limitation the
9+
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10+
// sell copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22+
// DEALINGS IN THE SOFTWARE.
23+
24+
import Assert = require('assert');
25+
import Enumerable = require('../../');
26+
import Helpers = require('../helpers');
27+
28+
const MAX_ARRAY_SIZE = 100;
29+
30+
Helpers.execute(
31+
'Testing numbers (floats)...',
32+
(ctx) => {
33+
for (let i = 0; i <= MAX_ARRAY_SIZE; i++) {
34+
const ARR: number[] = [];
35+
const EXPECTED: number[] = [];
36+
for (let j = 0; j < i; j++) {
37+
ARR.push( j );
38+
EXPECTED.push( Math.exp(j) );
39+
}
40+
41+
const SEQ = Enumerable.from(ARR)
42+
.exp();
43+
44+
const ACTUAL: number[] = [];
45+
for (let item of SEQ) {
46+
ACTUAL.push(item);
47+
}
48+
49+
Assert.equal( ACTUAL.length, EXPECTED.length );
50+
Assert.strictEqual( ACTUAL.length, EXPECTED.length );
51+
Assert.equal( '' + ACTUAL.length, EXPECTED.length );
52+
Assert.equal( ACTUAL.length, '' + EXPECTED.length );
53+
Assert.equal( '' + ACTUAL.length, '' + EXPECTED.length );
54+
Assert.strictEqual( '' + ACTUAL.length, '' + EXPECTED.length );
55+
56+
for (let k = 0; k < EXPECTED.length; k++) {
57+
const A = ACTUAL[k];
58+
const E = EXPECTED[k];
59+
60+
Assert.equal( A, E );
61+
Assert.strictEqual( A, E );
62+
Assert.equal( '' + A, E );
63+
Assert.equal( A, '' + E );
64+
Assert.equal( '' + A, '' + E );
65+
Assert.strictEqual( '' + A, '' + E );
66+
}
67+
}
68+
});
69+
70+
Helpers.execute(
71+
'Testing strings (floats)...',
72+
(ctx) => {
73+
for (let i = 0; i <= MAX_ARRAY_SIZE; i++) {
74+
const ARR: string[] = [];
75+
const EXPECTED: number[] = [];
76+
for (let j = 0; j < i; j++) {
77+
ARR.push( '' + j );
78+
EXPECTED.push( Math.exp(j) );
79+
}
80+
81+
const SEQ = Enumerable.from(ARR)
82+
.exp();
83+
84+
const ACTUAL: number[] = [];
85+
for (let item of SEQ) {
86+
ACTUAL.push(item);
87+
}
88+
89+
Assert.equal( ACTUAL.length, EXPECTED.length );
90+
Assert.strictEqual( ACTUAL.length, EXPECTED.length );
91+
Assert.equal( '' + ACTUAL.length, EXPECTED.length );
92+
Assert.equal( ACTUAL.length, '' + EXPECTED.length );
93+
Assert.equal( '' + ACTUAL.length, '' + EXPECTED.length );
94+
Assert.strictEqual( '' + ACTUAL.length, '' + EXPECTED.length );
95+
96+
for (let k = 0; k < EXPECTED.length; k++) {
97+
const A = ACTUAL[k];
98+
const E = EXPECTED[k];
99+
100+
Assert.equal( A, E );
101+
Assert.strictEqual( A, E );
102+
Assert.equal( '' + A, E );
103+
Assert.equal( A, '' + E );
104+
Assert.equal( '' + A, '' + E );
105+
Assert.strictEqual( '' + A, '' + E );
106+
}
107+
}
108+
});
109+
110+
Helpers.execute(
111+
'Testing numbers (ints)...',
112+
(ctx) => {
113+
for (let i = 0; i <= MAX_ARRAY_SIZE; i++) {
114+
const ARR: number[] = [];
115+
const EXPECTED: number[] = [];
116+
for (let j = 0; j < i; j++) {
117+
ARR.push( j );
118+
EXPECTED.push( Math.exp(j) );
119+
}
120+
121+
const SEQ = Enumerable.from(ARR)
122+
.exp(true);
123+
124+
const ACTUAL: number[] = [];
125+
for (let item of SEQ) {
126+
ACTUAL.push(item);
127+
}
128+
129+
Assert.equal( ACTUAL.length, EXPECTED.length );
130+
Assert.strictEqual( ACTUAL.length, EXPECTED.length );
131+
Assert.equal( '' + ACTUAL.length, EXPECTED.length );
132+
Assert.equal( ACTUAL.length, '' + EXPECTED.length );
133+
Assert.equal( '' + ACTUAL.length, '' + EXPECTED.length );
134+
Assert.strictEqual( '' + ACTUAL.length, '' + EXPECTED.length );
135+
136+
for (let k = 0; k < EXPECTED.length; k++) {
137+
const A = ACTUAL[k];
138+
const E = EXPECTED[k];
139+
140+
Assert.equal( A, E );
141+
Assert.strictEqual( A, E );
142+
Assert.equal( '' + A, E );
143+
Assert.equal( A, '' + E );
144+
Assert.equal( '' + A, '' + E );
145+
Assert.strictEqual( '' + A, '' + E );
146+
}
147+
}
148+
});
149+
150+
Helpers.execute(
151+
'Testing strings (ints)...',
152+
(ctx) => {
153+
for (let i = 0; i <= MAX_ARRAY_SIZE; i++) {
154+
const ARR: string[] = [];
155+
const EXPECTED: number[] = [];
156+
for (let j = 0; j < i; j++) {
157+
ARR.push( '' + j );
158+
EXPECTED.push( Math.exp(j) );
159+
}
160+
161+
const SEQ = Enumerable.from(ARR)
162+
.exp(true);
163+
164+
const ACTUAL: number[] = [];
165+
for (let item of SEQ) {
166+
ACTUAL.push(item);
167+
}
168+
169+
Assert.equal( ACTUAL.length, EXPECTED.length );
170+
Assert.strictEqual( ACTUAL.length, EXPECTED.length );
171+
Assert.equal( '' + ACTUAL.length, EXPECTED.length );
172+
Assert.equal( ACTUAL.length, '' + EXPECTED.length );
173+
Assert.equal( '' + ACTUAL.length, '' + EXPECTED.length );
174+
Assert.strictEqual( '' + ACTUAL.length, '' + EXPECTED.length );
175+
176+
for (let k = 0; k < EXPECTED.length; k++) {
177+
const A = ACTUAL[k];
178+
const E = EXPECTED[k];
179+
180+
Assert.equal( A, E );
181+
Assert.strictEqual( A, E );
182+
Assert.equal( '' + A, E );
183+
Assert.equal( A, '' + E );
184+
Assert.equal( '' + A, '' + E );
185+
Assert.strictEqual( '' + A, '' + E );
186+
}
187+
}
188+
});

0 commit comments

Comments
 (0)