Last active
October 8, 2015 10:35
-
-
Save drborges/751522addafb640f6b76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import logger from log | |
| // Function inline definition | |
| fn hello(name string) string = "Hello {name}" | |
| // Pattern matching | |
| fn fib(n int) int = match n { | |
| case n < 0: panic("Negative values not allowed") | |
| case 0, 1 : n | |
| default : fib(n-2) + fib(n-1) | |
| } | |
| // If-Then-Else as expresion | |
| fn fib(n int) int = | |
| if n < 0 panic("Negative values not allowed") | |
| if n < 2 return n | |
| else return fib(n-2) + fib(n-1) | |
| // Function definition with block | |
| fn add(a, b int) int = { | |
| a + b | |
| } | |
| // If-Then-Else with blocks | |
| fn allowed(id int) bool = { | |
| if id > 5 { | |
| // complex logic | |
| } else { | |
| // more complex logic | |
| } | |
| } | |
| // Variable definition + assignment | |
| var myVar = "With type inference" // myVar is string == true | |
| // Constant definition of type fn(int) string a.k.a closure | |
| val map = (data int) string { | |
| if data % 2 == 0 return "even" | |
| else return "odd" | |
| } | |
| val categorize = (ls List<string>) string { | |
| match ls { | |
| case h :: nil -> "single item list" | |
| case h :: t :: nil -> "multi items list" | |
| else -> "empty list" | |
| } | |
| } | |
| // Type alias | |
| type Integer int | |
| type Greeter interface { | |
| fn Greet(name string) string | |
| } | |
| type Person class { | |
| Name string | |
| Age int | |
| fn Greet(name string) string = "Hello {name}" | |
| } | |
| type User class : Person { | |
| Id string | |
| override fn Greet(name string) string = "Hello from User, Mr. {name}" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment