Okay so, I was doing some exercism.io golang exercises and found some peculiar shit.
For a given test:
func TestFold(t *testing.T) {
var got int
for _, tt := range foldTestCases {
got = tt.list.Foldr(tt.fn, tt.initial)
}
Where the data-structure is:
var foldTestCases = []struct {
name string
property string
fn binFunc
initial int
list IntList
want int
}{
name: "direction independent function applied to non-empty list",
property: "foldr",
fn: func(x, y int) int { return x + y },
initial: 5,
want: 15,
list: []int{1, 2, 3, 4},
}
a function like
func (list IntList) Foldr(fn binFunc, inital int) int {
for i := len(list) - 1; i >= 0; i-- {
inital = fn(list[i], inital)
}
return inital
}
Will pass the test.
However if I make the module main, generate a list myself and try to pass it to the same damn function
func main() {
list := []int{2,5,4,3,6,7}
passFunction = func(x,y int)int{return x + y}
fmt.Printf(list.Foldr(passFunction, 1)
}
I get list.Foldr undefined (type []int has no field or method Foldr)
Even though in both cases the predicate to the function has to be a list of integers - passed to the function by a custom data type that looks like type IntList []int
Why the fuck does it work when called from tests but not from main???