I write pull function to link (read -> through ... -> sink)
template<typename T>
auto pull(T &&stream) {
return std::forward<T>(stream);
}
// return void/read
// read -> sink
// read -> through
template<typename R, typename S>
auto pull(R &&read, S &sink) {
return sink(std::forward<R>(read));
}
// return read
// read -> through -> ...
template<typename R, typename T, typename... Ts>
auto pull(R &&read, T &through, Ts &&... args) {
return pull(through(std::forward<R>(read)), std::forward<Ts>(args)...);
}
read function, like this, provide a vector:
template<typename T>
auto values(T &begin, T &end) {
return [&](bool abort, auto cb) {
if (end != begin) {
cb(false, *begin++);
} else {
cb(true, *begin);
}
};
}
through function, like this:
template<typename T, typename M>
auto Map(M &&mapper) {
return [&](auto &&read) {
return [&](bool abort, auto cb) {
read(abort, [&](bool end, T val) {
if (end)
cb(true, val);
else
cb(false, mapper(val));
});
};
};
}
sink function like this:
template<typename T, typename R>
auto log(R &&read) {
std::function<void(bool, T)> more = [&](bool done, T val) {
if (!done) {
cout << val << endl;
read(false, more);
}
};
read(false, more);
}
then in the main function:
int main() {
vector<int> vec;
for (int i = 1; i < 4; i++) {
vec.push_back(i);
}
auto begin = vec.begin();
auto end = vec.end();
auto vals = values(begin, end);
auto mapper = [&](int val) { return val * 2; };
auto timesTwo = Map<int>(mapper);
auto newVals1 = pull(vals, timesTwo, timesTwo);
auto newVals2 = pull(vals, timesTwo);
auto logInt = [&](auto read) { log<int>(read); };
//pull(newVals1, logInt); // Segmentation fault, how to correct `pull` function to make this run right
pull(newVals2, logInt); // ok
return 0;
}
pull(newVals2, logInt);
work right,
but pull(newVals1, logInt);
throw Segmentation fault
;
I want to make pull(newVals1, logInt);
work right.
I think, may be some bug in pull
function, but i do not know where, who can help me?
Aucun commentaire:
Enregistrer un commentaire