所以我回答了一个关于懒惰评估的问题(这里,我的答案对于那个案例来说太过分了,但这个想法似乎很有趣),这让我想到了如何在C++中进行懒惰的评估.我提出了一个方法,但我不确定这方面的所有陷阱.是否有其他方法可以实现懒惰评估?怎么可能这样做?什么是陷阱,这个和其他设计?
这是我的想法:
#include <iostream>
#include <functional>
#include <memory>
#include <string>
#define LAZY(E) lazy<decltype((E))>{[&](){ return E; }}
template<class T>
class lazy {
private:
typedef std::function<std::shared_ptr<T>()> thunk_type;
mutable std::shared_ptr<thunk_type> thunk_ptr;
public:
lazy(const std::function<T()>& x)
: thunk_ptr(
std::make_shared<thunk_type>([x](){
return std::make_shared<T>(x());
})) {}
const T& operator()() const {
std::shared_ptr<T> val = (*thunk_ptr)();
*thunk_ptr = [val](){ return val; };
return *val;
}
T& operator()() {
std::shared_ptr<T> val = (*thunk_ptr)();
*thunk_ptr = [val](){ return val; };
return *val;
}
};
void log(const lazy<std::string>& msg) …Run Code Online (Sandbox Code Playgroud) 所以我试图在mongodb shell中使用javascript数组扩展(Array.prototype函数),它们似乎没有被定义.我认为这对我来说没问题,但我非常希望在实际的quires(即group)和map-reduce中使用它们.我使用的是mognodb版本2.4.10.它在这里说2.4 plus应该有ES5阵列扩展.他们只是没有壳?
具体来说,填充方法不起作用
test = [1, 2, 3, 4]
test.fill(0)
Run Code Online (Sandbox Code Playgroud)
没有说TypeError:对象1,2,3没有方法'填充'