GithubHelp home page GithubHelp logo

eibens / peek Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 5 KB

Add a peek function to iterators in TypeScript for Deno.

License: MIT License

TypeScript 100.00%
typescript deno iterable iterator peek utility

peek's Introduction

peek

peek implements a peeking iterator in TypeScript for Deno.

License Deno doc Deno module Github tag Build Code coverage

Motivation

A peeking iterator allows one to look at the next item without taking it. This can be useful if one only wants to consume items while a certain condition is met. For example, a scheduler algorithm that only schedules tasks within a certain time window can use a peeking iterator to check whether the event should be scheduled, otherwise leaving it in the iterator for the next scheduling cycle.

This module exports all other modules.

The fromIterator function creates a PeekingIterator from a normal Iterator by implementing the peek function.

import {
  fromIterator,
  PeekingIterator,
} from "https://deno.land/x/peek/peeking.ts";

const peeking: PeekingIterator = fromIterator(
  "abc"[Symbol.iterator](),
);

const preview: number = peeking.peek();
console.assert(preview === "a");

// The item is still in the iterator.
const next = peeking.next();
console.assert(next.value === "a");

The fromIterable function creates a PeekingIterator from an Iterable.

import { fromIterable } from "https://deno.land/x/peek/peeking.ts";

const peeking = fromIterable("abc");

console.assert(typeof peeking.peek === "function");

The takeWhile generator function removes items from a PeekingIterator as long as the specified condition is met.

import { fromIterable } from "https://deno.land/x/peek/peeking.ts";
import { takeWhile } from "https://deno.land/x/peek/take_while.ts";

const peeking = fromIterable("abcde");
const abc = takeWhile(peeking, (x) => x !== "d");

console.assert([...abc].join("") === "abc");

peek's People

Contributors

eibens avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.