返回函数,修改输入函数的值 [英] Return function that modifies the value of the input function

查看:97
本文介绍了返回函数,修改输入函数的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使一个函数具有输入功能并返回一个三倍值的函数.这是我要寻找的一些伪代码. Python或Scala中的具体示例将不胜感激.

How can I make a function that is given a function as input and returns a function with the value tripled. Here is some pseudo code for what I'm looking for. Concrete examples in Python or Scala would be appreciated.

def f(int x):
    return x ** 2

def tipleFunc(Function g)
    return 3 * g

Function newFunc = tripleFunc(f)
print newFunc(5)

推荐答案

def f(x):
    return x ** 2

def tripleFunc(g):
    return lambda *args: 3 * g(*args)

newFunc = tripleFunc(f)
print newFunc(5)

这篇关于返回函数,修改输入函数的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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