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

brianokken
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
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
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex&gt; product = %Product{} %Pento....
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
gilesdotcodes
In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions: In Gemfile, change gem 'rails' t...
New
swlaschin
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New
New
AufHe
I’m a newbie to Rails 7 and have hit an issue with the bin/Dev script mentioned on pages 112-113. Iteration A1 - Seeing the list of prod...
New
kolossal
Hi, I need some help, I’m new to rust and was learning through your book. but I got stuck at the last stage of distribution. Whenever I t...
New
EdBorn
Title: Agile Web Development with Rails 7: (page 70) I am running windows 11 pro with rails 7.0.3 and ruby 3.1.2p20 (2022-04-12 revision...
New

Other popular topics Top

DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
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 Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New
AstonJ
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: