├── .gitignore ├── LICENSE ├── README.txt ├── go.mod ├── iter.go └── iter_test.go /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 The Go Authors. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above 10 | copyright notice, this list of conditions and the following disclaimer 11 | in the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of Google Inc. nor the names of its 14 | contributors may be used to endorse or promote products derived from 15 | this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | See http://godoc.org/github.com/bradfitz/iter 2 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/bradfitz/iter 2 | 3 | go 1.11 4 | -------------------------------------------------------------------------------- /iter.go: -------------------------------------------------------------------------------- 1 | // Copyright 2014 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package iter provides a syntactically different way to iterate over 6 | // integers. That's it. 7 | // 8 | // This package was intended to be an educational joke when it was 9 | // released in 2014. People didn't get the joke part and started 10 | // depending on it. That's fine, I guess. (This is the Internet.) But 11 | // it's kinda weird. It's one line, and not even idiomatic Go style. I 12 | // encourage you not to depend on this or write code like this, but I 13 | // do encourage you to read the code and think about the 14 | // representation of Go slices and why it doesn't allocate. 15 | package iter 16 | 17 | // N returns a slice of n 0-sized elements, suitable for ranging over. 18 | // 19 | // For example: 20 | // 21 | // for i := range iter.N(10) { 22 | // fmt.Println(i) 23 | // } 24 | // 25 | // ... will print 0 to 9, inclusive. 26 | // 27 | // It does not cause any allocations. 28 | func N(n int) []struct{} { 29 | return make([]struct{}, n) 30 | } 31 | -------------------------------------------------------------------------------- /iter_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2014 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package iter_test 6 | 7 | import ( 8 | "fmt" 9 | "testing" 10 | 11 | "github.com/bradfitz/iter" 12 | ) 13 | 14 | func ExampleN() { 15 | for i := range iter.N(4) { 16 | fmt.Println(i) 17 | } 18 | // Output: 19 | // 0 20 | // 1 21 | // 2 22 | // 3 23 | } 24 | 25 | func TestAllocs(t *testing.T) { 26 | var sink []struct{} 27 | allocs := testing.AllocsPerRun(1000, func() { 28 | sink = iter.N(1e9) 29 | }) 30 | _ = sink 31 | if allocs > 0.1 { 32 | t.Errorf("allocs = %v", allocs) 33 | } 34 | } 35 | --------------------------------------------------------------------------------