Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- )
- type Tuple struct {
- First int
- Second int
- }
- func zip(m []int, n []int) func() (Tuple, bool) {
- if len(m) != len(n) {
- panic("Unequal list length")
- }
- i := -1
- return func() (Tuple, bool) {
- i += 1
- if i < len(m) {
- return Tuple{First: m[i], Second: n[i]}, true
- } else {
- return Tuple{First: 0, Second: 0}, false
- }
- }
- }
- func main() {
- m := []int{1, 2, 3}
- n := []int{4, 5, 6}
- // Get the iterator
- iter := zip(m, n)
- // Iterate through the list.
- for {
- out, ok := iter()
- if !ok {
- break
- }
- fmt.Println(out)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement