{"comment":{"url":"https://api.github.com/repos/epkann/Data-Structures/comments/19141580","html_url":"https://github.com/epkann/Data-Structures/commit/2298f8a0d3b806966a36670d789ccc5a2b761d4f#commitcomment-19141580","id":19141580,"user":{"login":"dsnet","id":6354026,"avatar_url":"https://avatars.githubusercontent.com/u/6354026?v=3","gravatar_id":"","url":"https://api.github.com/users/dsnet","html_url":"https://github.com/dsnet","followers_url":"https://api.github.com/users/dsnet/followers","following_url":"https://api.github.com/users/dsnet/following{/other_user}","gists_url":"https://api.github.com/users/dsnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dsnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dsnet/subscriptions","organizations_url":"https://api.github.com/users/dsnet/orgs","repos_url":"https://api.github.com/users/dsnet/repos","events_url":"https://api.github.com/users/dsnet/events{/privacy}","received_events_url":"https://api.github.com/users/dsnet/received_events","type":"User","site_admin":false},"position":5,"line":5,"path":"stack/stack.go","commit_id":"2298f8a0d3b806966a36670d789ccc5a2b761d4f","created_at":"2016-09-23T06:31:56Z","updated_at":"2016-09-23T06:31:56Z","body":"The iterator pattern is one that is common to Java and Python. Go has iterators too; you've probably used them before:\r\n```go\r\nfor k, v := range m { ... }\r\n```\r\nCurrently, Go does not allow you to define your own type that can be `ranged` over. Any attempt at adding Generics to Go will probably need to find some way for arbitrary user-defined types to satisfy some type of `Ranger` interface or something.\r\n\r\nThat being said, it's not hard to define your own iterator-like functionality in your data structure, and it is totally fine (and often useful) to do. One way to do it is the following:\r\n```go\r\n// IterByDFS returns a iterator callback that iterates over the BinaryTree\r\n// in a depth-first manner. The callback returns nil when it is done iterating.\r\nfunc (bt *BinaryTree) IterByDFS() func() *Item { ... }\r\n```\r\n\r\nUsage:\r\n```go\r\niter := bt.IterByDFS()\r\nfor v := iter(); v != nil; v = iter() {\r\n    // Logic here.\r\n}\r\n```\r\n\r\nThis is just one pattern I came up with. There are many other approaches. The fact that there are many approaches is also one reason that it's hard coming up with a unified language-level construct for iterators.\r\n\r\nExample of iterator-like patterns in production code:\r\n* [github.com/boltdb/bolt.Bucket.Cursor](https://godoc.org/github.com/boltdb/bolt#Bucket.Cursor)\r\n* [container/list](https://golang.org/pkg/container/list/) - see example in overview.\r\n\r\n> Also - what is the difference between iterating and recursion (for traversing a DS)?\r\nThey are separate concepts. I would say that the connection occurs because iterating over a DS often involves recursion (but not always). For example, iterating over a slice is done with a simple for loop.\r\n"}}
{"actor":{"display_login":"dsnet"}}