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

后端 未结 16 1714
盖世英雄少女心
盖世英雄少女心 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:10

    The generalized variant of the plain zip version of this is what I would think of as window. Not at a ghci terminal right now, but I think window n = take n . tails. Then your function is zipWith (\[x,yj -> f x y) . window 2. This sort of style naturally works better when f is of type [a] -> b.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2021-02-06 00:12

    Since it's similar to "fold" but doesn't collapse the list into a single value, how about "crease"? If you keep "creasing", you end up "folding" (sort of).

    We could go with a cooking metaphor and call it "pinch", like pinching the crust of a pie, though this might suggest a circular zipping, where the last element of the list is paired with the first.

    def pinch(f, l):
        return map(lambda t: f(*t), zip(l, l[1:]+l[:1]))
    

    (If you only like one of "crease" or "pinch", please note so as a comment. Should these be separate suggestions?)

    0 讨论(0)
  • 2021-02-06 00:12

    zipWithTail or adjacentPairs.

    0 讨论(0)
  • 2021-02-06 00:13

    I'd be tempted to call it contour as I've used it for "contour" processing in music software - at the time I called it twomap or something silly like that.

    There are also two specific named 'contours' in music processing one is gross contour - does the pitch go up or down. The other is refined contour where the the contour is either up, down, leap up or leap down, though I can't seem to find a reference for how large the semitone difference has to be to make a leap.

    0 讨论(0)
  • 2021-02-06 00:14

    This reminds me of convolution from image processing. But not sure if this is mathematically correct.

    0 讨论(0)
提交回复
热议问题