Repository: marpple/FxTS (ts)

Issue:
Add `compress` function
# Suggestion

<!--
  Please fill in each section completely. Thank you!
-->

## ⭐ Suggestion

<!-- What would you like to be added? -->

ref https://docs.python.org/3/library/itertools.html#itertools.compress

## 💻 Use Cases

<!-- What do you want to use this for? -->

```ts
const iter1 = compress([false,true,false,false,true],  [1, 2, 3, 4, 5]);
iter1.next(); // {value: 2, done:false}
iter1.next(); // {value: 5, done:false}
iter1.next(); // {value: undefined, done:true }

const iter2 = compress([1,0,0,1,0],  "abcde");
iter2.next(); // {value: "a", done:false}
iter2.next(); // {value: "d", done:false}
iter2.next(); // {value: undefined, done:true }

```


Pull request:
feat: add `compress` function
Fixes #80 


Patch:
diff --git a/src/Lazy/compress.ts b/src/Lazy/compress.ts
new file mode 100644
index 00000000..71d0a9fd
--- /dev/null
+++ b/src/Lazy/compress.ts
@@ -0,0 +1,69 @@
+import zip from "./zip";
+import pipe from "../pipe";
+import filter from "./filter";
+import map from "./map";
+import ReturnIterableIteratorType from "../types/ReturnIterableIteratorType";
+
+/**
+ * Returns Iterable/AsyncIterable that filters elements from 'iterable' returning only those that have a corresponding element in 'selectors' that evaluates to 'true'.
+ * Stops when either 'iterable' or 'selectors' has been exhausted.
+ *
+ * @example
+ * ```ts
+ * const iter1 = compress([false,true,false,false,true],  [1, 2, 3, 4, 5]);
+ * iter1.next(); // {value: 2, done:false}
+ * iter1.next(); // {value: 5, done:false}
+ * iter1.next(); // {value: undefined, done:true }
+ *
+ * const iter2 = compress([1,0,0,1,0],  "abcde");
+ * iter2.next(); // {value: "a", done:false}
+ * iter2.next(); // {value: "d", done:false}
+ * iter2.next(); // {value: undefined, done:true }
+ *
+ * // with pipe
+ * pipe(
+ *   [1, 2, 3],
+ *   compress([false, true, true]),
+ *   toArray,
+ * ); // [2, 3]
+ * ```
+ *
+ * see {@link https://fxts.dev/docs/pipe | pipe}, {@link https://fxts.dev/docs/toArray | toArray}
+ */
+function compress<A, B>(
+  selector: Array<A>,
+  iterable: Iterable<B>,
+): IterableIterator<B>;
+
+function compress<A, B>(
+  selector: Array<A>,
+  iterable: AsyncIterable<B>,
+): AsyncIterableIterator<B>;
+
+function compress<A, B extends Iterable<unknown> | AsyncIterable<unknown>>(
+  selector: Array<A>,
+): (iterable: B) => ReturnIterableIteratorType<B>;
+
+function compress<A, B extends Iterable<unknown> | AsyncIterable<unknown>>(
+  selectors: Array<A>,
+  iterable?: B,
+):
+  | ReturnIterableIteratorType<B>
+  | ((iterable: B) => ReturnIterableIteratorType<B>) {
+  if (iterable === undefined) {
+    return (iterable: B) => {
+      return compress(
+        selectors,
+        iterable as any,
+      ) as ReturnIterableIteratorType<B>;
+    };
+  }
+
+  return pipe(
+    zip(selectors, iterable),
+    filter(([selector]) => selector),
+    map(([, value]) => value),
+  ) as ReturnIterableIteratorType<B>;
+}
+
+export default compress;
diff --git a/src/Lazy/index.ts b/src/Lazy/index.ts
index 03364915..fb5fa678 100644
--- a/src/Lazy/index.ts
+++ b/src/Lazy/index.ts
@@ -1,6 +1,7 @@
 import append from "./append";
 import chunk from "./chunk";
 import compact from "./compact";
+import compress from "./compress";
 import concat from "./concat";
 import concurrent from "./concurrent";
 import cycle from "./cycle";
@@ -22,6 +23,8 @@ import range from "./range";
 import reject from "./reject";
 import repeat from "./repeat";
 import reverse from "./reverse";
+import scan from "./scan";
+import slice from "./slice";
 import split from "./split";
 import take from "./take";
 import takeUntil from "./takeUntil";
