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

Handle stream like maybe monad #2930

Closed
ghost opened this issue Oct 9, 2017 · 2 comments
Closed

Handle stream like maybe monad #2930

ghost opened this issue Oct 9, 2017 · 2 comments

Comments

@ghost
Copy link

ghost commented Oct 9, 2017

Hi all

Consider following stream:

     const oEmailValid$ = oEmailInput$
          .map(_catchInputCtrlValue)
          .map(_isStringNotEmpty) 
          .map(...)
          .map(...)
          .map(...)
          .map(...)
          .subscribe((predicate) => console.log(predicate))

Suppose _isStringNotEmpty returns false, so I wan't to continue the stream but still want to receive on the subscribe function the returned value of _isStringNotEmpty, in this case is false.

How to get it?

To clarify, what I mean, consider the following Haskell code:

(Just 3) >>= (\_ -> Just 4) >>= (\_ -> Just 7) >>= (\_ -> Nothing) >>= (\_ -> Just 10)

As the result I've got Nothing, because the 4th computation returns Nothing.

Thanks

@benlesh
Copy link
Member

benlesh commented Oct 11, 2017

@kostonstyle ... I believe you're looking for mergeMap rather than map above, and you'd have each of those functions return an Observable, rather than a plain value.

@BioPhoton
Copy link
Contributor

Hi @ghost!

Here you ave a working example for your problem. If you need any further help just comment.
If your problem is solved please close this issue

const _isValidName = (arr) =>  (v) => ({validName: arr.includes(v)});
const _isStringNotEmpty = (v) => ({StringNotEmpty: v !== ''});

fromEvent(document.getElementById('input'), 'input')
  .pipe(
    map(e => e.target.value),
    switchMap(v => 
      merge(
        of(v).pipe(map(_isValidName(['Tom', 'Eric']))), 
        of(v).pipe(map(_isStringNotEmpty))
      )               
    ),
    scan((e, v) => ({...e, ...v}))      
  )
  .subscribe((predicate) => console.log(predicate))

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

2 participants