Showing posts with label first class functions. Show all posts
Showing posts with label first class functions. Show all posts

Thursday, 2 January 2014

First class functions or passing arguments to functions in Golang - refactor

Last time, we learned how to pass arguments to functions in Go. We even managed to pass a function as an argument illustrating Go's First-Class Functions. We ended up with a small piece of code as follows:
package main
 
import "fmt"
 
func goodbye(aFunc func(words string) string) {
    fmt.Println(aFunc("Goodbye"))
}
 
func hello(aFunc func(words string) string) {
    fmt.Println(aFunc("Hello"))
}

func main() {
    var x = func(words string) string {
        return words + " world"
    }
    hello(x)
    goodbye(x)
 
}
We have two functions that take as an argument, another function. You will notice that both func goodbye and func hello have the same signature for their single argument. This kind of duplication is crying out for a refactor, and that's exactly what we are going to do. Instead of declaring the function signature as an argument (aFunc func(words string) string), we are going to create a type with that function signature. To declare a type in Go, we simply use the type keyword. Our function that will be used as an argument can be declared like this:
type myOwnFunction func(words string) string
We can then use our newly defined type. For example, our goodbye function can be re-written to use our new type:
func goodbye(aFunc myOwnFunction) {
    fmt.Println(aFunc ("Goodbye"))
}
Similarly, our hello function:
func hello(aFunc myOwnFunction) {
    fmt.Println(aFunc ("Hello"))
}
The rest of our code can remain untouched. A complete listing of our refactored code now looks like this:
package main
 
import "fmt"

type myOwnFunction func(words string) string
 
func goodbye(aFunc myOwnFunction) {
    fmt.Println(aFunc ("Goodbye"))
}
 
func hello(aFunc myOwnFunction) {
    fmt.Println(aFunc ("Hello"))
}
 
func main() {
    var x = func(words string) string {
        return words + " world"
    }
    hello(x)
    goodbye(x)
 
}

Wednesday, 25 December 2013

Passing arguments to functions in Golang

Functions wouldn't be much use if we couldn't pass parameters to them. Rewriting our previous example, we might decide to create a function that takes a string as it's single argument.
package main

import "fmt"

func saySomething(somethingToSay string) {
    fmt.Println(somethingToSay)
}

func main() {
    saySomething("Hello")
    saySomething("world")
}
We've declared a function saySomething that takes a single argument. The argument is a string type and is called somethingToSay. We then use our new function, calling it twice from our main func to output:
Hello
wordl
Go also provides first class functions. This means we can pass a function as an argument into another function. For example:
package main

import "fmt"

func hello(aFunc func(words string) string) {
    fmt.Println(aFunc("Hello"))
}


func main() {

    var x = func(words string) string {
        return words + " world"
    }
    hello(x)

}
Again, all our program does is print
Hello world
, though in a roundabout way. We've defined a function hello which accepts as its argument another function - note that the argument defines the signature of the function we expect to receive - in this case, a function that takes one string argument and returns a string. Moving into our main function, we define x to be a function that will append " world" onto whatever string is passed in. This is rather convoluted, but illustrates the point. Now, if we fancy, we can define a goodbye function as well - to wit:
package main

import "fmt"

func goodbye(aFunc func(words string) string) {
    fmt.Println(aFunc("Goodbye"))
}

func hello(aFunc func(words string) string) {
    fmt.Println(aFunc("Hello"))
}


func main() {

    var x = func(words string) string {
        return words + " world"
    }
    hello(x)
    goodbye(x)

}
Now our program outputs the following:
Hello world
Goodbye world
Now our code is ripe for a refactor - we have duplicate argument definitions, which we can code out - but we will leave that to another post.