Comments (5)
Yeah I ran into the same problem when I was trying to do this, and instead came up with a janky semi-stateful internal iteration solution? Not amazing.
So I guess the implementation of min
would look something like... this?
template <sequence Seq>
auto min(Seq& seq) -> Optional<value_t<Seq>> {
auto cursor = first(seq);
if (is_last(seq, cursor)) {
return {};
}
Optional<value_t<Seq>> best = read_at(seq, cursor);
inc(seq, cursor);
iterate_while(seq,
[&](auto&& elem){ *best = std::min(*best, elem); return true; },
cursor);
return best;
}
from flux.
I think min()
itself wouldn't need to be changed, as it just calls fold_first()
. But we could implement that as something like:
template <sequence Seq, typename Func, typename V = value_t<Seq>>
auto fold_first(Seq&& seq, Func func) -> flux::optional<V>
{
auto cur = first(seq);
if (is_last(seq, cur)) {
return std::nullopt;
}
V init(read_at(seq, cur));
inc(seq, cur);
iterate_while(seq, [&](auto&& elem) {
init = std::invoke(func, std::move(init), FLUX_FWD(elem));
return true;
}, std::move(cur));
return optional<V>(std::in_place, std::move(init));
}
(Which is pretty much exactly what you just wrote....)
from flux.
I think this seems like a pretty cool idea. Little awkward having the lambda not last I guess, so maybe do iterate_while(seq, cur, f)
and iterate_while(seq, cur, last, f)
? Definitely curious to see how this pans out!
from flux.
I think too many customization points can confuse users. I prefer a simpler API, even if it means a small performance loss. But if the compiler can optimize much better, it would be a pity not to use it. Maybe we can do some micro benchmarks to prove the speedup. I also agree with @brevzin that we should use lambda as the last argument.
from flux.
I think too many customization points can confuse users.
I agree in general. In this case though I think the argument in favour of adding one more -- in particular, that it would enable internal iteration of slices -- it persuasive enough that it's worth investigating.
I also agree with @brevzin that we should use lambda as the last argument.
As noted above, the odd parameter order is to allow some implementations to combine both overloads into a single function by defaulting the last parameter. For example, the implementation for C arrays could be
template <typename T, index_t N>
struct sequence_traits<T[N]> {
// ...
static constexpr auto iterate_while(auto& self, auto&& pred, index_t from, index_t to = N) -> index_t
{
for (; from < to; ++from) {
if (!std::invoke(pred, self[from])) {
break;
}
}
return from;
}
};
from flux.
Related Issues (20)
- Dropping from an empty sequence asserts HOT 3
- Using Flux to adapt a circular buffer : bug HOT 7
- Add product/permutations/combinations from Python itertools HOT 14
- Suggestion: add a header containing preprocessor macros. HOT 2
- Non const-iterable reversed sequences hard error when used as a range
- Attempted out-of-bounds read in flatten() HOT 1
- BUG: `flux::scan` broken HOT 3
- take_while should not be random-access HOT 1
- Consistent comparisons
- Internal `std::variant` access optimizations HOT 3
- cartesian_product size() can overflow
- Add zip_map adaptor
- flux::iota prevents auto-vectorization. HOT 4
- Clang Compilation failure HOT 4
- move of `loc` in assert.hpp causes clang-tidy error HOT 2
- cartesian_product::last() sometimes returns wrongly-initialised cursor if one of the source sequences is empty
- Clang crash HOT 2
- strange output, [[2]] instead of [] HOT 2
- Visual studio\code intellisense experience HOT 1
- Blog post shows perf gaps between flux and other approaches HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flux.