Skip to content

Commit

Permalink
Merge pull request #52 from tsingbx/lambda
Browse files Browse the repository at this point in the history
modify lambda expression
  • Loading branch information
xushiwei authored Oct 21, 2023
2 parents 8e1a8ab + fa307d4 commit 973f6ce
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
18 changes: 18 additions & 0 deletions 205-Lambda-expressions/lambda-expressions-1.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Lambda expressions are used when we want to define a function inline without giving it any name.

// The following example shows the lambda expression of go+ style, which is more compact and easy to understand.

func transform(a []float64, f func(float64) float64) []float64 {
return [f(x) for x <- a]
}

y := transform([1, 2, 3], x => x * x)
println y // [1 4 9]

z := transform([-3, 1, -5], x => {
if x < 0 {
return -x
}
return x
})
println z // [3 1 5]
11 changes: 11 additions & 0 deletions 205-Lambda-expressions/lambda-expressions-2.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

// If we want a lambda to be defined without being executed, we only omit its identifier. For example:

func returnLambda() func(string) {
return func(msg string) {
println msg
}
}

consoleLog := returnLambda()
consoleLog "Hello"
6 changes: 6 additions & 0 deletions 205-Lambda-expressions/lambda-expressions-3.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

// If we want the lambda to be defined and executed, we omit its identifier and add an argument list in parentheses after the body’s closing curly brace. For example:

func(msg string) {
println msg
}("Hello")
17 changes: 0 additions & 17 deletions 205-Lambda-expressions/lambda.gop

This file was deleted.

0 comments on commit 973f6ce

Please sign in to comment.