• curry function

    Function.method('curry', function() {
        //arguments can not be passed in to closure function
        //var l = arguments.length;
        //var args = [];
        //for (var i = 0; i < l; i++) {
        //    args[i] = arguments[i];
        //}
        var args = Array.prototype.slice.apply(arguments);
        var original_function = this;
    
    
  • Does it matter for an object to know its constructor?

    We all knows that javascript inherit the functionality from its constructor. It is important to understand the concept every object has a secret link to constructor's prototype. How about constructor? Does a object know who is its constructor? Let's see a simple example

    function Person() {
      alert(this.constructor.name); //Person
      this.constructor.count++; // reuse its constructor as a name space to prevent name polution
    };
    
    
  • 4 Equals, Reference Type, Value Type

    The very fundamental design in .net clr is that type system is classified into two type, reference type and value type. This design decision has profound implication on the .net. One examples is to test the equality between objects.
    Basically we have two kinds of comparison, identity comparison(whether two object has the same identity), semantic comparison(whether two object means the same thing, most people refer it as value equality comparison, I use “semantic” because value of reference type is a reference, even the values of reference typed variable are different, it is possible that they mean the same thing in semantics). Since we have the two different type, this makes things complicated. For example, can we compare the “value” of reference type, or can we compare the reference of value type. If there had been only reference type, if there had been no value type, the .net world will be simpler. Why we need two types? This is a deep question, lots of this topics has been covered in a book “CLR via C#”. Basically, this a consideration of memory efficiency and performance. What we need to know is that the value of reference type is reference, the value of value type is value.

    Reference type identity comparison

    To do identity comparison for reference type, we should call Object.ReferenceEquals(objA, objB), or you can use shortcurt operator “==” like “objA == objB”. The following source code shows that ReferenceEquals and == operator is the same.

    public class Object 
    {
       [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
       public static bool ReferenceEquals (Object objA, Object objB) {
           return objA == objB; 
       }
    }
    
    
  • IEnumberable, IQueryable , Lambda expression - part2

    I have seen such following piece of code written by a developer from a client.

    interface IContactRepository
    {
        IEnumberable<contact> GetSomeContacts();
    }
    
    
  • IEnumberable, IQueryable , Lambda expression - part1

    When we type the following code

    IEnumerable<int> intEnumerable = null;
    var q1 = intEnumerable.Where( x => x > 10);
    


    we know that Where method is not part of the IEnumberable<t> interface or IEnumberable interface, it comes from extension method of Enumerable, which is static class and it has no inheritance relation With IEnumerable or IEnumberable<t>. The power of Linq-To-Object does not come from IEnumberable or IEnumberable<t> or its implemenation, it comes from the extension method. Let's take a look what does the extension method do? Using Reflector we get the following source code.


    public static class Enumerable
    {
    public static IEnumerable<tsource> Where<tsource>(this IEnumerable<tsource> source, Func<TSource, bool> predicate)
    {
        if (source == null)
        {
            throw Error.ArgumentNull("source");
        }
        if (predicate == null)
        {
            throw Error.ArgumentNull("predicate");
        }
        if (source is Iterator<tsource>)
        {
            return ((Iterator<tsource>) source).Where(predicate);
        }
        if (source is TSource[])
        {
            return new WhereArrayIterator<tsource>((TSource[]) source, predicate);
        }
        if (source is List<tsource>)
        {
            return new WhereListIterator<tsource>((List<tsource>) source, predicate);
        }
        return new WhereEnumerableIterator<tsource>(source, predicate);
    }
    }
    

    We can see there , the delegate passed in to the method is the code that does the filtering.

    IQueryable<t> inherit from IEnumerable. But what extra value does the IQueryable bring. Let's take a look of the following code. and the code it generated by c# compiler.

    public interface IQueryable<t> : IEnumerable<t>, IQueryable, IEnumerable
    {}