Please create an account to participate in the Slashdot moderation system

 



Forgot your password?
typodupeerror
User Journal

Journal Com2Kid's Journal: Why functions as first class objects rocks. (C#) 5

Ok technically for the sake of type safety they are delegates, but the COOL part is that this:


z1 = z0 - ( f(z0) / f'(z0) )

math formula ends up looking like this:


newResult = previousResult - ( f(previousResult) / fprime(previousResult) );

code.

That is awesome in terms of sheer readability, compared to the insane amount of crud I'd have to go through to write that in Java.

This discussion has been archived. No new comments can be posted.

Why functions as first class objects rocks. (C#)

Comments Filter:
  • I am intrigued. Can you show some more code so I understand how it actually works?
    • by Com2Kid ( 142006 )
      No problem. :-D

      I need to divide the results of two functions f(x) and f`(x), in other words, find f(x)/f`(x). The user can select f(x) and f`(x) from 3 different choices.

      1. f(x) = e^x - 2 - x
      2. f(x) = (x-2)2 - ln(x)
      3. f(x) = cos(x) + 1 - x

      (f(x) is chosen and f`(x) is automatically paired with it)

      I have a function, solver, that takes in two other functions as its parameters. To do this in C#, I create a delegate type. A delegate just tells .NET what the return type and parameter types of a set of re

  • Too bad most Real Programming jobs don't consist of writing mathematical formulas. Delegates are only really useful in C# for Win GUI forms.
  • I really don't see any reason why you can't do something similar in Java just as easily.
    • by Com2Kid ( 142006 )
      You're right, this is all just syntactic sugar.

      In Java, I could make an abstract base class called MathFunction that had a single unimplemented method in it. I could then derive three concrete classes from this base class, each with a different implementation of that function. Then declare my variables to be of the base class type, and assign the more proper specific subtype to that initial variable.

      But that is a lot of work for one lonely little function. :-D Not to mention I then end up going

      MathFuncti

If I had only known, I would have been a locksmith. -- Albert Einstein

Working...