• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Lambda FizzBuzz

 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for fun.

 
Marshal
Posts: 73994
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It works
 
Campbell Ritchie
Marshal
Posts: 73994
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is so dreadful it merits a cow.
 
Scott Shipp
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yay! Thank you! I have five cows now!
 
Campbell Ritchie
Marshal
Posts: 73994
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott Shipp wrote:Just for fun.



And some people think that regular expressions are unmaintainable !
 
Scott Shipp
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just curious, what trips you up more, the chained method calls or the use of the ternary operator over and over?

The way lambdas use the chained method calls reminds me of Fowler's fluent interfaces and I kinda like it now that I am getting familiar with the different methods.

I won't defend the ternary usage though, lol.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never really liked the ?: ternary operator syntax, but the last time I taught CS1, I used a predominantly functional paradigm and it was natural to introduce ?: before if/else. Later on in the semester, when we had both features, we found that whenever we converted an if/else to a ?:, the code became significantly shorter and often clearer.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See also our FizzBuzz topic in the Meaningless Drivel forum.
 
Ranch Hand
Posts: 58
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's certainly as concise an implementation of FizzBuzz as I've seen in Java.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, it can be further shortened by eliminating redundant parentheses and braces, and using a method reference.
 
Saloon Keeper
Posts: 13255
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This solution makes heavy use of my imaginary ConditionallyMappingIntToObjectStream class.
 
Stephan van Hulst
Saloon Keeper
Posts: 13255
292
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No longer imaginary!
Filename: ExtendedIntStream.java
File size: 5 Kbytes
Filename: ConditionallyMappingIntToObjectStream.java
File size: 3 Kbytes
 
Gordon Brown
Ranch Hand
Posts: 58
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very interesting, Stephan. Will try to wrap my head around that code at some point soon.
 
Stephan van Hulst
Saloon Keeper
Posts: 13255
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ExtendedIntStream is just a class to add my custom pipeline operations to the IntStream class. I did this once before with the Stream class so I could add zip() and zipWith() operations.

ConditionallyMappingIntToObjectStream just stores a list of predicates, and an associated list of mapping functions. The elseMap() or elseFilter() method return a Stream which, when a terminal operator is invoked, will map each int using a different mapper, depending on which predicate tests as true for that element, or filters the element out if no predicates test true.

I didn't focus on code clarity, but on just proving it could work, so when I clean it up I'll repost the code.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic