# Itertools Package¶

The itertools package provides the `Iter` class for doing useful things with iterators. It is Inspired by Python's itertools library, Rust's Iterator, and Elixir's Enum and Stream.

## Iter¶

The Iter class wraps iterators so that additional methods may be applied to it. Some methods, such as fold and collect, run through the underlying iterator in order to return a result. Others, such as map and filter, are lazy. This means that they return another Iter so that the resulting values are computed one by one as needed. Lazy methods return Iter types.

For example, the following code creates an Iter from the values of an array containing the numbers 1 through 5, increments each number by one, filters out any odd numbers, and prints the rest.

``````let xs = Iter[I64]([1; 2; 3; 4; 5].values())
.map[I64]({(x) => x + 1 })
.filter({(x) => (x % 2) == 0 })
.map[None]({(x) => env.out.print(x.string()) })
``````

This will result in an iterator that prints the numbers 2, 4, and 6. However, due to the lazy nature of the map and filter, no iteration has actually occurred and nothing will be printed. One solution to this would be to loop over the resulting Iter as so:

``````for x in xs do
None
end
``````

This will trigger the iteration and print out the values 2, 4, and 6. This is where the `run` method comes in handy by doing the iteration without the need for a loop. So the final code would be as follows:

``````Iter[I64]([1; 2; 3; 4; 5].values())
.map[I64]({(x) => x + 1 })
.filter({(x) => (x % 2) == 0 })
.map[None]({(x) => env.out.print(x.string()) })
.run()
``````

Output:

``````2
4
6
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.86218566,"math_prob":0.9622775,"size":1562,"snap":"2021-31-2021-39","text_gpt3_token_len":428,"char_repetition_ratio":0.118741974,"word_repetition_ratio":0.13818182,"special_character_ratio":0.29769525,"punctuation_ratio":0.1597633,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9960014,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-08-04T05:33:19Z\",\"WARC-Record-ID\":\"<urn:uuid:b3827f91-ef6d-498c-a15a-ec02047218b3>\",\"Content-Length\":\"127282\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:babe80a4-05db-436c-b5f2-c7e099a15536>\",\"WARC-Concurrent-To\":\"<urn:uuid:1474c05f-6ec7-4568-8591-edbb3f8334b8>\",\"WARC-IP-Address\":\"54.205.240.192\",\"WARC-Target-URI\":\"https://stdlib.ponylang.org/itertools--index.html\",\"WARC-Payload-Digest\":\"sha1:3OBMT5WJ5D3SE5HMFFMJHCXCM5GSIUB7\",\"WARC-Block-Digest\":\"sha1:4EROAUUL7GXQPKXPTEMD62B3FV7KDEKM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046154796.71_warc_CC-MAIN-20210804045226-20210804075226-00213.warc.gz\"}"}