Code Monkey home page Code Monkey logo

jayield's Introduction

JAYield

Build Status Coverage Status Maven Central

Minimalistic, extensible, non-parallel and lazy sequence implementation interoperable with Java Stream (toStream and fromStream), which provides an idiomatic yield like generator.

JAYield Query provides similar operations to Java Stream, or jOOλ Seq, or StreamEx, or Vavr Stream. Yet, Query is extensible and its methods can be chained fluently with new operations in a pipeline. Furthermore, Query has lower per-element access cost and offers an optimized fast-path traversal, which presents better sequential processing performance in some benchmarks, such as sequences-benchmarks and jayield-jmh.

The core API of Query provides well-known query methods that can be composed fluently (pipeline), e.g.:

// pipeline: iterate-filter-map-limit-forEach
//
Query.iterate('a', prev -> (char) ++prev).filter(n -> n%2 != 0).map(Object::toString).limit(10).forEach(out::println);

Extensibility and chaining

Notice how it looks a JAYield custom collapse() method that merges series of adjacent elements. It has a similar shape to that one written in any language providing the yield operator such as C#.

class Queries {
  private U prev = null;
  <U> Traverser<U>  collapse(Query<U> src) {
    return yield -> {
      src.traverse(item -> {
        if (prev == null || !prev.equals(item))
        yield.ret(prev = item);
      });
    };
  }
}
static class Extensions {
  static IEnumerable <T> Collapse <T>(this IEnumerable <T> src) {
    IEnumerator <T> iter = src.GetEnumerator();
    T prev = null;
    while(iter.MoveNext ()) {
      if(prev == null || !prev.Equals(iter.Current))
      yield return prev = iter.Current;
    }
  }
}

These methods can be chained in queries, such as:

Query
    .of(7, 7, 8, 9, 9, 8, 11, 11, 9, 7)
    .then(new Queries()::collapse)
    .filter(n -> n%2 != 0)
    .map(Object::toString)
    .traverse(out::println);
new int[]{7, 7, 8, 9, 9, 8, 11, 11, 9, 7}
    .Collapse()
    .Where(n => n%2 != 0)
    .Select(n => n.ToString())
    .ToList()
    .ForEach(Console.WriteLine);

Internals Overview

Advancer is the core iterator of Query that provides both individually and bulk traversal, trough java.util.Iterator and Traverser interfaces. Traverser is the primary choice for traversing the Query elements and supports all its methods including terminal, intermediate and short-circuting operations. To that end, the traversal's consumer - Yield - provides one method to return an element (ret) and other to finish the iteration (bye).

Installation

In order to include it to your Maven project, simply add this dependency:

<dependency>
    <groupId>com.github.jayield</groupId>
    <artifactId>jayield</artifactId>
    <version>1.2.0</version>
</dependency>

You can also download the artifact directly from Maven Central Repository

License

This project is licensed under Apache License, version 2.0

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.