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

jeffmcompsci
Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list) Typo:...
New
ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
New
brian-m-ops
#book-python-testing-with-pytest-second-edition Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
New
fynn
This is as much a suggestion as a question, as a note for others. Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
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
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New

Other popular topics Top

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
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
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
PragmaticBookshelf
Explore the power of Ash Framework by modeling and building the domain for a real-world web application. Rebecca Le @sevenseacat and ...
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
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: