Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Lambda FizzBuzz  RSS feed

 
Scott Shipp
Ranch Hand
Posts: 217
11
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for fun.

 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It works
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is so dreadful it merits a cow.
 
Scott Shipp
Ranch Hand
Posts: 217
11
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yay! Thank you! I have five cows now!
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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: 217
11
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Stephen Bloch
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15861
81
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See also our FizzBuzz topic in the Meaningless Drivel forum.
 
Gordon Brown
Ranch Hand
Posts: 58
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's certainly as concise an implementation of FizzBuzz as I've seen in Java.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, it can be further shortened by eliminating redundant parentheses and braces, and using a method reference.
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This solution makes heavy use of my imaginary ConditionallyMappingIntToObjectStream class.
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 6980
110
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!