Skip to content

Commit

Permalink
Add support for flatmap
Browse files Browse the repository at this point in the history
  • Loading branch information
davecoates committed Jul 27, 2016
1 parent 3ce7f0e commit 6310540
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,25 @@ class TypedList extends BaseTypeInferedList {
}
}
}
flatMap(mapper, context) {
if (this.size === 0) {
return this
} else {
const result = TypeInferedList.from(this).flatMap(mapper, context)
if (this[$store] === result[$store]) {
return this
}
if (result[$type] === this[$type]) {
const list = construct(this)
list[$store] = result[$store]
list.size = result.size
return list
} else {
return result
}
}
}

}

export const List = function(descriptor, label) {
Expand Down
19 changes: 19 additions & 0 deletions src/test/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -985,3 +985,22 @@ test('empty list optimization', assert => {
assert.equal(Points([Point({x: 1})]).clear(),
Points([Point({x: 1}), Point({y: 2})]).clear())
})

test('flatMap', assert => {
var numbers = NumberList.of(97, 98, 99);
var letters = numbers.flatMap(v => Immutable.fromJS([
String.fromCharCode(v),
String.fromCharCode(v).toUpperCase(),
]))

assert.deepEqual(letters.toArray(), ['a','A','b','B','c','C'])

var letters = numbers.flatMap(v => [
String.fromCharCode(v),
String.fromCharCode(v).toUpperCase(),
])

assert.deepEqual(letters.toArray(), ['a','A','b','B','c','C'])

})

0 comments on commit 6310540

Please sign in to comment.