question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

partition makes no sense as a lettable/pipeable operator

See original GitHub issue

RxJS version: 5.5.1

partition is one of the new lettable/pipeable operators in the operators directory. However, it’s not really lettable, as it does not return an Observable:

export function partition<T>(
  predicate: (value: T, index: number) => boolean,
  thisArg?: any
): UnaryFunction<Observable<T>, [Observable<T>, Observable<T>]> {
  return (source: Observable<T>) => [
    filter(predicate, thisArg)(source),
    filter(not(predicate, thisArg) as any)(source)
  ] as [Observable<T>, Observable<T>];
}

And with it defined as it is, the usage is somewhat clumsy:

const source = of(1, 2, 3, 4);
const [odds, evens] = partition(value => value % 2)(source);

Are all of the operators in the operators directory supposed to be lettable? Should it be defined elsewhere? And in a way that makes its calls more straightforward? That is, is there any point in its returning a function if that function cannot be passed to pipe (or to let)?

The solution for toPromise was to move it to the prototype, but is there are alternative solution for these types of ‘operators’ (if that term is even appropriate)?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:10
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
brattercommented, Feb 24, 2018

@maxime1992, @crimx, not sure if you were suggesting you’d solved or not, but if not… you need to do as follows:

const source = of(1, 2, 3, 4);
const [odds, evens] = partition(val => val % 2)(source);

Note that the partition function returns a function that has to then be called with the source observable as per felix’s comment above, and therefore cannot be used with pipe().

4reactions
maxime1992commented, Jan 30, 2018

Indeed, I thought I could do something like that:

of(1, 2, 3, 4)
  .pipe(
    partition((x) => x % 2 === 0)
  );

But it ends up with the following error:

Argument of type ‘UnaryFunction<Observable<any>, [Observable<any>, Observable<any>]>’ is not assignable to parameter of type ‘UnaryFunction<Observable<number>, Observable<Observable<any>>>’. Type ‘[Observable<any>, Observable<any>]’ is not assignable to type ‘Observable<Observable<any>>’. Property ‘_isScalar’ is missing in type ‘[Observable<any>, Observable<any>]’.

Repro: https://stackblitz.com/edit/angular-fwfubg?file=app%2Fapp.component.ts

Read more comments on GitHub >

github_iconTop Results From Across the Web

RxJS: Understanding Lettable Operators
Lettable operators offer a new way of composing observable chains and they have advantages for both application developers and library ...
Read more >
Call asynchronous operations in RxJS operators-rx.js
One way to do this is to: use flatMap to call the async function, returning both the value and the result to be...
Read more >
rxjs/CHANGELOG.md
- Our very new creation function, `connectable`, now takes a configuration object instead of just the `Subject` instance. This was necessary to make...
Read more >
rxjs | Yarn - Package Manager
Complete notifications no longer end the duration. throttle: the observable returned by the throttle operator's duration selector must emit a next notification ...
Read more >
Untitled
Gone for a burton meaning, Chu ky ten danh, Jon snow all scenes season 1, Kako zakrpiti ... Atletico de tucuman vs temperley,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found