11 comments

[ 3.1 ms ] story [ 47.4 ms ] thread
Not sure how many other languages can do that! :P

    >>> def sum(x):
    ...   return lambda y: x + y
    ... 
    >>> sum(4)(5)
    9

    user=> (defn sum [x] #(+ % x)) 
    #'user/sum
    user=> ((sum 4) 5)
    9
Is this a feature ??? Seems a lot more verbose than it needs to be, and would likely be slower too :P
Why is this on the top page?
A lot of languages do that. Even "enterprise languages" such as C#:

  Func<int, int> Sum(int x)
  {
      return (Func<int, int>)((y) => x + y);
  }

  var z = Sum(4)(5);
You don't need the cast :-)

Func<int, int> Sum(int x) { return y => x + y; }

Don't forget about java!

  public static interface a
  {
  	public int a( int a );
  }
  
  public static a sum( final int n )
  {
  	return new a() { public int a( int a ) { return a + n; } };
  }
  
  public static void main( String[] args )
  {
  	System.out.println( sum( 3 ).a( 5 ) );
  }
Of course, the logical extension is making it generic across functions:

    function math(input_func){
        return function(first_input){
            return function(second_input){
                return input_func(first_input, second_input)
            }
        }
    }

    function add(a, b){
        return a + b;
    }
    
    function subtract(a, b){
        return a - b;
    }
   

    alert math(add)(4)(5);
    alert math(subtract)(4)(5);
Hooray for lexical closures!