logo
down
shadow

How is takeWhile different from filter?


How is takeWhile different from filter?

By : Zain
Date : November 29 2020, 03:01 PM
it helps some times filter will remove all items from the stream that do not satisfy the condition.
takeWhile will abort the stream on the first occurrence of an item which does not satisfy the condition.
code :
Stream.of(1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1)
    .filter(i -> i < 4 )
    .forEach(System.out::print);
Stream.of(1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1)
    .takeWhile(i -> i < 4 )
    .forEach(System.out::print);


Share : facebook icon twitter icon
Scala: What is the difference between filter and takeWhile on a stream?

Scala: What is the difference between filter and takeWhile on a stream?


By : Manisha Rathore
Date : March 29 2020, 07:55 AM
it fixes the issue The difference isn't stream-specific, but the same for all collections extending GenTraversableLike:
filter
code :
> List(1, 2, 3, 4).filter(_ % 2 == 1)
List(1, 3)

> List(1, 2, 3, 4).takeWhile(_ % 2 == 1)
List(1)
filter vs takeWhile: difference and runtime

filter vs takeWhile: difference and runtime


By : Nivedita22
Date : March 29 2020, 07:55 AM
I hope this helps you . Yes there is a huge difference. If we read the documentation, we see:
code :
filter odd (map (^2) [1..])
Prelude> takeWhile odd [1,3,5,12,7,9,1,3]
[1,3,5]
Prelude> filter odd [1,3,5,12,7,9,1,3]
[1,3,5,7,9,1,3]
RxJs 6 - React to concatMap in filter or takeWhile condition

RxJs 6 - React to concatMap in filter or takeWhile condition


By : jitender chauhan
Date : March 29 2020, 07:55 AM
it should still fix some issue The last callback of your subscribe is ran only for the second call. If you want the first HTTP call to call a method once done, try this :
code :
this.myService.createArticle(article)
  .pipe(
    finalize(() => console.log('Finally I have really reached the END (of the first call)'))
    filter(() => this.checkbox),
    concatMap(() => return this.mailer.createOtherResource(data)),
  }).subscribe(
    (x) => { console.log('OK', x); },
    error => console.error('FALLA', error),
    () => console.log('Finally I have really reached the END')
  );
Why takeWhile operator doesn't filter when inclusive?

Why takeWhile operator doesn't filter when inclusive?


By : user3219176
Date : March 29 2020, 07:55 AM
wish helps you After digging a little with the source code of the operator (https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/takeWhile.ts), there is indeed something wrong I gonna report as an issue to github.
in the meantime, here is a fixed custom takeWhileInclusive operator
code :
import { from, BehaviorSubject, Observable } from 'rxjs';
import { map, takeWhile } from 'rxjs/operators';

/** Custom takewhile inclusive Custom takewhile inclusive properly implemented */
const customTakeWhileInclusive = <T>(predicate: (value: T) => boolean) => (source: Observable<T>) => new Observable<T>(observer => {
  let lastMatch: T | undefined // fix
  return source.subscribe({
    next: e => {
      if (lastMatch) {
        observer.complete();
      }
      else {
        if (predicate(e)) {
          /*
           *   Code from https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/takeWhile.ts
           *  
           *   if (this.inclusive) {
           *      destination.next(value); // NO! with a synchronous scheduler, it will trigger another iteration without reaching the next "complete" statement 
           *      and there is no way to detect if a match already occured!
           *   }
           *   destination.complete();
           */

          // Fix:
          lastMatch = e; // prevents from having stackoverflow issue here
        }

        observer.next(e);
      }
    },
    error: (e) => observer.error(e),
    complete: () => observer.complete()
  });
});

const value$ = new BehaviorSubject<number>(1);

const source = value$.pipe(
  map(x => `value\$ = ${x}`),
  //takeWhile(x => !x.includes('4'), true)
  customTakeWhileInclusive(x => x.includes('4'))  // fix
);

source.subscribe(x => {
  console.log(x);
  value$.next(4);
});
Difference between takeWhile() and filter() method in Kotlin

Difference between takeWhile() and filter() method in Kotlin


By : JavaJack
Date : March 29 2020, 07:55 AM
To fix this issue The difference between both is filter() method returns a list with the elements that match an specific condition. And takeWhile() method also returns a list with the elements that match with the specific condition but just taking in account the firsts elements on the list.
An example for that:
shadow
Privacy Policy - Terms - Contact Us © voile276.org