项目作者: narimiran

项目描述 :
Nim rewrite of a very popular Python module of the same name.
高级语言: Nim
项目地址: git://github.com/narimiran/itertools.git
创建时间: 2018-03-24T18:13:55Z
项目社区:https://github.com/narimiran/itertools

开源协议:MIT License

下载


Itertools

This package is a Nim rewrite of a very popular Python module of the same name.

It also includes some of the iterators from iterutils.

Installation

  1. nimble install itertools

Required Nim version is at least 0.18.0.

Supported iterators

  • infinite iterators:

    • count
    • cycle
    • repeat
  • terminating iterators:

    • accumulate
    • chain
    • compress
    • dropWhile
    • filterFalse
    • groupBy
    • groupConsecutive
    • islice
    • takeWhile
  • combinatoric iterators:

    • product
    • distinctPermutations
    • permutations
    • combinations
  • iterutils iterators:

    • chunked
    • windowed
    • pairwise
    • unique

Usage

For more comprehensive examples, see the documentation.

Infinite iterators

WARNING: Version 0.3.0 introduced breaking changes regarding how these three iterators are implemented and used.
See the examples below for the new behaviour.

  1. import itertools
  2. for i in count(5):
  3. if i > 9: break
  4. echo i
  5. # 5; 6; 7; 8; 9
  6. for i in count(100, 30):
  7. if i > 200: break
  8. echo i
  9. # 100; 130; 160; 190
  10. var n = 0
  11. for i in @["I", "repeat", "myself"].cycle:
  12. inc n
  13. if n > 8: break
  14. echo i
  15. # I; repeat; myself; I; repeat; myself; I; repeat
  16. for i in "Beetlejuice".repeat(3):
  17. echo i
  18. # Beetlejuice; Beetlejuice; Beetlejuice
  19. var k = 0
  20. for i in "forever".repeat:
  21. inc k
  22. if k > 6: break
  23. echo i
  24. # forever; forever; forever; forever; forever; forever

Terminating iterators

  1. import itertools
  2. import future # to use `=>` for anonymous proc
  3. let # you can use: sequences, arrays, strings
  4. numbers = @[1, 3, 7, 8, 4, 2, 6, 5, 9]
  5. constants = [2.7183, 3.1416, 1.4142, 1.7321]
  6. word = "abracadabra"
  7. for i in accumulate(constants, (x, y) => x + y):
  8. echo i
  9. # 2.7183; 5.8599; 7.2741; 9.0062
  10. for i in compress(numbers, [true, true, false, true, false, true]):
  11. echo i
  12. # 1; 3; 8; 2
  13. for i in dropWhile(numbers, x => (x != 8)):
  14. echo i
  15. # 8; 4; 2; 6; 5; 9
  16. for i in filterFalse(word, x => (x == 'a')):
  17. echo i
  18. # b; r; c; d; b; r
  19. for key, group in numbers.groupBy(x => x mod 2 == 0):
  20. echo "key: ", key, " group: ", group
  21. # key: true, group: @[8, 4, 2, 6]; key: false, group: @[1, 3, 7, 5, 9]
  22. for key, group in word.groupBy():
  23. echo group
  24. # @['a', 'a', 'a', 'a', 'a']; @['b', 'b']; @['c']; @['d']; @['r', 'r']
  25. for key, group in groupConsecutive("aaabaabb"):
  26. echo group
  27. # @['a', 'a', 'a']; @['b']; @['a', 'a']; @['b', 'b']
  28. for i in islice(numbers, 5):
  29. echo i
  30. # 2; 6; 5; 9
  31. for i in islice(word, 1, step=2):
  32. echo i
  33. # b; a; a; a; r
  34. for i in islice(numbers, stop=5, step=2):
  35. echo i
  36. # 1; 7; 4
  37. for i in takeWhile(constants, x => (x >= 2.0)):
  38. echo i
  39. # 2.7183; 3.1416
  40. for i in chain(@[1, 3, 5], @[2, 4, 6], @[7, 8, 9]):
  41. echo i
  42. # 1; 3; 5; 2; 4; 6; 7; 8; 9

Combinatoric iterators

  1. import itertools
  2. import strutils # to join seq[char] into a string
  3. let # you can use: sequences, arrays, strings
  4. numbers = @[1, 3, 7, 8, 4]
  5. constants = [2.7183, 3.1416]
  6. word = "abba"
  7. for i in product([0, 1], repeat = 3):
  8. echo i
  9. # @[0, 0, 0]; @[0, 0, 1]; @[0, 1, 0]; @[0, 1, 1]; @[1, 0, 0]; @[1, 0, 1]; @[1, 1, 0]; @[1, 1, 1]
  10. for i in product(numbers, constants):
  11. echo i
  12. # (a: 1, b: 2.7183); (a: 1, b: 3.1416); (a: 3, b: 2.7183); (a: 3, b: 3.1416); (a: 7, b: 2.7183); (a: 7, b: 3.1416); (a: 8, b: 2.7183); (a: 8, b: 3.1416); (a: 4, b: 2.7183); (a: 4, b: 3.1416)
  13. for i in distinctPermutations(word):
  14. echo i.join
  15. # aabb; abab; abba; baab; baba; bbaa
  16. for i in permutations(word):
  17. echo i.join
  18. # abba; abab; abba; abab; aabb; aabb; baba; baab; bbaa; bbaa; baab; baba; baba; baab; bbaa; bbaa; baab; baba; aabb; aabb; abab; abba; abab; abba
  19. for i in combinations(5, 3):
  20. echo i
  21. # @[0, 1, 2]; @[0, 1, 3]; @[0, 1, 4]; @[0, 2, 3]; @[0, 2, 4]; @[0, 3, 4]; @[1, 2, 3]; @[1, 2, 4]; @[1, 3, 4]; @[2, 3, 4]
  22. for i in combinations(numbers, 2):
  23. echo i
  24. # @[1, 3]; @[1, 7]; @[1, 8]; @[1, 4]; @[3, 7]; @[3, 8]; @[3, 4]; @[7, 8]; @[7, 4]; @[8, 4]

Iterutils iterators

  1. import itertools
  2. let # you can use: sequences, arrays, strings
  3. numbers = @[1, 3, 7, 8, 4, 2, 6, 5, 9]
  4. constants = [2.7183, 3.1416, 1.4142, 1.7321]
  5. word = "abracadabra"
  6. for i in chunked(numbers, 3):
  7. echo i
  8. # @[1, 3, 7]; @[8, 4, 2]; @[6, 5, 9]
  9. for i in windowed(numbers, 4):
  10. echo i
  11. # @[1, 3, 7, 8]; @[3, 7, 8, 4]; @[7, 8, 4, 2]; @[8, 4, 2, 6]; @[4, 2, 6, 5]; @[2, 6, 5, 9]
  12. for i in pairwise(constants):
  13. echo i
  14. # @[2.7183, 3.1416]; @[3.1416, 1.4142]; @[1.4142, 1.7321]
  15. for i in unique(word):
  16. echo i
  17. # a; b; r; c; d

Contributing

There is probably a lot of room for improvement.
Feel free to fork the repo and submit your PRs.

Before submitting, run nim doc -o:./docs/index.html ./src/itertools.nim to make sure that all the asserts in runnableExamples are passing.

License

MIT license