Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Implementing a simple higher order function in Java

 
Ranch Foreman
Posts: 1833
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, we can use higher order functions like Map,Reduce,Filter. I am trying to create our own higher order function as a simple example.How can I complete the below code for a simple basic example of higher order function ?thanks


 
   
 
Saloon Keeper
Posts: 12436
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just call a method of your myParam parameter.

Why did you give your class generic type parameters? Why did you not parameterize the type of your myParam parameter?
 
Monica Shiralkar
Ranch Foreman
Posts: 1833
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Editing code to remove the generic type
 
Monica Shiralkar
Ranch Foreman
Posts: 1833
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You just call a method of your myParam parameter.



What does "method of myParam" mean ?
 
Monica Shiralkar
Ranch Foreman
Posts: 1833
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Why did you not parameterize the type of your myParam parameter?



What does "parameterize the type of your myParam parameter" mean?
 
Monica Shiralkar
Ranch Foreman
Posts: 1833
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying like below but requires correction :



 
Stephan van Hulst
Saloon Keeper
Posts: 12436
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:What does "method of myParam" mean ?


myParam refers to an object. You can call methods on objects, right?

Monica Shiralkar wrote:What does "parameterize the type of your myParam parameter" mean?


myParam is of type Function. Function is a generic type, so you should tell it what type arguments you want to use with it. In your original code you were using it as a raw type which you should avoid.

Monica Shiralkar wrote:


Have you ever encountered code like this in Java? It's not valid. For Function, you must specify what type the function expects and what type it returns. If it doesn't return anything, then it's not a Function and you must use a Consumer instead.
 
Monica Shiralkar
Ranch Foreman
Posts: 1833
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would that mean something like below ?



 
Monica Shiralkar
Ranch Foreman
Posts: 1833
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now the code is compiling but on running the lambda is not doing anything.

 
Stephan van Hulst
Saloon Keeper
Posts: 12436
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because your higher order function is not doing anything with myParam.
 
Bartender
Posts: 4109
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in

you do nothing with 'myParam", as Stephan remarks. But if you do, then you would get something like

So your method should be like:

By making the method static, you do not need an instance of your class.
 
Monica Shiralkar
Ranch Foreman
Posts: 1833
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all.

I came up with the below basic code which is producing results now :



It produces result testHello.

I am not clear on the below line


And that why should it be happening inside the myMethod and not at the time where we actually use the lambda by calling .



 
Stephan van Hulst
Saloon Keeper
Posts: 12436
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A higher order function is a function that uses a function that you pass to it. Why else would you pass a function if you weren't going to use it?
 
Monica Shiralkar
Ranch Foreman
Posts: 1833
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.Understood.So, in my case myMethod will apply the lambda I am passing to x.
 
Stephan van Hulst
Saloon Keeper
Posts: 12436
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it applies the function to the hardcoded string "test".

'x' is just the name of the parameter used in the lambda expression that you pass to your higher order function. Nothing is applied to it. In your lambda expression, you apply concat("Hello") to the value it references, which always happens to be "test".
 
Monica Shiralkar
Ranch Foreman
Posts: 1833
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I understand is that in my example x is the hard coded  "test". Is it not correct ?
 
Stephan van Hulst
Saloon Keeper
Posts: 12436
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x is a variable that will reference the hardcoded string "test". It is not the string "test" itself. It is important to be precise when it comes to programming.
 
Monica Shiralkar
Ranch Foreman
Posts: 1833
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:x is a variable that will reference the hardcoded string "test".


Yes.
 
Monica Shiralkar
Ranch Foreman
Posts: 1833
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote: It is important to be precise when it comes to programming.



Thanks
 
What are you doing? You are supposed to be reading this tiny ad!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic