javascript支持函数式编程的含义是什么?谁举个例子,谢谢。
现在函数编程语言很火,微软的vs2010就是有f#,专门是函数式编程语言。
我不理解函数式编程的本质含义是什么?听说javascript本身也是支持函数式编程的。体现在何处呢?

解决方案 »

  1.   

    我也是刚了解一点这个,函数式编程中,函数是“第一型”,JavaScript中的函数显然是“第一型”;函数式编程循环结构主要(甚至只允许)递归,JavaScript也是支持的,看下面的一段代码:
    Array.prototype.each = function(closure)
    {
    return this.length ? [closure(this[0])].concat(this.slice(1).each(closure)) : [];

    它充分发挥了函数式的魅力,在整个代码中只有函数(function)和符号(Symbol)。它形式简洁并且威力无穷。
    [1,2,3,4].each(function(x){return x * 2})得到[2,4,6,8],而[1,2,3,4].each(function(x){return x-1})得到[0,1,2,3]。 
      

  2.   

    看看这篇关于函数式编程的文章,很不错
    http://www.ibm.com/developerworks/cn/web/wa-javascript.html#functional
      

  3.   

    java火不?普通码农待遇低的要死人
      

  4.   

    函数式编程是对“对象”的数学抽象,在你列举方法基础上我再进一步定义个列表求值方法“map”:Array.prototype.each = function(closure){
          return this.length ? [closure(this[0])].concat(this.slice(1).each(closure)) : [];

    function map(symbol, list){
          return list.each(Function("x","return x" + symbol))
    }
    //------------------------------------
    map("+1", [1,2,3,4,5]) ==> [2,3,4,5,6]
    map("-1", [1,2,3,4,5]) ==> [0,1,2,3,4]
    map("*2", [1,2,3,4,5]) ==> [2,4,6,8,10]
    map("&1", [1,2,3,4,5]) ==> [1,0,1,0,1]
    map("^1", [1,2,3,4,5]) ==> [0,3,2,5,4]
    map(">>>1", [1,2,3,4,5]) ==> [0,1,1,2,2]
    map(">1", [1,2,3,4,5]) ==> [false,true,true,true,true]
    //----------------------------------------------------在上述函数式编程(虚线内)中,基本形式为map(symbol, list),symbol为操作符,list为列表,map方法就是通过symbol对list求值。我们真正关心的是虚线内的“求值前列表状态—求值算法—求值结果”,而无视虚线外的“求值过程”SUCH AS Array.prototype.each的递归过程等。
      

  5.   

    或者,定义匿名算子lambda。Array.prototype.each = function(closure){
          return this.length ? [closure(this[0])].concat(this.slice(1).each(closure)) : [];

    function map(lambda, list){
          return list.each(lambda)
    }
    //-------------------------------------------------------
    map(function(x){return x+1}, [1,2,3,4,5]) ==> [2,3,4,5,6]
    map(function(x){return x-1}, [1,2,3,4,5]) ==> [0,1,2,3,4]
    map(function(x){return x*2}, [1,2,3,4,5]) ==> [2,4,6,8,10]
    map(function(x){return x&1}, [1,2,3,4,5]) ==> [1,0,1,0,1]
    map(function(x){return x^1}, [1,2,3,4,5]) ==> [0,3,2,5,4]
    map(function(x){return x>>>1}, [1,2,3,4,5]) ==> [0,1,1,2,2]
    map(function(x){return x>1}, [1,2,3,4,5]) ==> [false,true,true,true,true]
    //-------------------------------------------------------------------------形式为“map(lambda, list)”,用数学的思维抽象,而不要用“对象”的思维“描述”。