caffeine

Replaces common java.util classes along with providing exciting new Data Structures:

  • Tries
  • Graphs
    • Finite Automaton
    • Weighted/Unweighted
  • Sequences
    • Lazy evaluation
    • Functional style
  • Monadic Pipelines
    • 'Streams' implementation with extra functions
  • Pattern Matching and Guards
    • Along with configure factory method for matching any object.
  • Tuples(0-12 arity), Try class, Lazy class
  • Cons list and recursive Trees using pattern matching.

Aims

  • The only imports from java.util will be to implement an Interface
  • Where possible, use generics and implement Collection, Set, Map, List, Iterable etc.
  • Always reduce complexity in algorithms
  • Always refactor to minimise repeated code

Pipelines

  • I have implemented and augmented the API for java.util.streams.Stream with Pipeline
    • Upgraded to match JDK 9 Streams API, with addition of more functional features:
    • zipping, cartesian product, matching, streaming optionals, pattern matching.
  • Implemented as a linked list of Nodes that perform operations on values passed to them

Sequences

I have combined my implementation of Pipelines with additional functions for use in creating sequences

  • Pick an algorithm style from
    • Recursive (New terms are generated based on the previous ones)
    • Non Recursive (Terms are only dependent on their index)
  • Users can then:
    • Set the range of terms in scope
    • filter only terms that pass a predicate
    • take terms while a predicate holds true
    • drop terms while a predicate holds true
    • add and remove these conditions on the same Sequence instance
    • view individual terms
    • convert to a Pipeline for more options;
  • Terms are memoised for higher performance over multiple iterations.

  • Terms are generated lazily, i.e. only when they are requested.

    • This saves complexity, especially when using nested sequences
  • Sequences are enhanced by:

    • SequenceOf - Contains plenty of standard sequences, such as natural numbers, integers, primes, ranges
    • Functions - Contains anonymous functions for use with the total() method
    • PredicateFor - Contains predicates for use with filtering or to add conditions

Prerequisites

  • Java 8

Usage

  1. Clone this repository onto your system
  2. Include the package com.bishabosha.caffeine in your build path

Replace standard library with:

  • HashMap built on HashTable, implements java.util.Map
  • HashTable implements java.util.Set and also provides term() method
  • TreeMap built on SearchTree implements java.util.Map AVL balanced
  • TreeSet built on SearchTree implements java.util.Set AVL balanced
  • LinkedList implements java.util.Queue, java.util.Deque and java.util.List

Other Collections:

  • SearchTree implements java.util.Collection AVL balanced BST, pre/post/in/level-order traversal
  • TrieSet, implements java.util.Set Get word completions
  • TrieMap, implements java.util.Map Get word completions and map values
  • Graph extends RelationGraph, using RelationSet
    • f: node -> node
  • WeightedGraph extends RelationGraph, using WeightedRelationSet
    • g: node -> (node, weight)

Base Classes to Save Code:

  • AbstractBase provides basic toString(), hashCode() toArray() functions
  • AbstractCollection implements java.util.Collection
  • AbstractMap implements java.util.Map
  • IncompleteSet implements java.util.Set

Introducing Sequences

  • Sequence Generate generic sequences, with pipeline api for filtering with Predicates
  • PredicateFor Special predicates for use with filtering
  • SequenceOf Common sequences and for Project Euler
  • Functions Common scalar functions to apply across the whole sequence

Special Included Classes

  • FiniteAutomaton pattern matching with generics
    • union, intersection, kleenestar, complement, concatenation
  • FiniteAutomatonFactory build automatons from words
  • RelationSet implements Relation simple adjacency list
  • WeightedRelationSet implements WeightedRelation extends Relation
    • f: node -> weight
    • g: weight -> {node}

Helper Classes

  • IntLoop loop in a range, with a specific step, used with sequences
  • Range value range with generics, used with sequences
  • ScannerPlus For programming challenge practise, eliminate boilerplate typing!

Built With

Share this project:

Updates