zoukankan      html  css  js  c++  java
  • Lua 函数链功能

    函数链

    http://lua-users.org/wiki/FiltersSourcesAndSinks

    A chain is a function that combines the effect of two (or more) other functions, but whose interface is indistinguishable from the interface of one of its components. Thus, a chained filter can be used wherever an atomic filter can be used. However, its effect on data is the combined effect of its component filters. Note that, as a consequence, chains can be chained themselves to create arbitrarily complex operations that can be used just like atomic operations.

    DEMO

    local function chain2(f1, f2)
      return function(chunk)
        local ret = f2(f1(chunk))
        if chunk then return ret
        else return ret .. f2() end
      end
    end
    
    local filter = {}
    function filter.chain(...)
      local f = arg[1]
      for i = 2, table.getn(arg) do
        f = chain2(f, arg[i])
      end
      return f
    end
    
    
    local function addOne( subject )
        return subject + 1
    end
    
    local function minOne( subject )
        return subject - 1
    end
    
    local nullFunc = filter.chain(addOne, minOne)
    
    local result = nullFunc(56)
    print(result)
    
    
    local nullFunc_1 = filter.chain(minOne, addOne)
    local result = nullFunc_1(6)
    print(result)

    扩展

    Filters can be seen as internal nodes in a network through which data flows, potentially being transformed along its way. Chains connect these nodes together. To complete the picture, we need sources and sinks as initial and final nodes of the network, respectively. Less abstractly, a source is a function that produces new data every time it is called. On the other hand, sinks are functions that give a final destination to the data they receive. Naturally, sources and sinks can be chained with filters.

    Finally, filters, chains, sources, and sinks are all passive entities: they need to be repeatedly called in order for something to happen. Pumps provide the driving force that pushes data through the network, from a source to a sink.

    http://lua-users.org/wiki/FiltersSourcesAndSinks

  • 相关阅读:
    PowerDesigner中生成SQL SERVER2005字段注释 和导出图片的方法
    右键显示打开控制台
    dubbo 的 Protocol 类
    nacos 的 grpc
    shell 替换文本中 为空格,多行为本合并为一行
    gcc、python3、python性能分析工具安装
    kafka listeners和advertised
    Default Activity not found 问题解决
    使用Global Mapper计算kml中面状图形的面积
    jeecg-boot 报表组——折线图初始化显示部分图例,部分变灰
  • 原文地址:https://www.cnblogs.com/lightsong/p/6351350.html
Copyright © 2011-2022 走看看