你如何在Clojure中创建一个可调用的类型或对象?
例如,我如何定义一个记录Foo,该记录采用:bar可以调用以打印该值的单个值?
user=> (def foo (Foo. "Hello world"))
user=> (foo)
Hello World
user=> (:bar foo)
"Hello World"
Run Code Online (Sandbox Code Playgroud) 这里有一个更通用的问题.将此视为对此的扩展.
我理解类代表对象的类型,我们应该使用名词作为名称.但是,许多语言支持的函数对象更像是函数而不是对象.我应该将这些类别命名为名词,或者在这种情况下动词是否正常.doSomething()从语义上讲,比说更有意义Something().
更新/结论
我得到的两个最高投票答案分享了一个混合的意见:
阿提拉
然而,在仿函数的情况下,它们代表"动作",因此用动词(或某种名词 - 动词组合)命名它们更合适 - 就像你根据它正在做什么命名一个函数一样.
车
另一方面,仿函数的实例实际上是一个函数,也许可以更好地命名.按照Jim的建议,
SomethingDoer doSomething; doSomething();
在经过一些代码之后,他们两个似乎都是常见的做法.在GNU实现STL的,我发现类,如negate,plus,minus(位/ stl_function.h)等.并且variate_generator,mersenne_twister(TR1/random.h).类似地,在升压我发现类,如base_visitor,predecessor_recorder(图/ visitors.hpp)以及inverse,inplace_erase(ICL/functors.hpp)
我注意到Runnable任务和Callable任务都有提交方法.为什么只有invokeAll用于可调用任务,但没有用于Runnable任务的invokeAll.谢谢
我正在尝试解决一个问题:
function f($callable_function){
print_r($callable_function);// doesn't work at all, of course
}
f(function(){echo "hello World"});
Run Code Online (Sandbox Code Playgroud)
我Closure Object使用 print_r 函数获取该元素。有没有办法获得:
//OUTPUT RESULT
echo "hello World";
Run Code Online (Sandbox Code Playgroud)
编辑
这样做的目的是获取字符串中的函数声明(稍后用于数据库)。f 函数可以由任何开发人员使用,因此我们的想法是使其尽可能简单,这意味着我不希望开发人员在字符串内声明他的函数。
使用Java 7 我正在尝试构建一个监视数据存储(某些集合类型)的观察程序,然后在某些点返回某些项目.在这种情况下,它们是时间戳,当时间戳通过我希望它返回到起始线程的当前时间.请参阅下面的代码.
@Override
public void run() {
while (!data.isEmpty()) {
for (LocalTime dataTime : data) {
if (new LocalTime().isAfter(dataTime)) {
// return a result but continue running
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了关于未来和可赎回的内容,但他们似乎在回归时停止了这个问题.
我不特别想要返回一个值并停止线程,然后启动另一个任务,如果使用callable,除非这是最好的方法.
寻找这个的最佳技术是什么?似乎有这么广泛的做法.
谢谢
我在包装类方面遇到问题,并且无法弄清楚我做错了什么。我如何让该包装器与带有“self”参数的任何类函数一起使用?
这是针对 Python 3.7.3 的。问题是我记得包装器以前工作过,但似乎有些东西发生了变化……也许我现在只是做了一些错误的事情,而以前没有。
class SomeWrapper:
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
# this fails because self is not passed
# ERROR: __init__() missing 1 required positional argument: 'self'
func_ret = self.func(*args, **kwargs)
# this is also wrong, because that's the wrong "self"
# ERROR: 'SomeWrapper' object has no attribute 'some_func'
# func_ret = self.func(self, *args, **kwargs)
return func_ret
class SomeClass:
SOME_VAL = False
def __init__(self):
self.some_func()
print("Success")
@SomeWrapper
def some_func(self):
self.SOME_VAL = True
def …Run Code Online (Sandbox Code Playgroud) Mypy 似乎无法理解属性不是类的可调用方法:
from abc import abstractmethod
from typing import Type
class A:
pass
class B:
@abstractmethod
@property
@classmethod
def other_type(cls) -> Type[A]:
pass
@classmethod
def __init_subclass__(cls, **kwargs):
assert issubclass(cls.other_type, A)
Run Code Online (Sandbox Code Playgroud)
在上面的示例上运行 mypy 会导致以下错误:
error: Argument 1 to "issubclass" has incompatible type "Callable[[], Type[A]]"; expected "type" [arg-type]
Run Code Online (Sandbox Code Playgroud)
如果没有 ,我如何强制执行正确的类型# type: ignore[arg-type]?我正在使用 mypy 版本 0.782 和 python 3.7
(注意:我正在使用该--check-untyped-defs标志运行 mypy,因此 mypy 应该扫描该__init_subclass__方法)。
我有一个定义 a 的类__call__,但是当用户在帮助返回时看到对这个“函数”的请求帮助时,他们获得了整个对象和方法,这可能有点令人生畏......我希望帮助看起来如果可能的话,更像是函数的帮助。
我跟踪了inspect模块,但似乎因为我不能从函数类型和/或方法类型继承,所以这可能不可行。我是否遗漏了什么,或者我是否必须重新安排代码以便我动态构建一个真正的 Python 函数对象?
我正在尝试使用泛型(我第一次尝试使用泛型)并使用ExecutorService来实现“ TaskExecutor”。
这是我的“ TaskExecutor”类:
public class ExecuteAlerterTask<T> {
public List<T> process(String executorName, Callable<T> task) throws ExecutionException, InterruptedException {
final ThreadFactory threadFactory = new ThreadFactoryBuilder()
.setNameFormat(executorName + "-%d")
.setDaemon(true)
.build();
ExecutorService executor = Executors.newFixedThreadPool(10, threadFactory);
Collection<Future<T>> futures = new ArrayList<>();
IntStream.range(1, 10).forEach(i -> {
Future<T> future = executor.submit(task);
futures.add(future);
});
List<T> result = new ArrayList<>();
for (Future<T> f : futures) {
result.add(f.get());
}
executor.shutdown();
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的运行方式:
@Test
public void process() throws Exception {
Callable<String> callable = () -> …Run Code Online (Sandbox Code Playgroud)