• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

An Example of Java 8's Functional Style Programming

 
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This article is an example of Java 8's functional style programming compared to pre Java 8 code. The code is run from Java 9's JShell REPL tool. There is also code of this example using a real functional programming language. Here is the link to the article...

Prasad Saya
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no attribution on that blog post. Did you write it?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's an interesting article Please explain what JShell is. Is it some sort of interpreter where you can enter a bit of code and have it interpreted directly? Does JShell help with developing and testing code.

I shall add this discussion to the “new features” fora in the hope of more people reading it.
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for viewing my post.

@Tim Cooke
Yes, I wrote the post.

@Campbell Ritchie
JShell is a new Java feature with Java 9, which was released recently. I only downloaded Java 9 couple of days back and started using it (for this post), and I found JShell quite easy to use and useful. I haven't explored it yet to make any comments other than what's mentioned in my post. As you had mentioned its a sort of interpreter where one can enter a bit of code (declarations, statements, and methods) and have it interpreted directly. It helps developing and testing code quickly.

I thought it would be interesting to use the JShell in my post though its not part of Java 8. The Haskell functional programming has a similar REPL (Read-Evaluate-Print Loop) tool, and its been there since the very earlier versions of Haskell language (how early, I see it sometime before year 2000). I have been learning Haskell for couple of months, since I got my Java 8 certification recently. I found the functional programming concepts interesting and kind of randomly selected Haskell to know more about a functional programming language. The Haskell's GHCi REPL tool is very useful while learning Haskell and I got this idea to use in the post.

Prasad.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. JShell looks even more interesting now.
I haven't used Haskell, but people who have speak well of it.
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been learning Haskell for couple of months, I somehow feel 'nice' to know about the language (just a feeling). I also am enjoying learning Haskell as I don't have particular goals about it. Its easy to grasp the language if one knows the functional-style programming of Java 8. There are quite a few features in Java which can be related to Haskell like, lambdas, streams, filter/map/reduce functions, something like interfaces, stateless functional programming, generic types, data types,  data structures (like lists, sets and maps),... But, getting used to functions, one has to persist. And moreover there is no for loop in Haskell and values cannot be reassigned to a variable.

I think knowing a real functional programming gives a deeper insight into the Java 8's functional-style features.

There are a lot of online material to learn Haskell; I started with the following and these definitely are a good starting point, for anyone who chooses.

http://learnyouahaskell.com/ -- An online tutorial (also available as a purchaseable book)
book.realworldhaskell.org/ -- An online tutorial (also available as a purchaseable book)
https://en.wikibooks.org/wiki/Haskell --  A downloadable wikibook for Haskell language
https://www.haskell.org/tutorial/ -- A gentle introduction to Haskell 98
 
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess learning the basics of Haskell years ago as helped with my Java skills; never thought I would say this. I haven't used Haskell since then, but with the functional style being imported into Java 8, I would have to.
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Programming in Haskell, by Graham Hutton is another book I referred to recently. It's more like an academic text book (if someone likes that style) with exercises at end of each chapter. The second edition has the recent version of Haskell is good for learning.
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain what is the difference between:

Stream.of(inputString.split(" "))
vs
Arrays.stream(inputString.split(" "))

And why did you choose one over another in your blog post?
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The inputString.split(" ") method returns an array of strings. This is same as: String [] result = inputString.split(" ");

In the example, any of the two methods usage returns the same output. But, there are some differences in the two methods. Here are the definitions of the two stream methods and examples.

Version One:

@SafeVarargs
static <T> Stream<T> of(T... values)

Returns a sequential ordered stream whose elements are the specified values.

EXAMPLES:


Version Two:

static <T> Stream<T> stream(T[] array)
Returns a sequential Stream with the specified array as its source.

EXAMPLES:

Note that the method Arrays.Stream method is overloaded to take various primitive arrays or a type array to return streams as in the examples above. These methods are capable of returning primitive streams (e.g., IntStream).  Arrays class also has variations of this method to select a range of elements from the input array to create the stream.

Both the method versions return sequential streams. But, the Stream class's method input must be an object type as input, and the following code will not compile on line 2:

Also, note that in the first version the stream method allows varargs as its arguments. This allows elements, as shown in the above version one example, can be entered without creating an array explicitly.

And why did you choose one over another in your blog post?


I really don't recollect why I did that. I think it means same output in this case.

Thanks for reading the article.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. You'll have to take care of cattle
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic