Erlang Central

Erlang Funs and C Sharp

Revision as of 19:24, 12 June 2007 by AntonettaFarnsworth475 (Talk | contribs)

mtv ringtones cheap cialis lisinopril online nokia ringtones paxil online cheap didrex ultracet online free sonyericsson ringtones free mono ringtones buy valium diazepam clomid online fioricet online paxil online free kyocera ringtones mp3 ringtones free mp3 ringtones alprazolam online free alltel ringtones alprazolam online buy lipitor buy hydrocodone verizon ringtones levitra tramadol online buy hydrocodone free sprint ringtones xanax online free real ringtones tracfone ringtones propecia online ativan online samsung ringtones cheap tramadol buy didrex cheap clomid cheap lorazepam samsung ringtones cheap albuterol zyban online order soma but clonazepam cheap paxil free real ringtones sildenafil online soma online online ambien order wellbutrin cheap adipex buy propecia free real ringtones diethylpropion online polyphonic ringtones cheap rivotril jazz ringtones but lipitor cheap lorazepam cheap prozac order carisoprodol free polyphonic ringtones tenuate valium online free polyphonic ringtones lipitor online free ringtones phentermine online sagem ringtones free free ringtones buy ultracet buy ortho buy vigrx online nexium buy meridia sharp ringtones music ringtones diazepam online carisoprodol online but wellbutrin meridia online free free ringtones xanax online mtv ringtones fioricet online mp3 ringtones nextel ringtones cheap adipex cheap xanax sagem ringtones diazepam online cheap clonazepam free polyphonic ringtones cheap carisoprodol buy wellbutrin free kyocera ringtones ativan online order viagra buy lorazepam ultram online free kyocera ringtones cheap alprazolam alltel ringtones ambien online cheap vicodin motorola ringtones free sony ericsson ringtones free punk ringtones buy viagra but phentermine cheap xanax ultram online cheap valium free cingular ringtones adipex online cyclobenzaprine online propecia online cheap tenuate cheap carisoprodol ultracet online cheap pharmacy online wwe ringtones diazepam online xenical online nokia ringtones free qwest ringtones cheap zanaflex diazepam online free verizon ringtones free motorola ringtones order lortab prozac online ericsson ringtones but ortho cyclobenzaprine online celexa online free free ringtones phentermine free cool ringtones cheap prozac qwest ringtones cheap cialis sprint ringtones nexium online free real ringtones sagem ringtones tracfone ringtones nokia ringtones free punk ringtones mp3 ringtones free sony ringtones buy ativan but hgh free midi ringtones free alltel ringtones flexeril online free sony ericsson ringtones motorola ringtones vicodin online tracfone ringtones soma online levitra online free motorola ringtones didrex online cheap sildenafil pharmacy online online cheap zoloft order hydrocodone sony ringtones free cingular ringtones online viagra buy paxil ambien online lortab online hydrocodone online cheap vigrx cheap ativan buy hgh sildenafil online viagra online ortho online free nextel ringtones lisinopril free funny ringtones free sony ericsson ringtones buy vicodin but hydrocodone cheap ativan mono ringtones online clonazepam levitra online free sprint ringtones cheap cialis sony ringtones order levitra cheap zanaflex phentermine cheap celexa order ultracet wwe ringtones free sharp ringtones buy norco

Contents

Author

Rudolph van Graan

Article

One of the stranger things in Erlang for me was the concept of Funs. (Think Functions). According to the Erlang Documentation a fun is a functional object. In short it means that you can use a fun to assign code to a variable.

This is very useful. Imagine a scenario where you want to write the code now (because you have access to data, state or variables), but it should only be executed much later and by someone else. A typical use is to run a query which writes something to database, without returning something. Usually I prefer to have some other process (or thread) in a system take care of all database related functionality. You can then write a fun to execute the query and (possibly) process the result. You can achieve this with code that looks something like this:

 1: function() ->
 2:   V1 = "select * from my_table;",
 3:   F = fun() ->
 4:       Query = V1,
 5: 	  {ok,Result} = queryexecuter:execute(Query),
 6: 	  io:format("The result of the query is ~p\n",[Result]),
 7: 	  Result
 8:   end,
 9:   queryqueue:queue(F),
10:   ok.

The interesting part is lines 3 to 8. This creates a functional object named F. You can now pass F into the (fictional) queryqueue:queue() which presumably will queue the query and execute it at some time in the future. What is neat about this, is that the Fun made references to the variable V1 and used that later.

When the queryqueue executes the Fun, it would do something like this:

11: executequery(Fun) ->
12:   Result = Fun(), 
13:   ok.

The earlier Fun is passed as the parameter to executequery (line 11). It can then execute the fun simply by invoking it (line 12). At this time, our earlier fun will execute and print the result as in line 6

C# Delegates

A delegate declaration defines a reference type that can be used to encapsulate a method with a specific signature. A delegate instance encapsulates a static or an instance method. Delegates are roughly similar to function pointers in C  ; however, delegates are type-safe and secure. A delegate lets you pass a function as a parameter.

Look at the following delegate declaration:

public delegate int doCalculate(int aParam1, int aParam2);

It takes two integer parameters and return another integer. This is known as the signature of the delegate.

If you build a class:

 1:  public class Class1 {
 2:    public  doCalculate getDelegate() {
 3:      return new doCalculate(intMultiply);
 4:    }
 5:
 6:    private int intMultiply(int aParam1, int aParam2) {
 7:      return aParam1 * aParam2; 
 8:    }
 9:  }

You can use this class as follows

10: ...
11: Class1 myClass = new Class1();
12: doCalculate delegate = myClass.getDelegate();
13: ...
14: int result = delegate(12,4);
15: ...

As you can see, this is very similar to the Erlang Fun concept. To be very honest, you could achieve the same effect by declaring a simple one function interface and implementing that in Class1.

I prefer delegates to interfaces because you don't have to explicitly implement the interface in the class, so you are completely free of the naming conventions interfaces impose. It is completely possible to pass return two delegate instances from the same class and of the same type that point to two different internal functions.

The difference

In essence the two languages achieve the same thing through two very different mechanisms. In C# the delegate retains a reference to the original object, while in Erlang the Fun is completely detached from the call stack at the time of execution.

In Java, this is called the Command Pattern. Simply put, you pass an object extending an interface with an execute() method as argument to such a queue or something that will later invoke .execute() on it, without knowing anything about the object.

In C, the same can be achieved using a pointer to a function. Pass this function pointer around to the queue class and let it later invoke an .execute() method or similar.