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

Shorthand for filter map #765

Open
eguneys opened this issue Jun 17, 2020 · 4 comments
Open

Shorthand for filter map #765

eguneys opened this issue Jun 17, 2020 · 4 comments

Comments

@eguneys
Copy link

eguneys commented Jun 17, 2020

I have this everywhere in code:


eventStream
  .filter(_ => _.props)
  .map(_ => _.props);

I hope there is a shorthand for that.

@semmel
Copy link
Member

semmel commented Jun 18, 2020

Not a shorthand but shorter.

stream
.map(_ => _.props)
.filter(Boolean)

I rarely encounter this scenario.

@semmel
Copy link
Member

semmel commented Jun 18, 2020

Not being a huge fan of Bacon's method chaining API, I prefer ramda-style auto-curried, data-last functions. And "chaining" them by function composition. This way it is easy to compose your own utility functions without the prototype hassle you would have if you would to extend Bacons native API.

import { curry, pipe } from "ramda"; 
// ramda-style Bacon API
const flatMap = curry((fn, observable) => observable.flatMap(fn));
// use
pipe(
   flatMap(_ => _.props ? Bacon.once(_.props) : Bacon.never())
)(stream);

// or make a utility function
const filtermap = key => 
      flatMap(evt => evt[key] ? Bacon.once(evt[key]) : Bacon.never());
// use
pipe(
   filtermap('props')
)(stream);

@raimohanska
Copy link
Contributor

I've been thinking about moving Bacon from the current "object-oriented" API to data-last functions that you can import separately, like RxJs has been packaged for quite a while already. Would have some benefits including treeshaking and easier extendability (typescript practically destroyed the earlier way of just adding stuff to EventStream prototype).

@steve-taylor
Copy link

Previously (before v3), you could do this in Bacon.js:

.filter('.path.to.some.property')

You can use Lodash to get something similar:

import property from 'lodash/property';
.filter(property('path.to.some.property'))

But I wouldn't do this anymore. Best to allow the types to flow and let your TypeScript-powered IDE find potential errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants