Last active
November 28, 2023 20:58
-
-
Save matheusmv/421fcef9ad79647428080f52e990bf10 to your computer and use it in GitHub Desktop.
wesly examples
This file contains 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
object User { | |
id int | |
username string | |
password string | |
address object { | |
zip, street string | |
number int | |
} | |
} | |
func newUser(id int, username, password, zip, street string, number int) User { | |
return User{ | |
id: id, | |
username: username, | |
password: password, | |
address: newAddress(zip, street, number), // lazy | |
}; | |
} | |
func newAddress(zip, street string, number int) object{zip, street string number int} { | |
object Address { | |
zip, street string | |
number int | |
} | |
return Address{ | |
zip: zip, | |
street: street, | |
number: number, | |
}; | |
} | |
func logUser(user User) { | |
println(user.id); | |
println(user.username); | |
println(user.password); | |
println(user.address.zip); | |
println(user.address.street); | |
println(user.address.number); | |
} | |
const john = newUser(1, "john", "12345", "62800-001", "John Street Name", 125); | |
const carl = newUser(2, "carl", "12345", "62800-002", "Carl Street Name", 126); | |
logUser(john); | |
logUser(carl); |
This file contains 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
object Address { | |
number int | |
zip string | |
} | |
object User { | |
id int | |
username, password string | |
address Address | |
} | |
func NewUser(id int, username, password string, address Address) User { | |
return User{ | |
id: id, | |
username: username, | |
password: password, | |
address: address, | |
}; | |
} | |
func NewAddress(number int, zip string) Address { | |
return Address{ | |
number: number, | |
zip: zip, | |
}; | |
} | |
var alex = NewUser(1, "alex", "12345", NewAddress(145, "12444-777")); | |
var alexCopyWithAnomObj = object { | |
id int | |
username, password string | |
address Address | |
}{ | |
id: alex.id, | |
username: alex.username, | |
password: alex.password, | |
address: object { | |
number int | |
zip string | |
}{ | |
number: alex.address.number, | |
zip: alex.address.zip, | |
}, | |
}; | |
alexCopyWithAnomObj.username = "alex-copy"; | |
var numbers = [][]int{ | |
{1, 2, 3}, | |
{4, 5, 6}, | |
{7, 8, 9}, | |
}; | |
println(numbers[2][2] == 9); | |
println(numbers[2][2] != 9); | |
println(numbers[2][2] | 9); | |
println(numbers[2][2] ^ 9); | |
println(numbers[2][2] & 9); | |
println(numbers[2][2] < 9); | |
println(numbers[2][2] > 9); | |
println(numbers[2][2] <= 9); | |
println(numbers[2][2] >= 9); | |
println(numbers[2][2] << 9); | |
println(numbers[2][2] >> 9); | |
println(numbers[2][2] + 9); | |
println(numbers[2][2] - 9); | |
println(numbers[2][2] * 9); | |
println(numbers[2][2] / 9); | |
println(numbers[2][2] % 9); | |
println("id: " + alex.id + ", username: " + alex.username); | |
var | |
i int = numbers[2][2], | |
j float = 1.1, | |
k char = 'a', | |
l string = "ok", | |
m bool = false, | |
n func(int,int):int = func(a, b int) int { | |
return a + b; | |
}; | |
func fib(num int) int { | |
if (num <= 1) { | |
return num; | |
} | |
return fib(num - 1) + fib(num - 2); | |
} | |
println(fib(7)); | |
println(alex.username); | |
var nums = []int{1, 2, 3}; | |
nums[0] = 10; | |
println(nums); | |
nums[0] += 1; | |
func sum(arr []int) int { | |
var result = 0; | |
loop (var i = 0; i < len(arr); i++) { | |
result += arr[i]; | |
} | |
return result; | |
} | |
println(sum(nums)); | |
println(len(alex.username)); | |
const PI = 3.14; | |
println(PI); | |
const alexCopy = copy(alex); | |
alexCopy.username = "alexRef"; | |
alexCopy.address.number = 158; | |
println(alex); | |
println(alexCopy); | |
const numsCopy = copy(nums); | |
numsCopy[0] = 1; | |
println(numsCopy); | |
println(nums); | |
var stringTest = "hello", stringTestCopy = stringTest; | |
stringTestCopy = "world"; | |
println(stringTest); | |
println(stringTestCopy); | |
func setUsername(user User, username string) { | |
if (user != nil) { | |
user.username = username; | |
} | |
} | |
func setValue(l, r string) { | |
l = r; | |
} | |
setUsername(alexCopy, "alex copy"); | |
println(alexCopy); | |
println(alex); | |
setValue(stringTest, "only arrays and obj's as ref"); | |
println(stringTest); | |
func sumAll(nums ...int) int { | |
var result = 0; | |
loop (var i = 0; i < len(nums); i++) { | |
result += nums[i]; | |
} | |
return result; | |
} | |
println(sumAll(1, 2, 3, 4, 5, 6)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment