Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
James Garfield
committed
Apr 5, 2015
1 parent
5ed8102
commit 0a3858f
Showing
3 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
//Type detection in impl finds all valid type targets | ||
//goast.net/x/iter looks for a type that matchs []interface{} | ||
//Botch Vector and Vectors match this, so both will have `iter` implemented | ||
//go:generate goast write impl goast.net/x/iter | ||
|
||
type Vector []int64 | ||
type Vectors []Vector | ||
|
||
func (v Vector) Product() int64 { | ||
return v.Fold(1, func(a, b int64) int64 { return a * b }) | ||
} | ||
|
||
func (v Vector) Sum() int64 { | ||
return v.Fold(0, func(a, b int64) int64 { return a + b }) | ||
} | ||
|
||
func (v Vector) ScalarProduct(vex ...Vector) int64 { | ||
var zipped Vectors = v.Zip(vex...) | ||
return zipped.Fold(Vector{}, func(acc, vec Vector) Vector { | ||
return append(acc, vec.Product()) | ||
}).Sum() | ||
} | ||
|
||
func main() { | ||
x := Vector{1, 2, 3, 4, 5} | ||
y := Vector{10, 10, 10, 10, 10} | ||
|
||
sp := x.ScalarProduct(y) | ||
//(1*10) + (2*10) + (3*10) + (4*10) + (5*10) == 150 | ||
fmt.Printf("Scalar Product: %d\n", sp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package main | ||
|
||
func (s Vector) All(fn func(int64) bool) bool { | ||
for _, v := range s { | ||
if !fn(v) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
func (s Vector) Any(fn func(int64) bool) bool { | ||
for _, v := range s { | ||
if fn(v) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
func (s Vector) Count(fn func(int64) bool) int { | ||
count := 0 | ||
for _, v := range s { | ||
if fn(v) { | ||
count += 1 | ||
} | ||
} | ||
return count | ||
} | ||
func (s Vector) Each(fn func(int64)) { | ||
for _, v := range s { | ||
fn(v) | ||
} | ||
} | ||
func (s *Vector) Extract(fn func(int64) bool) (removed Vector) { | ||
pos := 0 | ||
kept := *s | ||
for i := 0; i < len(kept); i++ { | ||
if fn(kept[i]) { | ||
removed = append(removed, kept[i]) | ||
} else { | ||
kept[pos] = kept[i] | ||
pos++ | ||
} | ||
} | ||
kept = kept[:pos:pos] | ||
*s = kept | ||
return removed | ||
} | ||
func (s Vector) First(fn func(int64) bool) (match int64, found bool) { | ||
for _, v := range s { | ||
if fn(v) { | ||
match = v | ||
found = true | ||
break | ||
} | ||
} | ||
return | ||
} | ||
func (s Vector) Fold(initial int64, fn func(int64, int64) int64) int64 { | ||
folded := initial | ||
for _, v := range s { | ||
folded = fn(folded, v) | ||
} | ||
return folded | ||
} | ||
func (s Vector) FoldR(initial int64, fn func(int64, int64) int64) int64 { | ||
folded := initial | ||
for i := len(s) - 1; i >= 0; i-- { | ||
folded = fn(folded, s[i]) | ||
} | ||
return folded | ||
} | ||
func (s Vector) Where(fn func(int64) bool) (result Vector) { | ||
for _, v := range s { | ||
if fn(v) { | ||
result = append(result, v) | ||
} | ||
} | ||
return result | ||
} | ||
func (s Vector) Zip(in ...Vector) (result []Vector) { | ||
minLen := len(s) | ||
for _, x := range in { | ||
if len(x) < minLen { | ||
minLen = len(x) | ||
} | ||
} | ||
for i := 0; i < minLen; i++ { | ||
row := Vector{s[i]} | ||
for _, x := range in { | ||
row = append(row, x[i]) | ||
} | ||
result = append(result, row) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package main | ||
|
||
func (s Vectors) All(fn func(Vector) bool) bool { | ||
for _, v := range s { | ||
if !fn(v) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
func (s Vectors) Any(fn func(Vector) bool) bool { | ||
for _, v := range s { | ||
if fn(v) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
func (s Vectors) Count(fn func(Vector) bool) int { | ||
count := 0 | ||
for _, v := range s { | ||
if fn(v) { | ||
count += 1 | ||
} | ||
} | ||
return count | ||
} | ||
func (s Vectors) Each(fn func(Vector)) { | ||
for _, v := range s { | ||
fn(v) | ||
} | ||
} | ||
func (s *Vectors) Extract(fn func(Vector) bool) (removed Vectors) { | ||
pos := 0 | ||
kept := *s | ||
for i := 0; i < len(kept); i++ { | ||
if fn(kept[i]) { | ||
removed = append(removed, kept[i]) | ||
} else { | ||
kept[pos] = kept[i] | ||
pos++ | ||
} | ||
} | ||
kept = kept[:pos:pos] | ||
*s = kept | ||
return removed | ||
} | ||
func (s Vectors) First(fn func(Vector) bool) (match Vector, found bool) { | ||
for _, v := range s { | ||
if fn(v) { | ||
match = v | ||
found = true | ||
break | ||
} | ||
} | ||
return | ||
} | ||
func (s Vectors) Fold(initial Vector, fn func(Vector, Vector) Vector) Vector { | ||
folded := initial | ||
for _, v := range s { | ||
folded = fn(folded, v) | ||
} | ||
return folded | ||
} | ||
func (s Vectors) FoldR(initial Vector, fn func(Vector, Vector) Vector) Vector { | ||
folded := initial | ||
for i := len(s) - 1; i >= 0; i-- { | ||
folded = fn(folded, s[i]) | ||
} | ||
return folded | ||
} | ||
func (s Vectors) Where(fn func(Vector) bool) (result Vectors) { | ||
for _, v := range s { | ||
if fn(v) { | ||
result = append(result, v) | ||
} | ||
} | ||
return result | ||
} | ||
func (s Vectors) Zip(in ...Vectors) (result []Vectors) { | ||
minLen := len(s) | ||
for _, x := range in { | ||
if len(x) < minLen { | ||
minLen = len(x) | ||
} | ||
} | ||
for i := 0; i < minLen; i++ { | ||
row := Vectors{s[i]} | ||
for _, x := range in { | ||
row = append(row, x[i]) | ||
} | ||
result = append(result, row) | ||
} | ||
return | ||
} |