Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Last active April 24, 2017 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpodwysocki/a227f49cb72242b063888bac526a68a7 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/a227f49cb72242b063888bac526a68a7 to your computer and use it in GitHub Desktop.
'use strict';
export function* buffer<TSource>(
source: Iterable<TSource>,
count: number,
skip?: number): Iterable<TSource[]> {
if (skip == null) { skip = count; }
let buffers: TSource[][] = [], i = 0;
for (let item of source)
{
if (i % skip === 0) {
buffers.push([]);
}
for (let buffer of buffers) {
buffer.push(item);
}
if (buffers.length > 0 && buffers[0].length === count) {
yield buffers.shift()!;
}
i++;
}
while (buffers.length > 0) {
yield buffers.shift()!;
}
}
'use strict';
import { bindCallback } from '../internal/bindcallback';
export function* filter<T>(
source: Iterable<T>,
predicate: (value: T, index: number) => boolean,
thisArg?: any): Iterable<T> {
let fn = bindCallback(predicate, thisArg, 2), i = 0;
for (let item of source) {
if (fn(item, i++)) {
yield item;
}
}
}
'use strict';
export function* _finally<TSource>(
source: Iterable<TSource>,
action: () => void) {
try {
for (let item of source) {
yield item;
}
} finally {
action();
}
}
@marcinnajder
Copy link

marcinnajder commented Apr 24, 2017

Hi

I mentioned some time ago about https://github.com/marcinnajder/powerseq (ReactiveX/IxJS#1) This is a complete implementation of all IxJS in RxJS5 style. Maybe it will be useful.

Marcin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment