1
0
mirror of https://github.com/golang/go synced 2024-11-22 14:24:45 -07:00

gofmt-ify 4s, iterable

R=rsc
http://go/go-review/1016055
This commit is contained in:
Robert Griesemer 2009-11-05 14:43:03 -08:00
parent 2a89915485
commit 9786b3f1bd
5 changed files with 352 additions and 341 deletions

View File

@ -139,4 +139,4 @@ var pieces5 = []Piece{
Piece{1, 6, Point{3, 3}, []Point{Point{0, 0}, Point{1, 0}, Point{0, 1}, Point{0, 1}, Point{1, 0}}, nil, nil},
Piece{2, 6, Point{3, 3}, []Point{Point{2, 0}, Point{-2, 1}, Point{1, 0}, Point{1, 0}, Point{-2, 1}}, nil, nil},
Piece{3, 6, Point{3, 3}, []Point{Point{0, 0}, Point{1, 0}, Point{0, 1}, Point{0, 1}, Point{1, 0}}, nil, nil},
};
}

View File

@ -95,43 +95,53 @@ var txbits = [NCOL][32]byte{
[32]byte{0xDD, 0xDD, 0xFF, 0xFF, 0x77, 0x77, 0xFF, 0xFF,
0xDD, 0xDD, 0xFF, 0xFF, 0x77, 0x77, 0xFF, 0xFF,
0xDD, 0xDD, 0xFF, 0xFF, 0x77, 0x77, 0xFF, 0xFF,
0xDD,0xDD,0xFF,0xFF,0x77,0x77,0xFF,0xFF},
0xDD, 0xDD, 0xFF, 0xFF, 0x77, 0x77, 0xFF, 0xFF,
},
[32]byte{0xDD, 0xDD, 0x77, 0x77, 0xDD, 0xDD, 0x77, 0x77,
0xDD, 0xDD, 0x77, 0x77, 0xDD, 0xDD, 0x77, 0x77,
0xDD, 0xDD, 0x77, 0x77, 0xDD, 0xDD, 0x77, 0x77,
0xDD,0xDD,0x77,0x77,0xDD,0xDD,0x77,0x77},
0xDD, 0xDD, 0x77, 0x77, 0xDD, 0xDD, 0x77, 0x77,
},
[32]byte{0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55},
0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
},
[32]byte{0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
0xAA,0xAA,0x55,0x55,0xAA,0xAA,0x55,0x55},
0xAA, 0xAA, 0x55, 0x55, 0xAA, 0xAA, 0x55, 0x55,
},
[32]byte{0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
0x22,0x22,0x88,0x88,0x22,0x22,0x88,0x88},
0x22, 0x22, 0x88, 0x88, 0x22, 0x22, 0x88, 0x88,
},
[32]byte{0x22, 0x22, 0x00, 0x00, 0x88, 0x88, 0x00, 0x00,
0x22, 0x22, 0x00, 0x00, 0x88, 0x88, 0x00, 0x00,
0x22, 0x22, 0x00, 0x00, 0x88, 0x88, 0x00, 0x00,
0x22,0x22,0x00,0x00,0x88,0x88,0x00,0x00},
0x22, 0x22, 0x00, 0x00, 0x88, 0x88, 0x00, 0x00,
},
[32]byte{0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00},
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
},
[32]byte{0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00},
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
},
[32]byte{0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC},
0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
},
[32]byte{0xCC, 0xCC, 0xCC, 0xCC, 0x33, 0x33, 0x33, 0x33,
0xCC, 0xCC, 0xCC, 0xCC, 0x33, 0x33, 0x33, 0x33,
0xCC, 0xCC, 0xCC, 0xCC, 0x33, 0x33, 0x33, 0x33,
0xCC,0xCC,0xCC,0xCC,0x33,0x33,0x33,0x33},
0xCC, 0xCC, 0xCC, 0xCC, 0x33, 0x33, 0x33, 0x33,
},
}
var txpix = [NCOL]draw.Color{
@ -470,8 +480,8 @@ func horiz() bool {
}
func mright() {
if !collide(draw.Pt(pos.X+pcsz, pos.Y), piece)
&& !collide(draw.Pt(pos.X+pcsz, pos.Y+pcsz-DY), piece) {
if !collide(draw.Pt(pos.X + pcsz, pos.Y), piece) &&
!collide(draw.Pt(pos.X + pcsz, pos.Y + pcsz - DY), piece) {
undrawpiece();
pos.X += pcsz;
drawpiece();
@ -480,8 +490,8 @@ func mright() {
}
func mleft() {
if !collide(draw.Pt(pos.X-pcsz, pos.Y), piece)
&& !collide(draw.Pt(pos.X-pcsz, pos.Y+pcsz-DY), piece) {
if !collide(draw.Pt(pos.X - pcsz, pos.Y), piece) &&
!collide(draw.Pt(pos.X - pcsz, pos.Y + pcsz - DY), piece) {
undrawpiece();
pos.X -= pcsz;
drawpiece();
@ -579,7 +589,7 @@ func play() {
if suspended {
break;
}
switch(r){
switch r {
case 'f', ';':
mright();
case 'a', 'j':

View File

@ -12,35 +12,35 @@ import "container/vector"
type Iterable interface {
// Iter should return a fresh channel each time it is called.
Iter() <-chan interface {}
Iter() <-chan interface{};
}
func not(f func(interface{}) bool) (func(interface{}) bool) {
return func(e interface {}) bool { return !f(e) }
return func(e interface{}) bool { return !f(e) };
}
// All tests whether f is true for every element of iter.
func All(iter Iterable, f func(interface{}) bool) bool {
for e := range iter.Iter() {
if !f(e) {
return false
return false;
}
}
return true
return true;
}
// Any tests whether f is true for at least one element of iter.
func Any(iter Iterable, f func(interface{}) bool) bool {
return !All(iter, not(f))
return !All(iter, not(f));
}
// Data returns a slice containing the elements of iter.
func Data(iter Iterable) []interface{} {
vec := vector.New(0);
for e := range iter.Iter() {
vec.Push(e)
vec.Push(e);
}
return vec.Data()
return vec.Data();
}
// filteredIterable is a struct that implements Iterable with each element
@ -53,10 +53,10 @@ type filteredIterable struct {
func (f *filteredIterable) iterate(out chan<- interface{}) {
for e := range f.it.Iter() {
if f.f(e) {
out <- e
out <- e;
}
}
close(out)
close(out);
}
func (f *filteredIterable) Iter() <-chan interface{} {
@ -67,22 +67,22 @@ func (f *filteredIterable) Iter() <-chan interface {} {
// Filter returns an Iterable that returns the elements of iter that satisfy f.
func Filter(iter Iterable, f func(interface{}) bool) Iterable {
return &filteredIterable{ iter, f }
return &filteredIterable{iter, f};
}
// Find returns the first element of iter that satisfies f.
// Returns nil if no such element is found.
func Find(iter Iterable, f func(interface{}) bool) interface{} {
for e := range Filter(iter, f).Iter() {
return e
return e;
}
return nil
return nil;
}
// Injector is a type representing a function that takes two arguments,
// an accumulated value and an element, and returns the next accumulated value.
// See the Inject function.
type Injector func(interface {}, interface {}) interface{};
type Injector func(interface{}, interface{}) interface{}
// Inject combines the elements of iter by repeatedly calling f with an
// accumulated value and each element in order. The starting accumulated value
@ -95,9 +95,9 @@ type Injector func(interface {}, interface {}) interface{};
func Inject(iter Iterable, initial interface{}, f Injector) interface{} {
acc := initial;
for e := range iter.Iter() {
acc = f(acc, e)
acc = f(acc, e);
}
return acc
return acc;
}
// mappedIterable is a helper struct that implements Iterable, returned by Map.
@ -108,26 +108,26 @@ type mappedIterable struct {
func (m *mappedIterable) iterate(out chan<- interface{}) {
for e := range m.it.Iter() {
out <- m.f(e)
out <- m.f(e);
}
close(out)
close(out);
}
func (m *mappedIterable) Iter() <-chan interface{} {
ch := make(chan interface{});
go m.iterate(ch);
return ch
return ch;
}
// Map returns an Iterable that returns the result of applying f to each
// element of iter.
func Map(iter Iterable, f func(interface{}) interface{}) Iterable {
return &mappedIterable{ iter, f }
return &mappedIterable{iter, f};
}
// Partition(iter, f) returns Filter(iter, f) and Filter(iter, !f).
func Partition(iter Iterable, f func(interface{}) bool) (Iterable, Iterable) {
return Filter(iter, f), Filter(iter, not(f))
return Filter(iter, f), Filter(iter, not(f));
}
// TODO:

View File

@ -8,82 +8,83 @@ import (
"testing";
)
type IntArray []int;
type IntArray []int
func (arr IntArray) Iter() <-chan interface{} {
ch := make(chan interface{});
go func() {
for _, x := range arr {
ch <- x
ch <- x;
}
close(ch)
close(ch);
}();
return ch
return ch;
}
var oneToFive = IntArray{ 1, 2, 3, 4, 5 };
var oneToFive = IntArray{1, 2, 3, 4, 5}
func isNegative(n interface{}) bool {
return n.(int) < 0
return n.(int) < 0;
}
func isPositive(n interface{}) bool {
return n.(int) > 0
return n.(int) > 0;
}
func isAbove3(n interface{}) bool {
return n.(int) > 3
return n.(int) > 3;
}
func isEven(n interface{}) bool {
return n.(int) % 2 == 0
return n.(int) % 2 == 0;
}
func doubler(n interface{}) interface{} {
return n.(int) * 2
return n.(int) * 2;
}
func addOne(n interface{}) interface{} {
return n.(int) + 1
return n.(int) + 1;
}
func adder(acc interface{}, n interface{}) interface{} {
return acc.(int) + n.(int)
return acc.(int) + n.(int);
}
// A stream of the natural numbers: 0, 1, 2, 3, ...
type integerStream struct{}
func (i integerStream) Iter() <-chan interface{} {
ch := make(chan interface{});
go func() {
for i := 0; ; i++ {
ch <- i
ch <- i;
}
}();
return ch
return ch;
}
func TestAll(t *testing.T) {
if !All(oneToFive, isPositive) {
t.Error("All(oneToFive, isPositive) == false")
t.Error("All(oneToFive, isPositive) == false");
}
if All(oneToFive, isAbove3) {
t.Error("All(oneToFive, isAbove3) == true")
t.Error("All(oneToFive, isAbove3) == true");
}
}
func TestAny(t *testing.T) {
if Any(oneToFive, isNegative) {
t.Error("Any(oneToFive, isNegative) == true")
t.Error("Any(oneToFive, isNegative) == true");
}
if !Any(oneToFive, isEven) {
t.Error("Any(oneToFive, isEven) == false")
t.Error("Any(oneToFive, isEven) == false");
}
}
func assertArraysAreEqual(t *testing.T, res []interface{}, expected []int) {
if len(res) != len(expected) {
t.Errorf("len(res) = %v, want %v", len(res), len(expected));
goto missing
goto missing;
}
for i := range res {
if v := res[i].(int); v != expected[i] {
t.Errorf("res[%v] = %v, want %v", i, v, expected[i]);
goto missing
goto missing;
}
}
return;
@ -98,31 +99,31 @@ func TestFilter(t *testing.T) {
for i := 0; i < 3; i++ {
res[i] = <-moreInts;
}
assertArraysAreEqual(t, res, []int{ 4, 5, 6 })
assertArraysAreEqual(t, res, []int{4, 5, 6});
}
func TestFind(t *testing.T) {
ints := integerStream{};
first := Find(ints, isAbove3);
if first.(int) != 4 {
t.Errorf("Find(ints, isAbove3) = %v, want 4", first)
t.Errorf("Find(ints, isAbove3) = %v, want 4", first);
}
}
func TestInject(t *testing.T) {
res := Inject(oneToFive, 0, adder);
if res.(int) != 15 {
t.Errorf("Inject(oneToFive, 0, adder) = %v, want 15", res)
t.Errorf("Inject(oneToFive, 0, adder) = %v, want 15", res);
}
}
func TestMap(t *testing.T) {
res := Data(Map(Map(oneToFive, doubler), addOne));
assertArraysAreEqual(t, res, []int{ 3, 5, 7, 9, 11 })
assertArraysAreEqual(t, res, []int{3, 5, 7, 9, 11});
}
func TestPartition(t *testing.T) {
ti, fi := Partition(oneToFive, isEven);
assertArraysAreEqual(t, Data(ti), []int{2, 4});
assertArraysAreEqual(t, Data(fi), []int{ 1, 3, 5 })
assertArraysAreEqual(t, Data(fi), []int{1, 3, 5});
}