Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for flatmap #15

Merged
merged 1 commit into from
Jul 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Add support for flatmap
  • Loading branch information
davecoates committed Jul 27, 2016
commit 6310540deae1a5e05d46767a35d8ec32865f2c33
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'])

})