-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flatmap
- Loading branch information
Showing
44 changed files
with
392 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { flatMap } from '../../../../src/inline-loops.macro'; | ||
|
||
const flattened = flatMap(array, fn); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let _result = []; | ||
|
||
for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { | ||
_value = array[_key]; | ||
|
||
_result.push.apply(_result, fn(_value, _key, array)); | ||
} | ||
|
||
const flattened = _result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { flatMapRight } from '../../../../src/inline-loops.macro'; | ||
|
||
const flattened = flatMapRight(array, fn); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let _result = []; | ||
|
||
for (let _key = array.length - 1, _value; _key >= 0; --_key) { | ||
_value = array[_key]; | ||
|
||
_result.push.apply(_result, fn(_value, _key, array)); | ||
} | ||
|
||
const flattened = _result; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { forEach } from '../../../../src/inline-loops.macro'; | ||
|
||
forEach([], ([a, b]) => { | ||
console.log(a, b); | ||
}); |
11 changes: 11 additions & 0 deletions
11
__tests__/__fixtures__/complex/destructured-params/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const _iterable = []; | ||
|
||
const _fn = ([a, b]) => { | ||
console.log(a, b); | ||
}; | ||
|
||
for (let _key = 0, _length = _iterable.length, _value; _key < _length; ++_key) { | ||
_value = _iterable[_key]; | ||
|
||
_fn(_value, _key, _iterable); | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions
3
__tests__/__fixtures__/inlined-arrow-expression/flatMap/code.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { flatMap } from '../../../../src/inline-loops.macro'; | ||
|
||
const flattened = flatMap(array, entry => [entry[0]]); |
9 changes: 9 additions & 0 deletions
9
__tests__/__fixtures__/inlined-arrow-expression/flatMap/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let _result = []; | ||
|
||
for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { | ||
_value = array[_key]; | ||
|
||
_result.push.apply(_result, [_value[0]]); | ||
} | ||
|
||
const flattened = _result; |
3 changes: 3 additions & 0 deletions
3
__tests__/__fixtures__/inlined-arrow-expression/flatMapRight/code.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { flatMapRight } from '../../../../src/inline-loops.macro'; | ||
|
||
const flattened = flatMapRight(array, entry => [entry[0]]); |
9 changes: 9 additions & 0 deletions
9
__tests__/__fixtures__/inlined-arrow-expression/flatMapRight/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let _result = []; | ||
|
||
for (let _key = array.length - 1, _value; _key >= 0; --_key) { | ||
_value = array[_key]; | ||
|
||
_result.push.apply(_result, [_value[0]]); | ||
} | ||
|
||
const flattened = _result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { flatMap } from '../../../../src/inline-loops.macro'; | ||
|
||
const flattened = flatMap(array, entry => { | ||
return [entry[0]]; | ||
}); |
9 changes: 9 additions & 0 deletions
9
__tests__/__fixtures__/inlined-arrow-return/flatMap/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let _result = []; | ||
|
||
for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { | ||
_value = array[_key]; | ||
|
||
_result.push.apply(_result, [_value[0]]); | ||
} | ||
|
||
const flattened = _result; |
5 changes: 5 additions & 0 deletions
5
__tests__/__fixtures__/inlined-arrow-return/flatMapRight/code.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { flatMapRight } from '../../../../src/inline-loops.macro'; | ||
|
||
const flattened = flatMapRight(array, entry => { | ||
return [entry[0]]; | ||
}); |
9 changes: 9 additions & 0 deletions
9
__tests__/__fixtures__/inlined-arrow-return/flatMapRight/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let _result = []; | ||
|
||
for (let _key = array.length - 1, _value; _key >= 0; --_key) { | ||
_value = array[_key]; | ||
|
||
_result.push.apply(_result, [_value[0]]); | ||
} | ||
|
||
const flattened = _result; |
5 changes: 5 additions & 0 deletions
5
__tests__/__fixtures__/inlined-function-return/flatMap/code.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { flatMap } from '../../../../src/inline-loops.macro'; | ||
|
||
const flattened = flatMap(array, function(entry) { | ||
return [entry[0]]; | ||
}); |
9 changes: 9 additions & 0 deletions
9
__tests__/__fixtures__/inlined-function-return/flatMap/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let _result = []; | ||
|
||
for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { | ||
_value = array[_key]; | ||
|
||
_result.push.apply(_result, [_value[0]]); | ||
} | ||
|
||
const flattened = _result; |
5 changes: 5 additions & 0 deletions
5
__tests__/__fixtures__/inlined-function-return/flatMapRight/code.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { flatMapRight } from '../../../../src/inline-loops.macro'; | ||
|
||
const flattened = flatMapRight(array, function(entry) { | ||
return [entry[0]]; | ||
}); |
9 changes: 9 additions & 0 deletions
9
__tests__/__fixtures__/inlined-function-return/flatMapRight/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let _result = []; | ||
|
||
for (let _key = array.length - 1, _value; _key >= 0; --_key) { | ||
_value = array[_key]; | ||
|
||
_result.push.apply(_result, [_value[0]]); | ||
} | ||
|
||
const flattened = _result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { flatMap } from '../../../../src/inline-loops.macro'; | ||
|
||
const flattened = flatMap([['foo', 'bar'], ['bar', 'baz']], (entry) => { | ||
const [first] = entry; | ||
|
||
return [first]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const _iterable = [['foo', 'bar'], ['bar', 'baz']]; | ||
|
||
const _fn = entry => { | ||
const [first] = entry; | ||
return [first]; | ||
}; | ||
|
||
let _result = []; | ||
|
||
for (let _key = 0, _length = _iterable.length, _value; _key < _length; ++_key) { | ||
_value = _iterable[_key]; | ||
|
||
_result.push.apply(_result, _fn(_value, _key, _iterable)); | ||
} | ||
|
||
const flattened = _result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { flatMapRight } from '../../../../src/inline-loops.macro'; | ||
|
||
const flattened = flatMapRight([['foo', 'bar'], ['bar', 'baz']], (entry) => { | ||
const [first] = entry; | ||
|
||
return [first]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const _iterable = [['foo', 'bar'], ['bar', 'baz']]; | ||
|
||
const _fn = entry => { | ||
const [first] = entry; | ||
return [first]; | ||
}; | ||
|
||
let _result = []; | ||
|
||
for (let _key = _iterable.length - 1, _value; _key >= 0; --_key) { | ||
_value = _iterable[_key]; | ||
|
||
_result.push.apply(_result, _fn(_value, _key, _iterable)); | ||
} | ||
|
||
const flattened = _result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* eslint-disable */ | ||
|
||
const { flatMap, flatMapRight } = require('../../src/inline-loops.macro'); | ||
|
||
const { deepEqual: isEqual } = require('fast-equals'); | ||
|
||
const ARRAY = [[1], [2], [3], [4], [5], [6]]; | ||
|
||
const withTriples = value => [value, value * 2, value * 3]; | ||
|
||
const BAD_ARRAY_RESULT = [...ARRAY]; | ||
const ARRAY_RESULT = ARRAY.reduce((accumulator, value) => { | ||
const result = withTriples(value); | ||
|
||
accumulator.push(...result); | ||
|
||
return accumulator; | ||
}, []); | ||
|
||
const BAD_DECREMENTING_ARRAY_RESULT = [...ARRAY].reverse(); | ||
const DECREMENTING_ARRAY_RESULT = ARRAY.reduceRight((accumulator, value) => { | ||
const result = withTriples(value); | ||
|
||
accumulator.push(...result); | ||
|
||
return accumulator; | ||
}, []); | ||
|
||
module.exports = { | ||
cached: { | ||
decrementing: { | ||
false: isEqual(flatMapRight(ARRAY, withTriples), BAD_DECREMENTING_ARRAY_RESULT), | ||
true: isEqual(flatMapRight(ARRAY, withTriples), DECREMENTING_ARRAY_RESULT), | ||
}, | ||
standard: { | ||
false: isEqual(flatMap(ARRAY, withTriples), BAD_ARRAY_RESULT), | ||
true: isEqual(flatMap(ARRAY, withTriples), ARRAY_RESULT), | ||
}, | ||
}, | ||
inlinedArrowExpression: { | ||
decrementing: { | ||
false: isEqual( | ||
flatMapRight(ARRAY, value => [value, value * 2, value * 3]), | ||
BAD_DECREMENTING_ARRAY_RESULT, | ||
), | ||
true: isEqual( | ||
flatMapRight(ARRAY, value => [value, value * 2, value * 3]), | ||
DECREMENTING_ARRAY_RESULT, | ||
), | ||
}, | ||
standard: { | ||
false: isEqual(flatMap(ARRAY, value => [value, value * 2, value * 3]), BAD_ARRAY_RESULT), | ||
true: isEqual(flatMap(ARRAY, value => [value, value * 2, value * 3]), ARRAY_RESULT), | ||
}, | ||
}, | ||
inlinedArrowReturn: { | ||
decrementing: { | ||
false: isEqual( | ||
flatMapRight(ARRAY, value => { | ||
return [value, value * 2, value * 3]; | ||
}), | ||
BAD_DECREMENTING_ARRAY_RESULT, | ||
), | ||
true: isEqual( | ||
flatMapRight(ARRAY, value => { | ||
return [value, value * 2, value * 3]; | ||
}), | ||
DECREMENTING_ARRAY_RESULT, | ||
), | ||
}, | ||
standard: { | ||
false: isEqual( | ||
flatMap(ARRAY, value => { | ||
return [value, value * 2, value * 3]; | ||
}), | ||
BAD_ARRAY_RESULT, | ||
), | ||
true: isEqual( | ||
flatMap(ARRAY, value => { | ||
return [value, value * 2, value * 3]; | ||
}), | ||
ARRAY_RESULT, | ||
), | ||
}, | ||
}, | ||
inlinedFunctionReturn: { | ||
decrementing: { | ||
false: isEqual( | ||
flatMapRight(ARRAY, function(value) { | ||
return [value, value * 2, value * 3]; | ||
}), | ||
BAD_DECREMENTING_ARRAY_RESULT, | ||
), | ||
true: isEqual( | ||
flatMapRight(ARRAY, function(value) { | ||
return [value, value * 2, value * 3]; | ||
}), | ||
DECREMENTING_ARRAY_RESULT, | ||
), | ||
}, | ||
standard: { | ||
false: isEqual( | ||
flatMap(ARRAY, function(value) { | ||
return [value, value * 2, value * 3]; | ||
}), | ||
BAD_ARRAY_RESULT, | ||
), | ||
true: isEqual( | ||
flatMap(ARRAY, function(value) { | ||
return [value, value * 2, value * 3]; | ||
}), | ||
ARRAY_RESULT, | ||
), | ||
}, | ||
}, | ||
uncached: { | ||
decrementing: { | ||
false: isEqual( | ||
flatMapRight([].concat(ARRAY), value => { | ||
const withTriples = [value, value * 2, value * 3]; | ||
|
||
return withTriples; | ||
}), | ||
BAD_DECREMENTING_ARRAY_RESULT, | ||
), | ||
true: isEqual( | ||
flatMapRight([].concat(ARRAY), value => { | ||
const withTriples = [value, value * 2, value * 3]; | ||
|
||
return withTriples; | ||
}), | ||
DECREMENTING_ARRAY_RESULT, | ||
), | ||
}, | ||
standard: { | ||
false: isEqual( | ||
flatMap([].concat(ARRAY), value => { | ||
const withTriples = [value, value * 2, value * 3]; | ||
|
||
return withTriples; | ||
}), | ||
BAD_ARRAY_RESULT, | ||
), | ||
true: isEqual( | ||
flatMap([].concat(ARRAY), value => { | ||
const withTriples = [value, value * 2, value * 3]; | ||
|
||
return withTriples; | ||
}), | ||
ARRAY_RESULT, | ||
), | ||
}, | ||
}, | ||
}; |
Oops, something went wrong.