我们称之为(新?)高阶函数? [英] What do we call this (new?) higher-order function?

查看:89
本文介绍了我们称之为(新?)高阶函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图说出我认为是高阶函数的新想法。对于重要的部分,下面是Python和Haskell中的代码来演示这个概念,后面将对此进行解释。



Python:

 >>> def pleat(f,l):
return map(lambda t:f(* t),zip(l,l [1:]))
>>> pleat(operator.add,[0,1,2,3])
[1,3,5]

Haskell:

  Prelude>让pleatWith f xs = zipWith f xs(drop 1 xs)
Prelude> pleatWith(+)[0,1,2,3]
[1,3,5]



正如你可以推断出的那样,序列正在迭代,利用相邻元素作为你传递它的函数的参数,并将结果投影到一个新的序列中。那么,有没有人看过我们创建的功能?这对于功能团体中的人员来说是否熟悉?如果没有,我们怎么命名它?

  ----更新---- 


$ b Pleat 胜出!

 前奏>让pleat xs = zip xs(drop 1 xs)
Prelude> pleat [1..4]
[(1,2),(2,3),(3,4)]

Prelude>让pleatWith f xs = zipWith f xs(drop 1 xs)
Prelude> pleatWith(+)[1..4]
[3,5,7]


解决方案

我真的无法在Python的任何地方看到任何编码名称,这是肯定的。 合并很好,但在其他各种环境中都有说法。 犁往往没有被使用,并提供了一个稳定推动整个土壤的巨大视觉效果。也许我只是花了太多的时间园艺。



我还扩展了原理,允许接收任意数量参数的函数。

您也可以考虑:Pleat。它很好地描述了你采取清单的方式(如一长串布料),并将它们聚集在一起。

 导入运算符

def stagger(l,w):
如果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),范围(10),1)
print pleat(lambda a,b,x,y:a + b == x + y,[3,2,4,1,5,0,9, 9,0],4)


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 be explained afterward.

Python:

>>> def pleat(f, l):
       return map(lambda t: f(*t), zip(l, l[1:]))
>>> pleat(operator.add, [0, 1, 2, 3])
[1, 3, 5]

Haskell:

Prelude> let pleatWith f xs = zipWith f xs (drop 1 xs)
Prelude> pleatWith (+) [0,1,2,3]
[1,3,5]

As you may be able to infer, the sequence is being iterated through, utilizing adjacent elements as the parameters for the function you pass it, projecting the results into a new sequence. So, has anyone seen the functionality we've created? Is this familiar at all to those in the functional community? If not, what do we name it?

---- Update ----

Pleat wins!

Prelude> let pleat xs = zip xs (drop 1 xs)
Prelude> pleat [1..4]
[(1,2),(2,3),(3,4)]

Prelude> let pleatWith f xs = zipWith f xs (drop 1 xs)
Prelude> pleatWith (+) [1..4]
[3,5,7]

解决方案

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)

这篇关于我们称之为(新?)高阶函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