@@ -37,6 +40,7 @@ export {
   append,
   chunk,
   compact,
+  compress,
   concat,
   concurrent,
   cycle,
@@ -58,6 +62,8 @@ export {
   reject,
   repeat,
   reverse,
+  scan,
+  slice,
   split,
   take,
   takeUntil,
diff --git a/src/Lazy/scan.ts b/src/Lazy/scan.ts
new file mode 100644
index 00000000..387f139b
--- /dev/null
+++ b/src/Lazy/scan.ts
@@ -0,0 +1,202 @@
+import pipe1 from "../pipe1";
+import Arrow from "../types/Arrow";
+import Awaited from "../types/Awaited";
+import IterableInfer from "../types/IterableInfer";
+import ReturnIterableIteratorType from "../types/ReturnIterableIteratorType";
+import { empty, isAsyncIterable, isIterable } from "../_internal/utils";
+import concurrent, { isConcurrent } from "./concurrent";
+import head from "../head";
+
+function* sync<A, B>(
+  f: (a: B, b: A) => B,
+  acc: B,
+  iterable: Iterable<A>,
+): IterableIterator<B> {
+  yield acc;
+  for (const a of iterable) {
+    yield (acc = f(acc, a));
+  }
+}
+
+async function* asyncSequential<A, B>(
+  f: (a: B, b: A) => B,
+  acc: Promise<B>,
+  iterable: AsyncIterable<A>,
+): AsyncIterableIterator<B> {
+  yield acc;
+  for await (const a of iterable) {
+    yield (acc = pipe1(acc, (acc) => f(acc as B, a)));
+  }
+}
+
+function async<A, B>(
+  f: (a: B, b: A) => B,
+  acc: Promise<B>,
+  iterable: AsyncIterable<A>,
+): AsyncIterableIterator<B> {
+  let _iterator: AsyncIterator<B>;
+  return {
+    async next(_concurrent: any) {
+      if (_iterator === undefined) {
+        _iterator = isConcurrent(_concurrent)
+          ? asyncSequential(f, acc, concurrent(_concurrent.length, iterable))
+          : asyncSequential(f, acc, iterable);
+      }
+      return _iterator.next(_concurrent);
+    },
+    [Symbol.asyncIterator]() {
+      return this;
+    },
+  };
+}
+
+function asyncWithoutSeed<A, B>(
+  f: (a: B, b: A) => B,
+  iterable: AsyncIterable<A>,
+): AsyncIterableIterator<B> {
+  let _iterator: AsyncIterator<B>;
+  return {
+    async next(_concurrent: any) {
+      if (_iterator === undefined) {
+        if (isConcurrent(_concurrent)) {
+          const _iterable = concurrent(_concurrent.length, iterable);
+          _iterator = asyncSequential(f, head(_iterable) as any, _iterable);
+        } else {
+          _iterator = asyncSequential(f, head(iterable) as any, iterable);
+        }
+      }
+      return _iterator.next(_concurrent);
+    },
+    [Symbol.asyncIterator]() {
+      return this;
+    },
+  };
+}
+
+/**
+ * Returns a Iterable/AsyncIterable of successively reduced values from the left.
+ * It's similar to reduce
+ *
+ * @example
+ * ```ts
+ * const iter = scan((acc, cur) => acc * cur, 1, [1, 2, 3, 4 ]);
+ * iter.next(); // {value: 1, done:false}
+ * iter.next(); // {value: 1, done:false}
+ * iter.next(); // {value: 2, done:false}
+ * iter.next(); // {value: 6, done:false}
+ * iter.next(); // {value: 24, done:false}
+ * iter.next(); // {value: undefined, done: true}
+ *
+ * // with pipe
+ * pipe(
+ *   [1, 2, 3, 4],
+ *   scan,
+ *   toArray,
+ * ); // [1, 2, 6, 24]
+ * ```
+ */
+function scan<A extends readonly []>(
+  f: Arrow,
+  iterable: A,
+): IterableIterator<never>;
+
+function scan<A extends readonly [], B>(
+  f: Arrow,
+  seed: B,
+  iterable: A,
+): IterableIterator<B>;
+
+function scan<A>(
+  f: (a: A, b: A) => A,
+  iterable: Iterable<A>,
+): IterableIterator<A>;
+
+function scan<A, B>(
+  f: (a: B, b: A) => B,
+  seed: B,
+  iterable: Iterable<A>,
+): IterableIterator<B>;
+
+function scan<A, B>(
+  f: (a: B, b: A) => B,
+  iterable: Iterable<A>,
+): IterableIterator<B>;
+
+function scan<A>(
+  f: (a: A, b: A) => A | Promise<A>,
+  iterable: AsyncIterable<A>,
+): AsyncIterableIterator<A>;
+
+function scan<A, B>(
+  f: (a: Awaited<B>, b: A) => B | Promise<B>,
+  seed: B | Promise<B>,
+  iterable: AsyncIterable<A>,
+): AsyncIterableIterator<B>;
+
+function scan<A, B>(
+  f: (a: Awaited<B>, b: A) => B | Promise<B>,
+  iterable: AsyncIterable<A>,
+): AsyncIterableIterator<Awaited<B>>;
+
+function scan<A extends Iterable<unknown> | AsyncIterable<unknown>>(
+  f: (
+    a: IterableInfer<A>,
+    b: IterableInfer<A>,
+  ) => IterableInfer<A> | Promise<IterableInfer<A>>,
+): (iterable: A) => ReturnIterableIteratorType<A, IterableInfer<A>>;
+
+function scan<A extends Iterable<unknown> | AsyncIterable<unknown>, B>(
+  f: (a: B, b: IterableInfer<A>) => B | Promise<B>,
+): (iterable: A) => ReturnIterableIteratorType<A, B>;
+
+function scan<A extends Iterable<unknown> | AsyncIterable<unknown>, B>(
+  f: (a: B, b: IterableInfer<A>) => B,
+  seed?: B | Iterable<IterableInfer<A>> | AsyncIterable<IterableInfer<A>>,
+  iterable?: Iterable<IterableInfer<A>> | AsyncIterable<IterableInfer<A>>,
+):
+  | IterableIterator<B>
+  | AsyncIterableIterator<B>
+  | ((iterable: A) => ReturnIterableIteratorType<A, B>) {
+  if (iterable === undefined) {
+    if (seed === undefined) {
+      return (iterable: A) => {
+        return scan(
+          f,
+          iterable as any,
+        ) as unknown as ReturnIterableIteratorType<A, B>;
+      };
+    }
+
+    if (isIterable(seed)) {
+      const iterator = seed[Symbol.iterator]();
+      const { done, value } = iterator.next();
+      if (done) {
+        return empty() as IterableIterator<B>;
+      }
+
+      return sync(f, value, {
+        [Symbol.iterator]() {
+          return iterator;
+        },
+      });
+    }
+
+    if (isAsyncIterable(seed)) {
+      return asyncWithoutSeed(f, seed);
+    }
+
+    throw new TypeError("'iterable' must be type of Iterable or AsyncIterable");
+  }
+
+  if (isIterable(iterable)) {
+    return sync(f, seed as B, iterable);
+  }
+
+  if (isAsyncIterable(iterable)) {
+    return async(f, Promise.resolve(seed as B), iterable);
+  }
+
+  throw new TypeError("'iterable' must be type of Iterable or AsyncIterable");
+}
+
+export default scan;
diff --git a/src/Lazy/slice.ts b/src/Lazy/slice.ts
new file mode 100644
index 00000000..80446245
--- /dev/null
+++ b/src/Lazy/slice.ts
@@ -0,0 +1,176 @@
+import isNumber from "../isNumber";
+import ReturnIterableIteratorType from "../types/ReturnIterableIteratorType";
+import { isAsyncIterable, isIterable } from "../_internal/utils";
+import concurrent, { isConcurrent } from "./concurrent";
+
+function* sync<T>(
+  start: number,
+  end: number,
+  iterable: Iterable<T>,
+): IterableIterator<T> {
+  let i = 0;
+  for (const item of iterable) {
+    if (i >= start && i < end) {
+      yield item;
+    }
+    i += 1;
+  }
+}
+
+async function* asyncSequential<T>(
+  start: number,
+  end: number,
+  iterable: AsyncIterable<T>,
+): AsyncIterableIterator<T> {
+  let i = 0;
+  for await (const item of iterable) {
+    if (i >= start && i < end) {
+      yield item;
+    }
+    i += 1;
+  }
+}
+
+function async<T>(
+  start: number,
+  end: number,
+  iterable: AsyncIterable<T>,
+): AsyncIterableIterator<T> {
+  let iterator: AsyncIterator<T>;
+  return {
+    [Symbol.asyncIterator]() {
+      return this;
+    },
+
+    async next(_concurrent: any) {
+      if (iterator === undefined) {
+        // prettier-ignore
+        iterator = isConcurrent(_concurrent)
+          ? asyncSequential(start, end, concurrent(_concurrent.length, iterable))
+          : asyncSequential(start, end, iterable);
+      }
+
+      return iterator.next(_concurrent);
+    },
+  };
+}
+
+function _slice<T extends Iterable<unknown> | AsyncIterable<unknown>>(
+  start: number,
+  end: number,
+  iterable: T,
+) {
+  if (!isNumber(start) || !isNumber(end)) {
+    throw new TypeError("'start' and 'end' must be type of number");
+  }
+
+  if (isIterable(iterable)) {
+    return sync(start, end, iterable);
+  }
+
+  if (isAsyncIterable(iterable)) {
+    return async(start, end, iterable);
+  }
+
+  throw new TypeError("'iterable' must be type of Iterable or AsyncIterable");
+}
+
+/**
+ * Returns Iterable/AsyncIterable of the given elements from startIndex(inclusive) to endIndex(exclusive).
+ *
+ * @example
+ * ```ts
+ * const iter1 = slice(1, 3, ['a', 'b', 'c', 'd', 'e']);
+ * iter1.next(); // {value: 'b', done: false};
+ * iter1.next(); // {value: 'c', done: false};
+ * iter1.next(); // {value: undefined, done: true};
+ *
+ * const iter2 = slice(3, ['a', 'b', 'c', 'd', 'e']);
+ * iter2.next(); // {value: 'd', done: false};
+ * iter2.next(); // {value: 'e', done: false};
+ * iter2.next(); // {value: undefined, done: true};
+ *
+ * const iter3 = slice(3, "abcde");
+ * iter3.next(); // {value: 'd', done: false};
+ * iter3.next(); // {value: 'e', done: false};
+ * iter3.next(); // {value: undefined, done: true};
+ *
+ * // with pipe
+ * pipe(
+ *  [1, 2, 3, 4, 5],
+ *  slice(3),
+ *  toArray,
+ * ); // [4, 5]
+ *
+ * pipe(
+ *  [1, 2, 3, 4, 5],
+ *  slice(1, 3),
+ *  toArray,
+ * ); // [2, 3]
+ *
+ * see {@link https://fxts.dev/docs/pipe | pipe}, {@link https://fxts.dev/docs/toArray | toArray}
+ * ```
+ */
+// prettier-ignore
+function slice<T>(
+  start: number, 
+  iterable: Iterable<T>
+): IterableIterator<T>;
+
+function slice<T>(
+  start: number,
+  iterable: AsyncIterable<T>,
+): AsyncIterableIterator<T>;
+
+function slice<T>(
+  start: number,
+  end: number,
+  iterable: Iterable<T>,
+): IterableIterator<T>;
+
+function slice<T>(
+  start: number,
+  end: number,
+  iterable: AsyncIterable<T>,
+): AsyncIterableIterator<T>;
+
+function slice<A extends Iterable<unknown> | AsyncIterable<unknown>>(
+  start: number,
+): (iterable: A) => ReturnIterableIteratorType<A>;
+
+function slice<A extends Iterable<unknown> | AsyncIterable<unknown>>(
+  start: number,
+  end: number,
+): (iterable: A) => ReturnIterableIteratorType<A>;
+
+function slice<T extends Iterable<unknown> | AsyncIterable<unknown>>(
+  start: number | T,
+  end?: number | T,
+  iterable?: T,
+) {
+  if (iterable === undefined) {
+    if (end === undefined) {
+      return (iterable: T) => {
+        return _slice(start as number, Infinity, iterable);
+      };
+    }
+
+    if (isIterable(end) || isAsyncIterable(end)) {
+      return _slice(start as number, Infinity, end);
+    }
+
+    if (isNumber(end)) {
+      return (iterable: T) => {
+        return _slice(start as number, end, iterable);
+      };
+    }
+
+    return (iterable: T) => {
+      return _slice(0, Infinity, iterable);
+    };
+  }
+
+  return _slice(start as number, end as number, iterable as T);
+}
+
+export default slice;


Test patch:
diff --git a/test/Lazy/compress.spec.ts b/test/Lazy/compress.spec.ts
new file mode 100644
index 00000000..03213d85
--- /dev/null
+++ b/test/Lazy/compress.spec.ts
@@ -0,0 +1,87 @@
+import { compress, map, pipe, toArray, toAsync } from "../../src";
+import { Concurrent } from "../../src/Lazy/concurrent";
+import { generatorMock } from "../utils";
+
+describe("compress", function () {
+  describe("sync", function () {
+    it.each([
+      [
+        [false, true, false, false, true],
+        [1, 2, 3, 4, 5],
+        [2, 5],
+      ],
+      [
+        [false, true, false, false, true],
+        [1, 2, 3, 4, 5, 6],
+        [2, 5],
+      ],
+      [[false, true, false, false, true], [1, 2, 3, 4], [2]],
+      [[1, 0, 0, 1, 0], "abcde", ["a", "d"]],
+    ])(
+      "should filter elements that have a corresponding element in 'selectors'",
+      function (selectors, iterable, result) {
+        const res = compress(
+          selectors as Array<any>,
+          iterable as Iterable<any>,
+        );
+        expect(toArray(res)).toEqual(result);
+      },
+    );
+
+    it("should be able to be used as a curried function in the pipeline", function () {
+      const res = pipe(
+        [1, 2, 3, 4, 5],
+        compress([false, true, false, false, true]),
+        toArray,
+      );
+
+      expect(res).toEqual([2, 5]);
+    });
+  });
+
+  describe("async", function () {
+    it.each([
+      [
+        [false, true, false, false, true],
+        [1, 2, 3, 4, 5],
+        [2, 5],
+      ],
+      [
+        [false, true, false, false, true],
+        [1, 2, 3, 4, 5, 6],
+        [2, 5],
+      ],
+      [[false, true, false, false, true], [1, 2, 3, 4], [2]],
+      [[1, 0, 0, 1, 0], "abcde", ["a", "d"]],
+    ])(
+      "should filter elements that have a corresponding element in 'selectors'",
+      async function (selectors, iterable, result) {
+        const res = compress(
+          selectors as Array<any>,
+          iterable as Iterable<any>,
+        );
+        expect(await toArray(toAsync(res))).toEqual(result);
+      },
+    );
+
+    it("should be able to be used as a curried function in the pipeline", async function () {
+      const res = await pipe(
+        [1, 2, 3, 4, 5],
+        toAsync,
+        compress([false, true, false, false, true]),
+        toArray,
+      );
+
+      expect(res).toEqual([2, 5]);
+    });
+
+    it("should be passed concurrent object when job works concurrently", async function () {
+      const mock = generatorMock();
+      const iter = map((a) => a, mock);
+      const concurrent = Concurrent.of(2) as any;
+
+      await iter.next(concurrent);
+      expect((mock as any).getConcurrent()).toEqual(concurrent);
+    });
+  });
+});
diff --git a/test/Lazy/scan.spec.ts b/test/Lazy/scan.spec.ts
new file mode 100644
index 00000000..2e78c974
--- /dev/null
+++ b/test/Lazy/scan.spec.ts
@@ -0,0 +1,149 @@
+import { delay, map, pipe, range, toArray, toAsync } from "../../src";
+import concurrent, { Concurrent } from "../../src/Lazy/concurrent";
+import scan from "../../src/Lazy/scan";
+import { callFuncAfterTime, generatorMock } from "../utils";
+
+describe("scan", function () {
+  describe("sync", function () {
+    it.each([
+      [(a: number, b: number) => a * b, 1, [1, 2, 3, 4], [1, 1, 2, 6, 24]],
+      [(a: string, b: string) => a + b, "a", ["b", "c"], ["a", "ab", "abc"]],
+    ])(
+      "should return value that is reduced given elements successively %s %s %s",
+      function (f, seed, iterable, result) {
+        expect(toArray(scan(f, seed, iterable as any))).toEqual(result);
+      },
+    );
+
+    it.each([
+      [(a: number, b: number) => a * b, [1, 2, 3, 4], [1, 2, 6, 24]],
+      [(a: string, b: string) => a + b, ["a", "b", "c"], ["a", "ab", "abc"]],
+    ])(
+      "should reduce given elements successively without seed %s %s %s",
+      function (f, iterable, result) {
+        expect(toArray(scan(f, iterable as any))).toEqual(result);
+      },
+    );
+
+    it("should be able to be used as a curried function in the pipeline", function () {
+      const res = pipe(
+        [1, 2, 3, 4],
+        scan((a, b) => a * b),
+        toArray,
+      );
+
+      expect(res).toEqual([1, 2, 6, 24]);
+    });
+  });
+
+  describe("async", function () {
+    it.each([
+      [(a: number, b: number) => a * b, 1, [1, 2, 3, 4], [1, 1, 2, 6, 24]],
+      [(a: string, b: string) => a + b, "a", ["b", "c"], ["a", "ab", "abc"]],
+    ])(
+      "should return value that is reduced given elements successively %s %s %s",
+      async function (f, seed, iterable, result) {
+        expect(
+          await toArray(scan(f, seed, toAsync(iterable as any) as any)),
+        ).toEqual(result);
+      },
+    );
+
+    it.each([
+      [(a: number, b: number) => a * b, 1, [1, 2, 3, 4], [1, 1, 2, 6, 24]],
+      [(a: string, b: string) => a + b, "a", ["b", "c"], ["a", "ab", "abc"]],
+    ])(
+      "should reduce given elements successively when seed is Promise %s %s %s",
+      async function (f, seed, iterable, result) {
+        expect(
+          await toArray(
+            scan(f, Promise.resolve(seed), toAsync(iterable as any) as any),
+          ),
+        ).toEqual(result);
+      },
+    );
+
+    it.each([
+      [(a: number, b: number) => a * b, [1, 2, 3, 4], [1, 2, 6, 24]],
+      [(a: string, b: string) => a + b, ["a", "b", "c"], ["a", "ab", "abc"]],
+    ])(
+      "should reduce given elements successively without seed %s %s %s",
+      async function (f, iterable, result) {
+        expect(await toArray(scan(f, toAsync(iterable as any) as any))).toEqual(
+          result,
+        );
+      },
+    );
+
+    it("should be able to be used as a curried function in the pipeline", async function () {
+      const res = await pipe(
+        [1, 2, 3, 4],
+        toAsync,
+        scan((a, b) => a * b),
+        toArray,
+      );
+
+      expect(res).toEqual([1, 2, 6, 24]);
+    });
+
+    it("should be passed concurrent object when job works concurrently", async function () {
+      const mock = generatorMock();
+      const iter = scan((a) => a, mock);
+      const concurrent = Concurrent.of(2) as any;
+
+      await iter.next(concurrent);
+      expect((mock as any).getConcurrent()).toEqual(concurrent);
+    });
+
+    it("should be handled concurrently", async function () {
+      const fn = jest.fn();
+      callFuncAfterTime(fn, 600);
+
+      const res = await pipe(
+        [1, 2, 3, 4, 5, 6, 7, 8, 9],
+        toAsync,
+        map((a) => delay(200, a)),
+        (nums) => scan((a, b) => a * b, 1, nums),
+        concurrent(3),
+        toArray,
+      );
+
+      expect(fn).toBeCalled();
+      expect(res).toEqual([1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]);
+    }, 650);
+
+    it("should be handled concurrently without seed", async function () {
+      const fn = jest.fn();
+      callFuncAfterTime(fn, 600);
+
+      const res = await pipe(
+        [1, 2, 3, 4, 5, 6, 7, 8, 9],
+        toAsync,
+        map((a) => delay(200, a)),
+        scan((a, b) => a * b),
+        concurrent(3),
+        toArray,
+      );
+
+      expect(fn).toBeCalled();
+      expect(res).toEqual([1, 2, 6, 24, 120, 720, 5040, 40320, 362880]);
+    }, 650);
+
+    it("should be able to handle an error when working concurrent", async function () {
+      await expect(
+        pipe(
+          toAsync(range(1, 21)),
+          map((a) => delay(100, a)),
+          scan((a, b) => {
+            if (a * b === 24) {
+              throw new Error("err");
+            }
+            return a * b;
+          }),
+          concurrent(2),
+          toArray,
+        ),
+      ).rejects.toThrow("err");
+    });
+  });
+});
diff --git a/test/Lazy/slice.spec.ts b/test/Lazy/slice.spec.ts
new file mode 100644
index 00000000..a6023bae
--- /dev/null
+++ b/test/Lazy/slice.spec.ts
@@ -0,0 +1,95 @@
+import { pipe, slice, toArray, toAsync } from "../../src";
+import { Concurrent } from "../../src/Lazy/concurrent";
+import { generatorMock } from "../utils";
+
+describe("slice", function () {
+  describe("sync", function () {
+    it.each([
+      [1, 3, [1, 2, 3, 4, 5], [2, 3]],
+      [-1, 3, [1, 2, 3, 4, 5], [1, 2, 3]],
+      [2, Infinity, [1, 2, 3, 4, 5], [3, 4, 5]],
+      [7, 3, [1, 2, 3, 4, 5], []],
+      [1, 3, "abcde", ["b", "c"]],
+    ])(
+      "should return elements from startIndex(%i) to endIndex(%i) of %s",
+      function (startIndex, endIndex, iterable, result) {
+        expect(
+          toArray(slice(startIndex, endIndex, iterable as Iterable<any>)),
+        ).toEqual(result);
+      },
+    );
+
+    it.each([
+      [1, [1, 2, 3, 4, 5], [2, 3, 4, 5]],
+      [-1, [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]],
+      [2, [1, 2, 3, 4, 5], [3, 4, 5]],
+      [7, [1, 2, 3, 4, 5], []],
+      [1, "abcde", ["b", "c", "d", "e"]],
+    ])(
+      "should return elements from startIndex(%i) to end of %s",
+      function (startIndex, iterable, result) {
+        expect(toArray(slice(startIndex, iterable as Iterable<any>))).toEqual(
+          result,
+        );
+      },
+    );
+
+    it("should be able to be used as a curried function in the pipeline", function () {
+      const res1 = pipe([1, 2, 3, 4, 5], slice(2), toArray);
+      expect(res1).toEqual([3, 4, 5]);
+
+      const res2 = pipe([1, 2, 3, 4, 5], slice(1, 3), toArray);
+      expect(res2).toEqual([2, 3]);
+    });
+  });
+
+  describe("async", function () {
+    it.each([
+      [1, 3, [1, 2, 3, 4, 5], [2, 3]],
+      [-1, 3, [1, 2, 3, 4, 5], [1, 2, 3]],
+      [2, Infinity, [1, 2, 3, 4, 5], [3, 4, 5]],
+      [7, 3, [1, 2, 3, 4, 5], []],
+      [1, 3, "abcde", ["b", "c"]],
+    ])(
+      "should return elements from startIndex(%i) to endIndex(%i) of %s",
+      async function (startIndex, endIndex, iterable, result) {
+        expect(
+          await toArray(
+            slice(startIndex, endIndex, toAsync(iterable as Iterable<any>)),
+          ),
+        ).toEqual(result);
+      },
+    );
+
+    it.each([
+      [1, [1, 2, 3, 4, 5], [2, 3, 4, 5]],
+      [-1, [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]],
+      [2, [1, 2, 3, 4, 5], [3, 4, 5]],
+      [7, [1, 2, 3, 4, 5], []],
+      [1, "abcde", ["b", "c", "d", "e"]],
+    ])(
+      "should return elements from startIndex(%i) to end of %s",
+      async function (startIndex, iterable, result) {
+        expect(
+          await toArray(slice(startIndex, toAsync(iterable as Iterable<any>))),
+        ).toEqual(result);
+      },
+    );
+
+    it("should be able to be used as a curried function in the pipeline", async function () {
+      const res1 = await pipe([1, 2, 3, 4, 5], toAsync, slice(2), toArray);
+      expect(res1).toEqual([3, 4, 5]);
+
+      const res2 = await pipe([1, 2, 3, 4, 5], toAsync, slice(1, 3), toArray);
+      expect(res2).toEqual([2, 3]);
+    });
+
+    it("should be passed concurrent object when job works concurrently", async function () {
+      const mock = generatorMock();
+      const iter = slice(1, 2, mock);
+      const concurrent = Concurrent.of(2) as any;
+      await iter.next(concurrent);
+      expect((mock as any).getConcurrent()).toEqual(concurrent);
+    });
+  });
+});
diff --git a/type-check/Lazy/compress.test.ts b/type-check/Lazy/compress.test.ts
new file mode 100644
index 00000000..2ef34dbb
--- /dev/null
+++ b/type-check/Lazy/compress.test.ts
@@ -0,0 +1,24 @@
+import * as Test from "../../src/types/Test";
+import { toAsync, compress, pipe } from "../../src";
+
+const { checks, check } = Test;
+
+const res1 = compress([], []);
+const res2 = compress([true, false], [1, 0]);
+const res3 = compress([true, false], "ab");
+const res4 = compress([true, 0], [1, 0]);
+const res5 = compress([true, false], toAsync([1, 0]));
+const res6 = pipe([1, 0], compress([true, false]));
+const res7 = pipe(Promise.resolve([1, 2, 3]), compress([true, false]));
+const res8 = pipe(toAsync([1, 2, 3]), compress([true, false]));
+
+checks([
+  check<typeof res1, IterableIterator<never>, Test.Pass>(),
+  check<typeof res2, IterableIterator<number>, Test.Pass>(),
+  check<typeof res3, IterableIterator<string>, Test.Pass>(),
+  check<typeof res4, IterableIterator<number>, Test.Pass>(),
+  check<typeof res5, AsyncIterableIterator<number>, Test.Pass>(),
+  check<typeof res6, IterableIterator<number>, Test.Pass>(),
+  check<typeof res7, Promise<IterableIterator<number>>, Test.Pass>(),
+  check<typeof res8, AsyncIterableIterator<number>, Test.Pass>(),
+]);
diff --git a/type-check/Lazy/scan.test.ts b/type-check/Lazy/scan.test.ts
new file mode 100644
index 00000000..c45c9642
--- /dev/null
+++ b/type-check/Lazy/scan.test.ts
@@ -0,0 +1,81 @@
+import * as Test from "../../src/types/Test";
+import { pipe, scan, toAsync } from "../../src";
+
+const { checks, check } = Test;
+
+const res1 = scan((a, b) => a + b, []);
+const res2 = scan((a, b) => a + b, "seed", []);
+const res3 = scan((a, b) => a + b, 0, [1, 2, 3]);
+const res4 = scan((a, b) => a + b, [1, 2, 3]);
+const res5 = scan((a, b) => a + b, "0", [1, 2, 3]);
+const res6 = scan((a: string, b) => String(Number(a) + Number(b)), [1, 2, 3]);
+const res7 = scan((a, b) => a + b, toAsync([1, 2, 3]));
+const res8 = scan((a, b) => a + b, 0, toAsync([1, 2, 3]));
+const res9 = scan((a, b) => a + b, Promise.resolve(0), toAsync([1, 2, 3]));
+const res10 = scan(
+  (a: string, b) => String(Number(a) + Number(b)),
+  toAsync([1, 2, 3]),
+);
+const res11 = scan(
+  (a: string, b) => Promise.resolve(String(Number(a) + Number(b))),
+  toAsync([1, 2, 3]),
+);
+
+const res12 = pipe(
+  [1, 2, 3],
+  scan((a, b) => a + b),
+);
+
+const res13 = pipe(
+  [1, 2, 3],
+  scan((a: string, b) => String(Number(a) + Number(b))),
+);
+
+const res14 = pipe(
+  [1, 2, 3],
+  toAsync,
+  scan((a, b) => a + b),
+);
+
+const res15 = pipe(
+  [1, 2, 3],
+  toAsync,
+  scan((a, b) => Promise.resolve(a + b)),
+);
+
+const res16 = pipe(
+  [1, 2, 3],
+  toAsync,
+  scan((a: string, b) => String(Number(a) + Number(b))),
+);
+
+const res17 = pipe(
+  [1, 2, 3],
+  toAsync,
+  scan((a: string, b) => Promise.resolve(String(Number(a) + Number(b)))),
+);
+
+const res18 = pipe([1, 2, 3], toAsync, (res) =>
+  scan((a, b) => Promise.resolve(a + b), 0, res),
+);
+
+checks([
+  check<typeof res1, IterableIterator<never>, Test.Pass>(),
+  check<typeof res2, IterableIterator<string>, Test.Pass>(),
+  check<typeof res3, IterableIterator<number>, Test.Pass>(),
+  check<typeof res4, IterableIterator<number>, Test.Pass>(),
+  check<typeof res5, IterableIterator<string>, Test.Pass>(),
+  check<typeof res6, IterableIterator<string>, Test.Pass>(),
+  check<typeof res7, AsyncIterableIterator<number>, Test.Pass>(),
+  check<typeof res8, AsyncIterableIterator<number>, Test.Pass>(),
+  check<typeof res9, AsyncIterableIterator<number>, Test.Pass>(),
+  check<typeof res10, AsyncIterableIterator<string>, Test.Pass>(),
+  check<typeof res11, AsyncIterableIterator<string>, Test.Pass>(),
+  check<typeof res12, IterableIterator<number>, Test.Pass>(),
+  check<typeof res13, IterableIterator<string>, Test.Pass>(),
+  check<typeof res14, AsyncIterableIterator<number>, Test.Pass>(),
+  check<typeof res15, AsyncIterableIterator<number>, Test.Pass>(),
+  check<typeof res16, AsyncIterableIterator<string>, Test.Pass>(),
+  check<typeof res17, AsyncIterableIterator<string>, Test.Pass>(),
+  check<typeof res18, AsyncIterableIterator<number>, Test.Pass>(),
+]);
diff --git a/type-check/Lazy/slice.test.ts b/type-check/Lazy/slice.test.ts
new file mode 100644
index 00000000..a22858a6
--- /dev/null
+++ b/type-check/Lazy/slice.test.ts
@@ -0,0 +1,47 @@
+import * as Test from "../../src/types/Test";
+import { slice, pipe, toAsync } from "../../src";
+
+const { checks, check } = Test;
+
+const res1 = slice(2, [1, 2, 3, 4]);
+const res2 = slice(1, 2, [1, 2, 3, 4]);
+const res3 = slice(2, "abcde");
+const res4 = slice(1, 2, "abcde");
+
+const res5 = slice(2, toAsync([1, 2, 3, 4]));
+const res6 = slice(1, 2, toAsync([1, 2, 3, 4]));
+
+const res7 = pipe([1, 2, 3, 4, 5, 6, 7], slice(2));
+const res8 = pipe([1, 2, 3, 4, 5, 6, 7], slice(2));
+
+const res9 = pipe([1, 2, 3, 4, 5, 6, 7], slice(2, 3));
+const res10 = pipe([1, 2, 3, 4, 5, 6, 7], slice(2, 3));
+
+const res11 = pipe([1, 2, 3, 4, 5, 6, 7], toAsync, slice(2));
+const res12 = pipe([1, 2, 3, 4, 5, 6, 7], toAsync, slice(2));
+
+const res13 = pipe([1, 2, 3, 4, 5, 6, 7], toAsync, slice(2, 3));
+const res14 = pipe([1, 2, 3, 4, 5, 6, 7], toAsync, slice(2, 3));
+
+checks([
+  check<typeof res1, IterableIterator<number>, Test.Pass>(),
+  check<typeof res2, IterableIterator<number>, Test.Pass>(),
+
+  check<typeof res3, IterableIterator<string>, Test.Pass>(),
+  check<typeof res4, IterableIterator<string>, Test.Pass>(),
+
+  check<typeof res5, AsyncIterableIterator<number>, Test.Pass>(),
+  check<typeof res6, AsyncIterableIterator<number>, Test.Pass>(),
+
+  check<typeof res7, IterableIterator<number>, Test.Pass>(),
+  check<typeof res8, IterableIterator<number>, Test.Pass>(),
+
+  check<typeof res9, IterableIterator<number>, Test.Pass>(),
+  check<typeof res10, IterableIterator<number>, Test.Pass>(),
+
+  check<typeof res11, AsyncIterableIterator<number>, Test.Pass>(),
+  check<typeof res12, AsyncIterableIterator<number>, Test.Pass>(),
+
+  check<typeof res13, AsyncIterableIterator<number>, Test.Pass>(),
+  check<typeof res14, AsyncIterableIterator<number>, Test.Pass>(),
+]);