добавил литерал функциональный aka анонимную функцию
parent
be95ad05e3
commit
ebf1922c90
|
@ -285,6 +285,7 @@ type:
|
|||
// literals
|
||||
literal:
|
||||
STRING_LITERAL { }
|
||||
| func_literal { }
|
||||
| COMPLEX_LITERAL { }
|
||||
| BOOL_LITERAL { }
|
||||
| FLOAT_LITERAL { }
|
||||
|
@ -316,6 +317,13 @@ import_list:
|
|||
//
|
||||
|
||||
// functions
|
||||
|
||||
func_literal:
|
||||
FUNC
|
||||
{ printf("\033[1;35mHELLO, ANON FUNC\n\033[0m"); }
|
||||
LPAREN arg_list RPAREN return_type block
|
||||
{ printf("\033[1;35mBY, ANON FUNC\n\n\033[0m"); }
|
||||
|
||||
func_call:
|
||||
any_identifier LPAREN math_expr_or_literals_list_or_empty RPAREN
|
||||
|
||||
|
|
|
@ -34,6 +34,8 @@ func test(a int, b string) {
|
|||
|
||||
func main() {
|
||||
|
||||
CubeRoot(123.1);
|
||||
|
||||
var a int;
|
||||
var c complex128 = 555.12i+1.123i;
|
||||
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package main;
|
||||
|
||||
import "fmt";
|
||||
|
||||
// анонимные функции
|
||||
func anon_func() {
|
||||
// Присваиваем анонимную функцию переменной
|
||||
square := func(x int) int {
|
||||
return x * x;
|
||||
};
|
||||
|
||||
fmt.Println(square(5)); // Выведет: 25
|
||||
}
|
||||
|
||||
// замыкания
|
||||
func intSeq() func() int {
|
||||
i := 0;
|
||||
return func() int {
|
||||
i++;
|
||||
return i;
|
||||
};
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
nextInt := intSeq();
|
||||
|
||||
fmt.Println(nextInt());
|
||||
fmt.Println(nextInt());
|
||||
fmt.Println(nextInt());
|
||||
|
||||
newInts := intSeq();
|
||||
fmt.Println(newInts());
|
||||
}
|
Loading…
Reference in New Issue