You, a dotnetprogrammer, just joined a new project team. Except you, other members include: ceer, a programmer who has been engaged in C projects. He just transferred to C # for less than a month; jally, holding the design pattern all day (yes, it's the gof's) The former Java programmer, and semon, you have no idea about him, I just heard from PM that he was engaged in scheme (one of the legends of the second Ancient Language LISP ). But you don't care about it either. After all, computers are not sweet.
On Monday, when I started my computer, my boss ran to the office seat of your group and said, "Okay, guys, you need a function now. Specifically, you can enter two numbers and an operator. You can calculate the result based on the input. "
Example: Foo(+, 1, 2) = 3; Foo(*, 3, 6) = 18; Foo(/, 2, 4) = 0.5
Ceer is the first to respond: It's easy to judge the input operator. He quickly wrote the following code on the whiteboard:
public class CStyle_Calculator { static public double Foo(char op, double x, double y) { switch(op) case ’+’: return x + y; break; case ’-’: return x - y; break; case ’*’: return x * y; break; case ’/’: return x / y; break; default: throw new Exception(”What the Hell you have input?"); } }
Jally only read it once and shook his head with his nose covered: a bad smell of code. It is better to see that I use the OO method to solve the problem:
Public interface I operator // who says the Code cannot write Chinese? En {double operation (Double X, Double Y);} public class oo_calculator {private I operator m_op; Public oo_calculator (I operator OP) {This. m_op = op; // dependency injection [NOTE 2]} public double Foo (Double X, Double Y) {return this. m_op. operation (x, y) ;}} public class addition: I operator {public double operation (Double X, Double Y) {return x + y ;}} public class subtraction: I operator {public double operation (Double X, Double Y) {return x-y;} public class multiplication: I operator {public double operation (Double X, Double Y) {return x * y ;}} public class division: I operator {public double (Double X, Double Y) {return x/y ;}} public class themainclass {static public void main () {I operator my addition = new addition (); oo_calculator my addition operator = new oo_calculator (my addition ); double sum = My multiplier. foo (3, 4); system. console. writeline (SUM); // sum = 7 // I will not talk nonsense about the other three }}
You looked at jally and wrote the whiteboard in a dense manner. Then, you shrugged and sighed. You used Java waste, so it was annoying to use the interface tool. Let me show you how powerful DOTNET is. Isn't it good for me to directly use the delegate operator to transfer it in.
Public Delegate double theoperator (Double X, Double Y); public class operators {static public double add (Double X, Double Y) {return X + Y ;} static public double sub (Double X, Double Y) {return x-y;} // multiplication, division I am too lazy to talk nonsense} public class dotnet_calculator {public double Foo (theoperator op, double X, Double Y) {return OP (x, y) ;}} public class themainclass {static public void main () {theoperator myadd = new theoperator (operators. add); theoperator mysub = new theoperator (operators. sub); dotnet_calculator Dc = new dotnet_calculator (); double sum = dc. foo (myadd, 2, 4); // sum = 6 system. console. writeline (SUM); double sub = dc. foo (mysub, 3, 7); // sub =-4 system. console. writeline (sub );}}
// Under dot net, you can also use codedom to dynamically construct C # code and compile and run it in the memory.
// If it is annoying to write operators specifically, try the anonymous method C #2.0.
Well, after you finish writing the code and look at jally, ceer starts to complain: "Isn't that the function pointer in C? I will..."
"But the delegate under DOTNET is a type of security drops..." you continue to be proud ./
Semon looked at your three gorgeous codes and didn't say anything. I just typed two lines of code on the keyboard.
(define (Foo op x y) (op x y))
Then I got off work...
[Note: The scheme code is explained below: (+ 1 2) = 3, (* 3 4) = 12 .]
As for the semon solution:
(define (Foo op x y) (op x y))
The above code has only one function: the first line is the function header, which defines a function named Foo. This function accepts three parameters op, X, and Y.
The second row defines the function behavior: the first parameter op is used as an operator to calculate the next two parameters.
So :( Foo + 1 2) = 3. (FOO/12 6) = 2.
Okay, no more stories.
I just want to make everyone look at the wonderful world of function programming after busy work. Function programming, the biggest feature is that it treats functions as 1st class elements in the language. A function can take another function as a parameter, or return a function as a result. Such a function is called the higher-order function.
So what are the differences between function programming and our traditional object-oriented systems? Well, that's the difference between the Turing Machine and von noriman systems... @ _ @ but that is too academic, so I won't talk about it. I have time to get popular in the "Code Mystery" series of articles. However, a sentence can better summarize the difference between FP and OO (well, this is also the one on the "zipi book ):
"Pascal is to build the pyramid... LISP is used to build organisms... "" As the internal data structure of lisp, tables play an important role in improving this availability... "using 100 functions to operate on a single data structure is far better than using 10 operations to work on ten data structures." "The pyramid stands there for thousands of years, while the organism must evolve, otherwise it will die ".
The other well summarized statement is: (copied as well)
An object: A group of identical operations plus different data. (Think about your object, isn't it like this ?)
A closure: A group of identical data plus different operations. (Delegate is such an idea. If you are interested, you can check Ruby)
Basically, well, nothing to say. If you are interested, you can take a look at the MIT SiC course (with an online version, MIT is also set up as an Open Course course)
Link: http://www.cnblogs.com/linkcd/archive/2005/07/19/196087.html