Skip to content

Latest commit

 

History

History
192 lines (144 loc) · 5.03 KB

04-anonymous-functions.md

File metadata and controls

192 lines (144 loc) · 5.03 KB

Lab: Anonymous Functions

Take me to the lab!

Help for the VSCode editor.

  1. What is the most appropriate definition for recursion?
    1. var my_func := (func(s string) {fmt.Println("Hey there,", s)})
    2. var (my_func = func(s string) {fmt.Println("Hey there,", s)})
    3. func main() {
          var (
                  my_func = func(s string) { fmt.Println("Hey there,", s) }
          )
          my_func("Joe")
      }
    4. var my_func func := (func(s string) {fmt.Println("Hey there,", s)})
    Reveal

    B, C

    Quite simply, you can never use := with a var statement, leaving only two possible answers.

  2. What would be the output of the following program:
    package main
    
    import (
            "fmt"
            "strings"
    )
    
    func main() {
            x := func(s string) string {
                    return strings.ToUpper(s)
            }
            fmt.Printf("%T \n", x)
            fmt.Println(x("Joe"))
    }
    • func(string) string
      joe
    • func(string) string
      JOE
    • string
      Joe
    • func(string)
      JOE
    Reveal

    func(string) string
    JOE

    • Declare a variable x as an anonymous function that takes a string, returns a string with a body that returns the uppercase of what ever is passed as the argument.
    • Print the type of variable x, which will yield the function signature. A signature comprises:
      1. func
      2. (
      3. List of argument types (if any)
      4. )
      5. Return type(s) (if any) - the return types being a bracketed list if there is more then one.
    • Print the invocation of the function via x, passing "Joe" as an argument. This is returned as "JOE" which gets printed.
  3. What would be the output of the following program:
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
        x := func(s string) {
            fmt.Println(strings.ToLower(s))
        }
        fmt.Printf("%T \n", x)
        x("RacheL")
    }
    • func(string)
      rachel
    • func(string) string
      RACHEL
    • func(string) string
      rachel
    • func(string)
      Rachel
    Reveal

    func(string)
    rachel

    Very similar to the previous question, however

    • Variable x is assigned the anonymous function
    • The function handles the printing itself, therefore does not return anything, thus the signature that is printed is only func(string)
    • The function is lowercasing the value it is given.
  4. What would be the output of the following program:
    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
        x := func(s string) {
            fmt.Println(strings.ToLower(s))
        }("RacheL")
        fmt.Printf("%T \n", x)
    }
    • func(string)
      Rachel
    • Error
    • func(string) string
      rachel
    • func(string)
      rachel
    Reveal

    Error

    • The compiler detects an attempt to call the function during assignment of an anonymous function to a variable :- }("RacheL). This is illegal so the program fails to compile.
  5. What would be the output of the following program:

    Add package and import statements as needed

    var (
            cube = func(i int) string {
                    c := i * i * i
                    return strconv.Itoa(c)
            }
    )
    
    func main() {
            x := cube(8)
            fmt.Printf("%T %v", x, x)
    }
    • func(i int) string
      512
    • int 512
    • func(i int) int string
      512
    • string 512
    Reveal

    string 512

    Note the use of strconv.Itoa. To build this program requires the additional import of "strconv". Recall from earlier labs that Itoa converts an integer to its string representation.

    • cube is declared as a package variable that holds an anonymous function that takes int and returns string
    • The function computes the cube of i, converts that to a string and returns it.
    • In main() variable x is assigned the value returned by calling cube with 8.
    • Finally print the type and value of x. x is the value returned by cube, not cube itself, therefore its type is string. The value of x is the string "512" so that's just printed. It's all on the same line since it's a single Printf with no newline character in it.