• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Functional Programming in Java 8 -- For Venkat

 
Ranch Hand
Posts: 598
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Venkat,
It is nice to ask questions here

1. Is Functional Programming really necessary in Java 8?


2. Can you illustrate the advantages and pitfalls when coding in Java 8 using Functional Programming ?


3. Why do we need a transformation from old way of coding to Functional Programming ?


4. Can you illustrate Functional Programming in Java 8 with a working example. ?


Hope to win this book. !!


Regards
Deepak Lal
 
Author
Posts: 109
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepak,


> 1. Is Functional Programming really necessary in Java 8?

I think so. We've been programming with imperative style for way too long in Java.
Declarative style, expressiveness are some of the things that attracted me to other languages in the first place.
It is nice that we can create code with some of those very capabilities in Java. Those of us using Java don't
have to be deprived of those features any more.

> 2. Can you illustrate the advantages and pitfalls when coding in Java 8 using Functional Programming ?

I have mentioned some advantages in other posts, I will mention some of the pitfalls here.

Java has predominantly been imperative, OO, mutable state (or shared mutable state). FP is declarative and favors immutability.
We should use extra caution not to mix mutability, shared state, and functional style. The language offers a thin layer of protection,
but it's not hard to breach that. As programmers we should refrain from that temptation to mix these paradigms in an unhealthy manner.

> 3. Why do we need a transformation from old way of coding to Functional Programming ?

I touched on some of these here: Why FP

>4. Can you illustrate Functional Programming in Java 8 with a working example. ?

There are several examples out there, instead of repeating it here, if you would, please take a look at one example here:
Presentation from SpringOne2GX on Java 8

Thanks,

Venkat
 
Deepak Lal
Ranch Hand
Posts: 598
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Venkat,
Thanks for explanation of all above mentioned points.


Point 2 :Venkats comment :: Java has predominantly been imperative, OO, mutable state (or shared mutable state). FP is declarative and favors immutability.
We should use extra caution not to mix mutability, shared state, and functional style. The language offers a thin layer of protection,
but it's not hard to breach that. As programmers we should refrain from that temptation to mix these paradigms in an unhealthy manner.



We should use extra caution not to mix mutability, shared state, and functional style.-->Can you just give me a rough example on this statement


Regards,
Deepak Lal
 
Venkat Subramaniam
Author
Posts: 109
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We should use extra caution not to mix mutability, shared state, and functional style.-->Can you just give me a rough example on this statement


Here's one example, I've seen beginners of FP do this:



In the above code, from within the lamba expression we're mutating the ArrayList. This one is quite easy to fix.




In general we should avoid the temptation to mutate a variable from within a lambda expression. In other words,
we want to keep the lambda pure - as functions with no side effects.

Hope this answer your question.
 
Deepak Lal
Ranch Hand
Posts: 598
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Venkat,
Thank you for your explanation. Can i get to see such examples in your book http://www.amazon.com/gp/product/1937785467?tag=jranch-20

I would like to learn through Examples. rather than just reading a book for namesake.

Hope to win this book .

Regards
Deepak Lal
 
Venkat Subramaniam
Author
Posts: 109
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Lal wrote:... Can i get to see such examples in your book



Yes and you may also download the code examples from Source code from the book.
 
Deepak Lal
Ranch Hand
Posts: 598
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Venkat
Thanks for the Source code.
Keeping fingers crossed to own a copy of this book through Java Ranch Promotion.
Regards
Deepak Lal
reply
    Bookmark Topic Watch Topic
  • New Topic