将更新的功能传递给现有的功能 [英] Pass an updated function to an existing function

查看:79
本文介绍了将更新的功能传递给现有的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个短序列中,用户创建了一个函数 userfunc(),但是想要更新第一个定义来做一些不同的事情。但是 programfunc()已经编译了第一个版本,并继续使用它。



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
hellohellohello

#程序使用用户函数
programfunc(func,a,b)= func(a,b)
programfunc(userfunc, hello,3)

hellohellohello

#现在用户重新定义函数
userfunc(str,n)= str ^(n * n)
#userfunc(hello,3)给hellohellohellohellohellohellohellohellohello

#但程序仍然使用第一个userfunc()
programfunc(userfunc,hello,3 )

hellohellohello

所以要定义programfunc(),以便它始终使用传递给它的函数的最新定义?

解决方案

invoke 就可以做到。 (请注意,虽然这可能不会编译成漂亮的专用代码)。

这里的问题是,专注于Type的julia。
也就是说,它为每个传递给它的类型组合编译一个定制版本的函数。
由于函数在julia中有一个类型0.5
(每个函数都是单例类型)
导致它专用于函数

在0.5-rc0上测试

  julia>警告:在REPL [20]:1覆盖的REPL [16]:1模块中,模块Main中的方法定义userfunc(Any,Any)被覆盖。 
userfunc(带1个方法的通用函数)

julia>函数programfunc(func,a,b)
invoke(func,(typeof(a),typeof(b)),a,b)
end
programfunc(带1个方法的通用函数)

julia> programfunc(userfunc,hello,3)
hellohellohellohellohellohellohellohellohello

julia>用户函数(str,n)= str ^(n)
警告:在REPL [20]:1覆盖的REPL [16]:1模块中,方法定义userfunc(Any,Any)
userfunc(带1个方法的通用函数)

julia> programfunc(userfunc,hello,3)
hellohellohello

围绕#265

 朱莉娅> foo(x)= 2 * x 
foo(带1个方法的泛型函数)


julia>函数g(x)
invoke(foo,(typeof(x),),x)
end
g(带1个方法的通用函数)

julia> g(2)
4

朱莉亚> foo(x)= 3 * x
警告:REPL [1]:1处的模块Main中的方法定义foo(Any)在REPL [10]:1处被覆盖。
foo(带1个方法的通用函数)

julia> g(2)
6


In this short sequence, the user creates a function userfunc(), but then wants to update the first definition to do something different. However the programfunc() has already compiled the first version, and continues to use it.

userfunc(str, n) = str ^ n
userfunc("hello", 3)

"hellohellohello"

# program makes use of the user's function
programfunc(func, a, b) = func(a, b)
programfunc(userfunc, "hello", 3)

"hellohellohello"

# now the user redefines the function
userfunc(str, n) = str ^ (n * n)
# userfunc("hello", 3) give "hellohellohellohellohellohellohellohellohello"

# but program still makes use of the first userfunc()
programfunc(userfunc, "hello", 3)

"hellohellohello"

So how could programfunc() be defined so that it always uses the latest definition of the function passed to it?

解决方案

invoke will do it. (Note though this will probably not compile to nice specialized code)

The issue here is that julia specialized on Type. That is to say it compiles a custom version of the function for every combination of types passed to it. Since Functions have a type in julia 0.5 (Each function is a singleton type.) that causes it to specialize on the function

tested on 0.5-rc0

julia> userfunc(str, n) = str ^ (n*n)
WARNING: Method definition userfunc(Any, Any) in module Main at REPL[16]:1 overwritten at REPL[20]:1.
userfunc (generic function with 1 method)

julia> function programfunc(func, a, b)
       invoke(func, (typeof(a), typeof(b)), a, b)
       end
programfunc (generic function with 1 method)

julia> programfunc(userfunc, "hello", 3)
"hellohellohellohellohellohellohellohellohello"

julia> userfunc(str, n) = str ^ (n)
WARNING: Method definition userfunc(Any, Any) in module Main at REPL[16]:1 overwritten at REPL[20]:1.
userfunc (generic function with 1 method)

julia> programfunc(userfunc, "hello", 3)
"hellohellohello"

Note this also works around #265

julia> foo(x)=2*x
foo (generic function with 1 method)


julia> function g(x)
       invoke(foo, (typeof(x),), x)
       end
g (generic function with 1 method)

julia> g(2)
4

julia> foo(x)=3*x
WARNING: Method definition foo(Any) in module Main at REPL[1]:1 overwritten at REPL[10]:1.
foo (generic function with 1 method)

julia> g(2)
6

这篇关于将更新的功能传递给现有的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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