home.social

#iterators — Public Fediverse posts

Live and recent posts from across the Fediverse tagged #iterators, aggregated by home.social.

  1. Symbol.iterator Creates INFINITE Sequence?!

    Custom iterators can generate INFINITE sequences! This object never ends, but spread operator can slice it. This is how generators break the rules of finite data!

    #javascript #javascripttricks #symbol.iterator #generators #infinitesequences #javascriptweird #javascriptquiz #codingchallenge #javascriptshorts #javascriptwtf #iterators #advancedjavascript

    youtube.com/watch?v=xt35jT3qFcY

  2. Reverse Iterations

    Sometimes, we all need a way to iterate over a container in the opposite direction. There are several ways to reverse-iterate a container, and in this article, we’ll explore them.

    Index Iteration

    Probably the simplest way, taken from C is to iterate using an index location:

    for (int64_t index = ssize(container) - 1; index >= 0; --index) {    // do something with `container[index]`}

    This way is highly not recommended as it might lead to infinite loops if done incorrectly (for example by using uint64_t or size_t for the index type), and you can find more issues with this way in some previous articles about iterators in this blog.

    Reverse Iterators

    Another way to iterate a container is by using reverse iterators (rbegin() and rend()):

    for (auto it = container.rbegin(); it != container.rend(); ++it) {    // do something with `*it`}

    This is a more recommended way, but it might be a little bit frustrating compared to a regular for-range loop:

    for (auto elem : container) { /*do something with `elem`*/ }

    The closest way to this method using the standard (before C++20) is:

    std::for_each(container.rbegin(), container.rend(), [](auto elem) {    // so something with `elem`});

    Ranges (C++20)

    If you are using the ranges library, or using at least C++20, you can use the following method to iterate a container in reverse order:

    for (auto elem : container | std::views::reverse) {    // do something with `elem`}

    Custom Reverse Container View

    Another way to use the for-range loop (even if you don’t use C++20 or the ranges library), is to create your own container mask, to modify the behavior of the begin() and end() functions:

    template <typename T>class reverse_view {public:    reverse_view(T& cont) : container(&cont) {}        typename T::reverse_iterator begin() { return container->rbegin(); }    typename T::reverse_iterator end() { return container->rend(); }    private:    T* container;};

    Then, you’ll be able to use it like that:

    std::vector<int> container = {1, 2, 3, 4, 5};for (int elem : reverse_view(container)) {    std::cout << elem << ", ";}

    More Ways

    I hope you learned something new, and feel free to share in the comments if you know other ways to reverse-iterate over a container.

    #advanced #C_ #c11 #c14 #c17 #c20 #containers #Intermediate #Iterators #ranges #reverse #reverseIterators

  3. Итерация по UENUM в Unreal Engine

    Понадобилось мне создать панель категорий размещаемых предметов в UI. В наследие мне достался уже готовый UENUM, который в будущем будет изменен. Естественно, очень не хотелось вручную перемещать и настраивать каждый отдельный виджет. Так еще и заниматься этим в будущем с изменениями категорий. Хотелось чего-то простого и универсального. Чтобы вот вызвал условный For Each Loop и сгенерировал все как надо, еще и не обязательно только для этого енама. Выход был найден! Если мы создаем UENUM, то unreal сам генерит всю нужную инфу и создает для нас UEnum класс, который является UObject. Нужно лишь правильно использовать эту информацию.

    habr.com/ru/articles/861944/

    #uenum #unreal_engine #c++ #blueprints #iterator #iterators #tutorial #async #asynchronous #for_each