Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- )
- type Tuple struct {
- Index int
- Element int
- }
- func enumerate(m []int) func() (Tuple, bool) {
- i := -1
- return func() (Tuple, bool) {
- i += 1
- if i < len(m) {
- return Tuple{Index: i, Element: m[i]}, true
- } else {
- return Tuple{Index: i, Element: 0}, false
- }
- }
- }
- func main() {
- m := []int{10, 20, 30, 40, 50}
- iter := enumerate(m)
- for {
- out, ok := iter()
- if !ok {
- break
- }
- fmt.Println(fmt.Sprintf("%d: %d", out.Index, out.Element))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement