Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Functional vs Procedural - Lambda Expressions

 
Ranch Hand
Posts: 138
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
while checking the new feature of Lambda expressions, I am trying to understand

1. Is the functional programming in Java introduced only through Lambda expressions ?

2. From the definition of Functional programming and Procedural Programming. If a sequence of steps that take some inputs and process them and give an output, without depending on the state of the object is a Function. Then can I say a static utility method in my application be treated as function ? . Where the Object Oriented paradigm fits in this Functional and Procedural programming languages ? Can a Object Oriented language be a functional programming language ?

Sorry for the confusion in the question.. but I am totally confused with Functional vs Procedural and what Lambda expressions going to achieve ...

Can someone throw some light..
 
Bartender
Posts: 4001
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Bala,

difficult questions, but I'll give it a shot.

1) No, what you can do with java 8 you could also do in java < 8. For instance, look at this code snippet:

(Note: I seem to have thrown out all my Java < 8 platforms, so I could test this snippet only on java 8. Can you check if the override of BalaFunction is legal on java 7?

So lambdas just make it look like functional programming. But whether I'm right or wrong, lambdas sure make life a lot easier!

2) Is it possible that such a static method depends on a static mutable variable in the class? Can you elaborate why you think that OOP and FP are contradictory?
 
Saloon Keeper
Posts: 12141
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bala Gangadhar wrote:From the definition of Functional programming and Procedural Programming.


What are the definitions of functional and procedural programming?
 
Bala Tilak
Ranch Hand
Posts: 138
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Bala,

difficult questions, but I'll give it a shot.

1) (Note: I seem to have thrown out all my Java < 8 platforms, so I could test this snippet only on java 8. Can you check if the override of BalaFunction is legal on java 7?



I got it, If I am not wrong, Its like creating an anonymous class for a interface and passing the object across the methods. This is made lot easier with Lambda. I tried with Compactor. Thanks it gave me better understanding of usages of Lambda expressions.

Piet Souris wrote:

2) Is it possible that such a static method depends on a static mutable variable in the class? Can you elaborate why you think that OOP and FP are contradictory?


Answer is No. I meant, a utility method lets say, Date format utility method, that takes a date string and returns the Date object .
 
Bala Tilak
Ranch Hand
Posts: 138
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Bala Gangadhar wrote:From the definition of Functional programming and Procedural Programming.


What are the definitions of functional and procedural programming?



I've never seen this definition given elsewhere, but I think this sums up the differences given here fairly well:

Functional programming focuses on expressions

Procedural programming focuses on statements

Expressions have values. A functional program is an expression who's value is a sequence of instructions for the computer to carry out.

Statements don't have values and instead modify the state of some conceptual machine.



I got it from this discussion https://stackoverflow.com/questions/23277/what-is-the-difference-between-procedural-programming-and-functional-programming

Can you explain me Functional Vs Procedural and how these fits in OOO.

 
Stephan van Hulst
Saloon Keeper
Posts: 12141
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think those summaries are way too simplistic. For instance, they completely ignore the fact that an important aspect of functional programming is that functions are first-class citizens that can be passed as arguments to higher order functions. Indeed, I would say that's the defining feature of functional languages, not that they consist only of expressions. I can write a program in Java that consists of one big expression without ever passing a function as an argument. Is that functional? I wouldn't say so.

Phrases like "functional", "procedural" and "object oriented" just imply that a language has certain features. I don't think they work well as mutually exclusive categories that a language can fall under. A language that bundles data and (high-order) functions together into immutable objects, but that doesn't allow you to call statements with side-effects, I would call that a functional object oriented language. It's not procedural.

Java has object oriented features, procedural features and functional features. It's also statically typed, has reflection, has generics, etc. Why do we call a language "functional" but rarely "reflective"?
 
Stephan van Hulst
Saloon Keeper
Posts: 12141
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bala Gangadhar wrote:If a sequence of steps that take some inputs and process them and give an output, without depending on the state of the object is a Function.


I would go beyond that. An instance method of an object is also a pure function, as long as it only depends on the method parameters and immutable and constant fields of the enclosing class. So a function can depend on object state. An example:

Why would the toString() method in the following Java example not be considered functional:

When the following code in Haskell clearly is:

Is it because the title and author functions explicitly take a Book argument?

Remember that underneath, instance methods in Java are really just static methods that have a hidden parameter that accepts a reference to the current object. You may be surprised, but the following is actually legal Java code:
 
Stephan van Hulst
Saloon Keeper
Posts: 12141
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To drive my point home:
Versus:

Not so different eh?
 
Greenhorn
Posts: 17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bala,

your questions are some how confusing. But don't worry it took me also a little time to understand this concept. So I'll try to give you an explanation

1- Is the functional programming in Java introduced only through Lambda expressions ?

functional programming was introduced in java 8 through Functional Interfaces and you can use lambda expressions in the context of a functional interface. So far with the technical explanation. In a simple way, I could explain functional programming as the possibility to inject code  or behaviour in methods. For example let's suppose you have a list of apples and you want to classify them by color . You could write something like this:


Now suppose you want to classify them by weight. You could then write something like this:



Herre comes the functional programming. You could  write a generic method and inject behavior in the code by writing a  function like this.

So from now you could call the generic method by injecting code, or an anonymous function or a lambda expression in context of the functional interface Predicate like this:


2- to answer your 2nd question in  a very very quick nutshell:
C is a pure procedural language
Java is a pure object oriented language
Since Java 8, Java became an object oriented language wich allows functional programming.

Hope I could clear the little confusion






 
Stephan van Hulst
Saloon Keeper
Posts: 12141
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pierrot, what makes a programming language a "pure object oriented language"?

I also agree with Piet. Java already allowed a degree of functional programming before Java 8, by passing objects that represent operations (usually in the form of anonymous classes). This is inherent to all object oriented languages.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First-Java id NOT pure OOP because there is so many concept that are missing for ex multi Inheritance in class
also we have the Primitive data type
(I know that primitive data type are classes as well but they are not applicable for a programmer  )
Second-The main Objective of Lambda Expression is to bring benefit of functional Programming and to reduce the length of the code so i disagree on the saying that java prior to 8 is functional programming about Functional interfaces  the main reason we have functional interface is to invoke lambda function because Lambda function is an anonymous  function  NB in a Functional interface you could have only
1- abstract method
2- any number of static Method
3- any number of default method
4 - in java 9 we could add as well any number of private Method
    Bookmark Topic Watch Topic
  • New Topic