Coder Social home page Coder Social logo

12-functional-cli's Introduction

This is an assignment to the class Programmieren 3 at the University of Applied Sciences Rosenheim.

Assignment 12: Functional - CLI variant

This assignment covers the basics of the Java 8 Stream API. Streams are a functional concept and are relatively new in Java but they are very useful in combination with all kinds of iterable data sources. Streams may look a little bit complicated but when you got the concept they improve the readability of your code because they are more data pipeline oriented as normal Java code is where you always have to iterate a collection with any kind of loop.

Setup

  1. Create a fork of this repository (button in the right upper corner)
  2. Clone the project (get the link by clicking the green Clone or download button)
  3. Import the project to your IntelliJ
  4. Read the whole assignment spec!

Remark: the given test suite is incomplete but will succeed after the checkout.

Generators

A generator is component that creates a Stream. There are two kinds of streams:

  • finite
  • infinite

A stream based on a list of objects is a finite stream as there are only a discrete number of elements that can be iterated.

Infinite streams are seaming to be a little bit weird but they are also very useful. Think of a pseudo random number generator. An infinite stream may be used to produce as many random numbers as required. This generator may be implemented like this:

public abstract class PseudoRandomNumberGenerator {

    private PseudoRandomNumberGenerator() {
    }

    public static Stream<Integer> createStream() {
        return Stream.generate(new PseudoRandomNumberSupplier());
    }

    private static class PseudoRandomNumberSupplier implements Supplier<Integer> {

        private final Random random = new Random();

        @Override
        public Integer get() {
            return random.nextInt();
        }
    }
}

The stream may be used like this:

Stream<Integer> prngStream = createStream();
prngStream
        .limit(10)
        .forEach(System.out::println);

Note that the limit(...) operation is mandatory because the stream is infinite and otherwise the whole operation will not terminate!

In this part of the assignment you have to implement two generators as shown in the following UML:

Generator spec

Remark: the UML is incomplete and is meant as implementation hint!

  1. Implement the RandomJokeSupplier - this supplier returns a random joke every time it is used.
  2. Implement the AllJokesSupplier - this supplier iterates all jokes in an infinite loop i.e. if all jokes were retrieved it continues with the first joke.
  3. Implement the JokeGenerator - the generator returns infinite streams based on the implemented suppliers.
  4. Complete the test suite to ensure that your generators are working correctly!

Note that the class structure is already there (including the empty unit tests). You may also implement the unit tests after the next part of the assignment.

Using the generators

The given class App already implements a basic CLI interface. To complete the assignment implement the required code marked with a TODO.

The following flow diagram explains how to use the jokesSource:

Stream flow

Every chart element corresponds to a single method call on the stream of jokes. For further reading about the Java 8 streams have a look at this article.

Remark: this part is technically an one-liner. To improve the readability add line breaks after every stream transformation. That should result in 5-6 lines of code.

If you want to improve your knowledge about streams you could extend the assignment by asking the user if he wants to filter the jokes for a specific category and if so which category (read the category as string). Then filter the stream after the unwrap transformation for the chosen category.

12-functional-cli's People

Contributors

prskr avatar

Watchers

James Cloos 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.