mchirico / zDaily

Playground to hack and test ideas with Zoe
1 stars 2 forks source link

Day 20: Closures #22

Open mchirico opened 3 years ago

mchirico commented 3 years ago

Video

Ref: https://tour.golang.org/moretypes/25

https://repl.it/talk/share/Day-20-Closures/60938

package main

import (
    "fmt"
)

func New() func(a string, b string) map[string]string {
    m := map[string]string{}

    return func(a string, b string) map[string]string {
        m[a] = b
        return m
    }

}

func main() {
    x := New()
    m := x("zoe", "loves Mouse")
    m["Leah"] = "Loves Mouse"

    m2 := x("abby", "loaves Mouse")
    fmt.Println(m)
    fmt.Printf("m2: %v\n", m2)
}
tacomonkautobot[bot] commented 3 years ago

mchirico, Thanks for opening this issue!

ZoeChiri commented 3 years ago

package main

import "fmt"

func hello_mouse(msg string){ fmt.Println(msg) }

func return_msg() func(string){ return func(msg string){ fmt.Print(msg) } } func int_seq() func() int{ i := 0 return func() int{ i++ return i } }

func main() { fmt.Println("Hello hello")

hello_mouse("Hi Mousey")

func(msg string){ fmt.Println(msg) }("Hello there")

print_func := return_msg() print_func("Hello this is the return")

next_int := int_seq()

fmt.Println(next_int()) fmt.Println(next_int()) }