Git Product home page Git Product logo

java8-katas's Introduction

java8-functional-Katas

Originally forked from https://github.com/zupzup/java8-functional-katas but needed to move to my separate repo since the owner didn't review my PR


Some Katas for functional programming in Java 8 for a coding dojo (loosely based on http://reactive-extensions.github.io/learnrx/ and converted to Java)

Just read the instructions in the Kata and try to finish them. You can also use TDD if you want - there are dummy tests for all Katas.

Katas

  • Kata 1: simple projection using map
  • Kata 2: filter() and map()
  • Kata 3: using map() and flattening the array using flatMap()
  • Kata 4: a simple query using map(), filter() and flatMap()
  • Kata 5: simple example using reduce()
  • Kata 6: map() and reduce() combined
  • Kata 7: more advanced queryin
  • Kata 8: combining collections using zip()
  • Kata 9: Complex Query
  • Kata 10: Transforming a data structure
  • Kata 11: Transforming a more complex data structure

Workshop

public class Exercise1 {
    public static void main(String[] args) {

        // Use StreamSources.intNumbersStream() and StreamSources.userStream()

        // Print all numbers in the intNumbersStream stream
        //TODO: Write code here
        StreamSources.intNumbersStream().forEach(System.out::println);
        System.out.println("---------------------------------------");


        // Print numbers from intNumbersStream that are less than 5
        //TODO: Write code here
        StreamSources.intNumbersStream().filter(integer -> integer < 5).forEach(System.out::println);
        System.out.println("---------------------------------------");


        //Print the second and third numbers in intNumbersStream that's greater than 5
        //TODO: Write code here
        StreamSources.intNumbersStream().filter(integer -> integer > 5)
                //we skip the 0 from 0, 2,4
                .skip(1)
                //and we take last two elements after first element so those will be the second and the third
                .limit(2).forEach(System.out::println);
        System.out.println("---------------------------------------");


        // Print the first number in intNumbersStream that's greater than 5
        // If nothing is found, print -1
        //TODO: Write code here
        Integer result = StreamSources.intNumbersStream()
                .filter(integer -> integer > 5)
                .findFirst()
                .orElse(-1);
        System.out.println(result);
        System.out.println("---------------------------------------");


        // Print firstNames of all users in userStream
        //TODO: Write code here
        StreamSources.userStream()
                .map(user -> user.getName())
                .forEach(firstName -> System.out.println(firstName));
        System.out.println("---------------------------------------");


        //Combining 2 streams
        //Print surnames in userStream for all the users those have IDs from the numberSteam
        //TODO: Write code here

        //Bad way
        StreamSources.intNumbersStream()
                .flatMap(id -> StreamSources.userStream().filter(user -> user.getId() == id))
                .map(user -> user.getSurname())
                .forEach(surname -> System.out.println(surname));

        System.out.println("---------------------------------------");

        //Better way
        StreamSources.userStream()
                .filter(user -> StreamSources.intNumbersStream().anyMatch(id -> user.getId() == id))
                .map(user -> user.getSurname())
                .forEach(System.out::println);

    }
}

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.