What do we call this (new?) higher-order function?

后端 未结 16 1723
盖世英雄少女心
盖世英雄少女心 2021-02-05 23:44

I am trying to name what I think is a new idea for a higher-order function. To the important part, here is the code in Python and Haskell to demonstrate the concept, which will

16条回答
  •  渐次进展
    2021-02-06 00:12

    I really can't see any codified names for this anywhere in Python, that's for sure. "Merge" is good but spoken for in a variety of other contexts. "Plow" tends to be unused and supplies a great visual of pushing steadily through a line of soil. Maybe I've just spent too much time gardening.

    I also expanded the principle to allow functions that receive any number of parameters.

    You might also consider: Pleat. It describes well the way you're taking a list (like a long strand of fabric) and bunching segments of it together.

    import operator
    
    def stagger(l, w):
        if len(l)>=w:
            return [tuple(l[0:w])]+stagger(l[1:], w)
        return []
    
    def pleat(f, l, w=2):
        return map(lambda p: f(*p), stagger(l, w))
    
    print pleat(operator.add, range(10))
    print pleat(lambda x, y, z: x*y/z, range(3, 13), 3)
    print pleat(lambda x: "~%s~"%(x), range(10), 1)
    print pleat(lambda a, b, x, y: a+b==x+y, [3, 2, 4, 1, 5, 0, 9, 9, 0], 4)
    

提交回复
热议问题