dtonhofer

dtonhofer

Functional Programming in Java, Second Edition: p.62 - Notes on "ListSubDirs.java"

We read:

public static void betterWay() {
   List<File> files =
      Stream.of(new File(".").listFiles())
         .flatMap(file -> file.listFiles() == null ?
            Stream.of(file) : Stream.of(file.listFiles()))
         .collect(toList());
      System.out.println("Count: " + files.size());
}

One may note that there are two calls to file.listFiles(), which may or may not be a problem - the exercise to ameliorate that can be left to the reader.

More interestingly, I tried to rejiggle the if-the-else expression into

Function<File,Stream<File>> func1 =
    ((File file) -> 
      Stream.of((file.listFiles() == null) ? file : file.listFiles()));

rather than

Function<File,Stream<File>> func2 =
    ((File file) -> 
      file.listFiles() == null ? Stream.of(file) : Stream.of(file.listFiles()));

while func2 is what is used in the book, func1 is rejected by the Java 18 compiler (although in principle equivalent) becaus the type hole cannot be resolved. Looks like the ?: expressions presents some kind of problem.

Note that File[] array can be null in case the file does not exist or the file is not a directory.

Thus, properly:

    public void listSubDirsBetterWay() {
        File[] files = new File(theDir).listFiles();
        String res;
        if (files == null) {
            res = "Looks like '" + theDir + "' is not a directory";
        }
        else {
            Function<File,Stream<File>> func = ((File file) -> file.listFiles() == null ? Stream.of(file) : Stream.of(file.listFiles()));
            List<File> filesAndSubfiles = Stream.of(files).flatMap(func).collect(toList());
            res = "Count: " + filesAndSubfiles.size();
        }
        System.out.println(res);
    }

First Post!

dtonhofer

dtonhofer

I have posted another suggestion for chapter 3 with all the code changes, so this post can be disregarded.

Where Next?

Popular Pragmatic Bookshelf topics Top

jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
johnp
Hi Brian, Looks like the api for tinydb has changed a little. Noticed while working on chapter 7 that the .purge() call to the db throws...
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
herminiotorres
Hi! I know not the intentions behind this narrative when called, on page XI: mount() |&gt; handle_event() |&gt; render() but the correc...
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
nicoatridge
Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in p...
New
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
New
creminology
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New
mcpierce
@mfazio23 I’ve applied the changes from Chapter 5 of the book and everything builds correctly and runs. But, when I try to start a game,...
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

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1037 19435 386
New
AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
AstonJ
Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face: https://on...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
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
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: