Return recursive anonymous functions in golang - go

I want to be able to return a recursive anonymous function in golang. I have used the code snippet below.
Here foo() doesn't work because the anonymous function has no way of referring to itself.
bar() works as expected.
What would be the right way of doing this, if at all possible?
package main
import (
"fmt"
)
func foo() func(int) int {
return func(x int) int {
if x == 1 {
return 1
}
return x * func(x-1) // this is where the problem lies
}
}
func bar() func(int) int {
return func(x int) int {
return x * 100
}
}
func main() {
a:= foo()
b:= bar()
fmt.Println(a(5))
fmt.Println(b(5))
}

You can declare f first:
func foo() func(int) int {
var f func(x int) int
f = func(x int) int {
if x == 1 {
return 1
}
return x * f(x-1)
}
return f
}

Related

Golang a map of functions with a receiver

Is there anyway to make a map of function pointers, but functions that take recievers? I know how to do it with regular functions:
package main
func someFunc(x int) int {
return x
}
func main() {
m := make(map[string]func(int)int, 0)
m["1"] = someFunc
print(m["1"](56))
}
But can you do that with functions that take recievers? Something like this (though I've tried this and it doesn't work):
package main
type someStruct struct {
x int
}
func (s someStruct) someFunc() int {
return s.x
}
func main() {
m := make(map[string](someStruct)func()int, 0)
s := someStruct{56}
m["1"] = someFunc
print(s.m["1"]())
}
An obvious work around is to just pass the struct as a parameter, but that's a little dirtier than I would have liked
You can do that using Method Expressions:
https://golang.org/ref/spec#Method_expressions
The call is a bit different, since the method expression takes the receiver as the first argument.
Here's your example modified:
package main
type someStruct struct {
x int
}
func (s someStruct) someFunc() int {
return s.x
}
func main() {
m := make(map[string]func(someStruct)int, 0)
s := someStruct{56}
m["1"] = (someStruct).someFunc
print(m["1"](s))
}
And here's a Go playground for you to test it:
https://play.golang.org/p/PLi5A9of-U

Generating Go method set with reflection

Is it possible to generate an interface or method set of a struct at runtime with reflection?
For example:
type S struct {
a int
}
func (s *S) Fn(b int) int {
return s.a + b
}
type I interface {
Fn(a int) int
}
func main() {
var x I = &S{a: 5}
fmt.Printf("%#v\n", x.Fn)
fmt.Printf("%#v\n", reflect.TypeOf(x).Method(0))
var y I
y.Fn = x.Fn // This fails, but I want to set y.Fn at runtime.
fmt.Printf("%#v\n", reflect.TypeOf(y).Method(0))
}
https://play.golang.org/p/agH2fQ4tZ_
To clarify, I'm trying to build a middleware library so interface I contains the http handlers and I want to wrap each hander with some sort of req/response logging so I need to return a new interface I where each function in new Interface I wraps the original + some logging.
Here's how to handle this for interfaces I and J.
type I interface { Fn1(a int) int }
type J interface { Fn2(a int) int }
type Y struct { // Y implements I by calling fn
fn func(a int) int
}
func (y Y) Fn1(a int) int { return y.fn(a) }
type Z struct { // Z implements J by calling fn
fn func(a int) int
}
func (z Z) Fn2(a int) int { return y.fn(a) }
var y I = Y{fn: x.Fn}
var z J = Z{fn: x.Fn}
There's no need to use reflection.
playground example

Can I define a receiving method in main?

I can define a function globally,
func f()int{
return 0
}
But I can also do in main,
func main(){
var f=func()int{
return 0
}
}
Can I do the same with receiving methods? I can do globally,
type I int
func(i I)f()int{
return int(i)
}
But can I do this in main too, so like (because this does not work)
func main(){
type I int
var f=func(i I)()int{
return 0
}
}
No, not like that,
but like this working sample code you may still use f:
package main
import "fmt"
func main() {
j := I(2)
fmt.Println(j.Double())
f := j.Double
fmt.Println(f())
}
type I int
func (i I) Double() int {
return int(i) * 2
}
output:
4
4

Golang function and function variable semantics

Why can't I define a recursive function as a variable? I seem to be able to define arbitrary functions except when they recurse.
This is legal:
func f(i int) int {
if i == 0 {
return 1
}
return i * f(i-1)
}
func main() {
fmt.Println(f(2))
}
This is illegal:
var f func(int) int = func(i int) int {
if i == 0 {
return 1
}
return i * f(i-1)
}
func main() {
fmt.Println(f(2))
}
This is legal and I'm guessing it is just because you can figure out f after initialization:
func main() {
var f *func(int) int;
t := func(i int) int {
if i == 0 {
return 1
}
return i * (*f)(i-1)
}
f = &t
fmt.Println((*f)(2))
}
So it looks like it comes down to function and variable declarations of a function type are treated differently though from the reading the documentation I wouldn't expect that to be the case. Did I miss the part of the documentation detailing this?
I would expect the illegal case to work just because it works in other languages. Like in JavaScript:
(function () {
var f = function (i) {
if (i == 0) {
return 1;
}
return i * f(i - 1);
};
console.log(f(2));
})();
The below code would be the preferred way of doing what you describe. Note that you do not have to create an additional variable, nor do you have a pointer to a function:
package main
import "fmt"
func main() {
var f func(int) int
f = func(i int) int {
if i == 0 {
return 1
}
return i * f(i-1)
}
fmt.Println(f(2))
}

Golang multiple-value function composition

Short question to which I haven't found an answer on SO: how do I write composite function calls when an inner function has multiple return values?
Sub-question: can you cast just one of the returns from a multiple-value function without using a temp variable?
Example: http://play.golang.org/p/intnxkzSO1
package main
import "fmt"
func multiReturn() (int, int) {
return 0, 1
}
func noOp(a int) int {
return a
}
func main() {
// Too many arguments
fmt.Print(noOp(multiReturn()))
// multiple-value in single-value context
fmt.Print(string(multiReturn()))
}
You can have your outer function match the return values of the inner function
func multiReturn() (int, int) {
return 10, 1
}
func noOp(a, _ int) int {
return a
}
func main() {
fmt.Printf("%v\n", noOp(multiReturn())) // output: 10
fmt.Printf("%s", strconv.Itoa(noOp(multiReturn()))) // output: 10
}
On a side note string(int) will not work, you have to use the strconv package.
Go play
Another option would to use a variadic parameter.
func noOp(a ...int) int {
if len(a) > 0 {
return a[0]
}
return 0
}
For example,
package main
import "fmt"
func singleReturn() int {
s, _ := multiReturn()
return s
}
func multiReturn() (int, int) {
return 0, 1
}
func noOp(a int) int {
return a
}
func main() {
fmt.Print(noOp(singleReturn()))
fmt.Print(string(singleReturn()))
// Too many arguments
//fmt.Print(noOp(multiReturn()))
// multiple-value in single-value context
//fmt.Print(string(multiReturn()))
}
you can also return a function that returns multiple values.
func f() (int, int) {
return 1, 2
}
func g() (int, int) {
return f()
}

Resources