# Itertools Library for Motoko

**URL:** <https://forum.dfinity.org/t/itertools-library-for-motoko/14000>\
**Category:** Language Support\
**Tags:** Motoko\
**Created:** [June 23, 2022, 8:26am UTC](https://forum.dfinity.org/t/itertools-library-for-motoko/14000 "2022-06-23T08:26:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomijaga](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/tomijaga/32/4389_2.png) [@tomijaga](https://forum.dfinity.org/u/tomijaga)\
**Post date:** [June 23, 2022, 8:26am UTC](https://forum.dfinity.org/t/itertools-library-for-motoko/14000/1 "2022-06-23T08:26:38Z")

</div>

[Itertools](https://github.com/tomijaga/Itertools.mo) is a library with utility functions and data types for creating efficient iterators in Motoko.

I’ve used Motoko for a while and have created a library in the past, [http-parser.mo](https://github.com/tomijaga/http-parser.mo). While creating this library, I noticed that I used Iterators a lot for looping, accumulating and mutating data for different Motoko types. I had to design custom iterators to perform most of these operations efficiently. That’s why I created this library to boost the functionality of the `Iter` type in Motoko and give the developers the tools they need to build iterators without limits.

I am excited to hear your feedback about this library, and I hope it makes building your Motoko applications and canisters a little bit easier.

I’ve open-sourced the library under the MIT license, so if you have any feature requests, feel free to create an issue or open a PR.

> **[GitHub - tomijaga/Itertools.mo: A library with utility functions for...](https://github.com/tomijaga/Itertools.mo)**
>
> A library with utility functions for iterators in Motoko - GitHub - tomijaga/Itertools.mo: A library with utility functions for iterators in Motoko

Here’s a demo, creating chunks with a max size of 3 elements from an array

```auto
    let vals = [1, 2, 3, 4, 5, 6, 7].vals();
    let chunks = Itertools.chunks(vals, 3);

    assert Iter.toArray(chunks) == [[1, 2, 3], [4, 5, 6], [7]];

```

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [June 23, 2022, 8:50am UTC](https://forum.dfinity.org/t/itertools-library-for-motoko/14000/2 "2022-06-23T08:50:07Z")

</div>

This looks very nice indeed and extending Iter.mo has been on my wish-list for a while. Thank you for doing this!

---

<div class="post-metadata">

**Author:** ![tomijaga](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/tomijaga/32/4389_2.png) [@tomijaga](https://forum.dfinity.org/u/tomijaga)\
**Post date:** [June 23, 2022, 9:29am UTC](https://forum.dfinity.org/t/itertools-library-for-motoko/14000/3 "2022-06-23T09:29:18Z")

</div>

I’m glad I could contribute and take a bit of your planned workload. I’ve used your `mo-pasec` library in the past and have to say it was a good experience that helped me learn more about Motoko.  
I tried to write docs for the use cases and limitations of all the methods in the library, so your feedback is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [June 23, 2022, 10:17am UTC](https://forum.dfinity.org/t/itertools-library-for-motoko/14000/4 "2022-06-23T10:17:50Z")

</div>

I unfortunately won’t have time to take a closer look in the next two days because of the supernova event, but perhaps someone else will.

Again, thank you!

---

<div class="post-metadata">

**Author:** ![matthewhammer](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/matthewhammer/32/323_2.png) [@matthewhammer](https://forum.dfinity.org/u/matthewhammer)\
**Post date:** [June 23, 2022, 1:55pm UTC](https://forum.dfinity.org/t/itertools-library-for-motoko/14000/5 "2022-06-23T13:55:18Z")

</div>

> [@tomijaga](#):
>
> That’s why I created this library to boost the functionality of the `Iter` type in Motoko and give the developers the tools they need to build iterators without limits.

Wonderful work! Thank you!

I wonder, what is the ideal path in your mind for the future? Is it one where some or all of this merges into `base`, or one where it continues to evolve side-by-side, like in the Python and Rust instances?

(Or something in between those two extremes?)

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [June 24, 2022, 1:20am UTC](https://forum.dfinity.org/t/itertools-library-for-motoko/14000/6 "2022-06-24T01:20:20Z")

</div>

I’d love to see some things make their way into base. The http parser should be considered also since http\_request is a system level thing and the base type it implements is lacking.

---

<div class="post-metadata">

**Author:** ![tomijaga](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/tomijaga/32/4389_2.png) [@tomijaga](https://forum.dfinity.org/u/tomijaga)\
**Post date:** [June 24, 2022, 4:18pm UTC](https://forum.dfinity.org/t/itertools-library-for-motoko/14000/7 "2022-06-24T16:18:07Z")

</div>

It would be amazing if these features are merged into the base lib. However, I doubt every function will make it into the base library as I also have more planned. I think the best approach would be to deprecate methods chosen to be merged into the base library and redirect devs there. So, devs who want general iterator tools merged in the base can find them there, and those who want additional functionality can import this lib.
