4 Replies - 3219 Views - Last Post: 04 April 2008 - 02:37 AM Rate Topic: -----

#1 Shuyin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-September 07

Java generics and functors

Posted 03 April 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

Is This A Good Question/Topic? 0
  • +

Replies To: Java generics and functors

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java generics and functors

Posted 03 April 2008 - 06:09 AM

View PostShuyin, on 3 Apr, 2008 - 05:50 AM, said:

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: 03 April 2008 - 06:12 AM

Was This Post Helpful? 0
  • +
  • -

#3 1lacca   User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Java generics and functors

Posted 03 April 2008 - 06:31 AM

The reflection API is your friend, and the Methodclass.
Was This Post Helpful? 0
  • +
  • -

#4 bhandari   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

Re: Java generics and functors

Posted 04 April 2008 - 01:36 AM

but remember avoid reflection API as its behaviour effects the program at runtime. Not recommended for good commercial apps.
Was This Post Helpful? 0
  • +
  • -

#5 1lacca   User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Java generics and functors

Posted 04 April 2008 - 02:37 AM

Hahaha, back to school :) 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1