Advertisement
Guest User

AHK Iterators, but it works!

a guest
Jun 11th, 2024
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. ; Just some of my libraries, nothing important here.
  2. #Include ../libs/useful/logger.v2.ahk
  3. #Include ../libs/useful/option.v2.ahk
  4. #Include ../libs/useful/result.v2.ahk
  5.  
  6. #SingleInstance Force
  7.  
  8. Class Iter {
  9.  
  10. __New(data) {
  11. this.data := data
  12. this.i := 1
  13. }
  14.  
  15. next() {
  16. if this.data.Length < this.i {
  17. return Option.none()
  18. }
  19.  
  20. this.i++
  21. return Option.of(this.data[this.i - 1])
  22.  
  23. }
  24.  
  25. map(mapper) {
  26. return IterMap(this, mapper)
  27. }
  28.  
  29. filter(predicate) {
  30. return IterFilter(this, predicate)
  31. }
  32.  
  33. }
  34.  
  35. class IterMap extends Iter {
  36.  
  37. __New(itr, mapper) {
  38. this.itr := itr
  39. this.mapper := mapper
  40. }
  41.  
  42. next() {
  43. item := this.itr.next()
  44. if item.isNone() {
  45. return item
  46. }
  47.  
  48. return Option.of(this.mapper.Call(item.unwrap()))
  49. }
  50.  
  51. }
  52.  
  53. class IterFilter extends Iter {
  54.  
  55. __New(itr, predicate) {
  56. this.itr := itr
  57. this.predicate := predicate
  58. }
  59.  
  60.  
  61. next() {
  62.  
  63. item := this.itr.next()
  64. if item.isNone() {
  65. return item
  66. }
  67.  
  68. res := this.predicate.Call(item.unwrap())
  69.  
  70. if !res {
  71. return Option.none()
  72. }
  73.  
  74. return Option.of(res)
  75. }
  76.  
  77. }
  78.  
  79. main() {
  80. lg := Logger(, true)
  81.  
  82. f := Iter([1, 2, 3, 4, 5]).map(x => Round(x * 33 / 1.534)) ; .filter(x => Mod(x, 2) == 0)
  83.  
  84. while (item := f.next()).isSome() {
  85.  
  86. lg.log(item)
  87. }
  88.  
  89. }
  90.  
  91. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement