dtonhofer

dtonhofer

Functional Programming in Java, Second Edition: Code for "Chapter 7, Ininite Collections" in two files

SimpleTimer.java

package chapter7;

import java.util.function.Supplier;

public abstract class SimpleTimer {

    public record Result<T>(long duration_ms, T result) {}

    public static <T> chapter7.SimpleTimer.Result<T> timeIt(Supplier<T> supplier) {
        long start = System.currentTimeMillis();
        T result = supplier.get();
        long stop = System.currentTimeMillis();
        return new Result<T>(stop - start, result);
    }
}

BeingLazy_InfiniteCollections.java

package chapter7;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class BeingLazy_InfiniteCollections {

    // If CPU cycles are of no importance...
    // "a desparate attempt", p.133

    public static boolean isPrimeVeryGorilla(final int number) {
        return number > 1 &&
                IntStream.rangeClosed(2, (int) Math.sqrt(number))
                        .noneMatch(divisor -> number % divisor == 0);
    }

    // This doesn't work as it causes an infinite recursion
    // "a desparate attempt", p.134, but with different method & parameter names

    public static List<Integer> primesUpwardsFrom_notworking(final int number) {
        if (isPrimeVeryGorilla(number)) {
            List<Integer> morePrimes = primesUpwardsFrom_notworking(number + 1);
            morePrimes.add(0, number);
            return morePrimes;
        } else {
            return primesUpwardsFrom_notworking(number + 1);
        }
    }

    // "lazy/fpij/Primes.java", p.135, but with different method & parameter names

    private static int firstPrimeAfter(final int number) {
        if (isPrimeVeryGorilla(number + 1)) {
            return number + 1;
        } else {
            return firstPrimeAfter(number + 1);
        }
    }

    // "lazy/fpij/Primes.java", p.135, but with different method & parameter names

    private static List<Integer> primesUpwardsFrom(final int fromNumber, final int count) {
        return Stream.iterate(firstPrimeAfter(fromNumber - 1), BeingLazy_InfiniteCollections::firstPrimeAfter)
                .limit(count)
                .collect(toList());
    }

    // Testing primesUpwardsFrom()
    // https://en.wikipedia.org/wiki/List_of_prime_numbers
    // https://oeis.org/A000040

    @Test
    public void generatePrimes() {
        final var shall = List.of(101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
                179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281);
        final List<Integer> primesAfter100 = primesUpwardsFrom(100, shall.size());
        final String txt = primesAfter100.stream().map(it -> Integer.toString(it)).collect(Collectors.joining(",\n"));
        System.out.println(txt);
        assertEquals(shall, primesAfter100);
    }

    // Testing primesUpwardsFrom()
    // Similar to "lazy/fpij/Primes.java" on p.136

    @Test
    public void runBookPrimes() {
        List<Integer> l1 = primesUpwardsFrom(1, 10);
        List<Integer> l2 = primesUpwardsFrom(100, 5);
        assertEquals(List.of(2, 3, 5, 7, 11, 13, 17, 19, 23, 29), l1);
        assertEquals(List.of(101, 103, 107, 109, 113), l2);
        System.out.println("10 primes from 1: " + l1);
        System.out.println("5 primes from 100: " + l2);
    }


    // It's quite slow!

    @Test
    void runBookPrimesWithTimer() {
        final int limit = 1_000_000;
        // not an actual test, just printout
        final SimpleTimer.Result<List<Integer>> stRes =
                SimpleTimer.timeIt(
                        () -> primesUpwardsFrom(2, limit)
                );
        final List<Integer> result = stRes.result();
        final String range = (result.isEmpty()) ? "" : "in range [" + result.get(0) + "," + result.get(result.size() - 1) + "] ";
        System.out.println("Using book code: finding " + result.size() + " primes " + range + "took " + stRes.duration_ms() + " ms");
    }

}

Where Next?

Popular Pragmatic Bookshelf topics Top

belgoros
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84: bundle exec sequel...
New
jeffmcompsci
Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list) Typo:...
New
Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New
cro
I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New
leonW
I ran this command after installing the sample application: $ cards add do something --owner Brian And got a file not found error: Fil...
New
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
New
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
s2k
Hi all, currently I wonder how the Tailwind colours work (or don’t work). For example, in app/views/layouts/application.html.erb I have...
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New

Other popular topics Top

brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
AstonJ
If you are experiencing Rails console using 100% CPU on your dev machine, then updating your development and test gems might fix the issu...
New
PragmaticBookshelf
Create efficient, elegant software tests in pytest, Python's most powerful testing framework. Brian Okken @brianokken Edited by Kat...
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
PragmaticBookshelf
Programming Ruby is the most complete book on Ruby, covering both the language itself and the standard library as well as commonly used t...
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
sir.laksmana_wenk
I’m able to do the “artistic” part of game-development; character designing/modeling, music, environment modeling, etc. However, I don’t...
New
NewsBot
Node.js v22.14.0 has been released. Link: Release 2025-02-11, Version 22.14.0 'Jod' (LTS), @aduh95 · nodejs/node · GitHub
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: