Welcome to Dream.In.Code
Become a Java Expert!

Join 150,422 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,103 people online right now. Registration is fast and FREE... Join Now!




Java generics and functors

 
Reply to this topicStart new topic

Java generics and functors

Shuyin
3 Apr, 2008 - 04:50 AM
Post #1

New D.I.C Head
*

Joined: 17 Sep, 2007
Posts: 1


My Contributions
Hello,

This is a lab question I'm unsure on.

I have a number of methods each performing a single operation on a number, for example : Switching a sign, applying a sine function or applying a square root.

If I wanted to perform something like the minus of the square root of a sine of a number then how would I replace all these methods with a single method which takes a parameter as a functor. I'm guessing the functor is another method with what we're applying to a number (like the example above)?

I could really do with some help here, thank you in advance :-)

Shuyin
User is offlineProfile CardPM
+Quote Post

pbl
RE: Java Generics And Functors
3 Apr, 2008 - 05:09 AM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,591



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(Shuyin @ 3 Apr, 2008 - 05:50 AM) *

Hello,

This is a lab question I'm unsure on.

I have a number of methods each performing a single operation on a number, for example : Switching a sign, applying a sine function or applying a square root.

If I wanted to perform something like the minus of the square root of a sine of a number then how would I replace all these methods with a single method which takes a parameter as a functor. I'm guessing the functor is another method with what we're applying to a number (like the example above)?

I could really do with some help here, thank you in advance :-)

Shuyin


Easy in C and C++ but not in Java. For security reason you do not want a malicious user or hacker to pass on a custom made function/method to another function expecting one of the standard functions/method.

As there is no arithmetic on pointers in Java there are no pointer to method in Java.

This post has been edited by pbl: 3 Apr, 2008 - 05:12 AM
User is online!Profile CardPM
+Quote Post

1lacca
RE: Java Generics And Functors
3 Apr, 2008 - 05:31 AM
Post #3

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
The reflection API is your friend, and the Methodclass.
User is offlineProfile CardPM
+Quote Post

bhandari
RE: Java Generics And Functors
4 Apr, 2008 - 12:36 AM
Post #4

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
but remember avoid reflection API as its behaviour effects the program at runtime. Not recommended for good commercial apps.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Java Generics And Functors
4 Apr, 2008 - 01:37 AM
Post #5

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Hahaha, back to school smile.gif But this is Sparta!
Anyway, I am not really sure about the problem, but it seems to me that using some interfaces it could be solved without using method pointers.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:26PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month