2015-07-11 21:18:58 +03:00
|
|
|
use "math"
|
|
|
|
use "std"
|
2016-01-09 21:21:17 +02:00
|
|
|
use "functional"
|
|
|
|
|
2015-05-25 23:20:10 +03:00
|
|
|
word = 2 + 2
|
|
|
|
word2 = PI + word
|
|
|
|
str = "a" * 5 + "ba" * 7 + "\n"
|
2016-01-09 14:11:15 +02:00
|
|
|
println str * "3" * 2
|
|
|
|
println "word = " + word
|
|
|
|
println "word2 = " + word2
|
|
|
|
println "1" > "abc"
|
|
|
|
if (1 <= 2) println "1 = 1"
|
|
|
|
else println "1 != 1"
|
2015-06-07 16:35:04 +03:00
|
|
|
if (40 < 50 || 50 > 60) {
|
2016-01-09 14:11:15 +02:00
|
|
|
println "true1"
|
|
|
|
println "true2"
|
2015-06-07 16:35:04 +03:00
|
|
|
i = 0
|
2016-01-09 14:11:15 +02:00
|
|
|
println "do while"
|
2015-06-12 21:46:13 +03:00
|
|
|
do {
|
2016-01-09 14:11:15 +02:00
|
|
|
println "i = " + i
|
2015-06-12 21:46:13 +03:00
|
|
|
i = i + 1
|
|
|
|
} while (i < 10)
|
|
|
|
i = 0
|
2016-01-09 14:11:15 +02:00
|
|
|
println "while"
|
2015-06-07 16:35:04 +03:00
|
|
|
while (i < 10) {
|
2016-01-09 14:11:15 +02:00
|
|
|
println "i = " + i + sin(i)
|
2015-06-07 16:35:04 +03:00
|
|
|
i = i + 1
|
|
|
|
}
|
2016-01-09 14:11:15 +02:00
|
|
|
println "for"
|
2015-06-07 16:35:04 +03:00
|
|
|
for i = 0, i < 10, i = i + 1 {
|
2016-01-09 14:11:15 +02:00
|
|
|
println "i = " + i
|
2015-06-07 16:35:04 +03:00
|
|
|
}
|
|
|
|
}
|
2015-06-15 14:45:52 +03:00
|
|
|
else print "false"
|
|
|
|
|
|
|
|
print "sin(PI) = " + sin(PI/2)
|
2015-06-19 16:24:22 +03:00
|
|
|
echo(1,2,3,"4","5","text",sin(0),cos(0),sin(PI),cos(PI),PI)
|
|
|
|
|
|
|
|
a = "print"
|
|
|
|
print a
|
|
|
|
|
|
|
|
def name(a,b) {
|
|
|
|
echo("a = ", a, " b = ", b)
|
|
|
|
}
|
|
|
|
|
|
|
|
def sum(a,b) {
|
|
|
|
a = -60
|
|
|
|
return a+b
|
|
|
|
}
|
|
|
|
name(1,"text")
|
|
|
|
print sum(10, 15)
|
|
|
|
|
2015-06-23 17:23:32 +03:00
|
|
|
print a + "\n"
|
|
|
|
|
|
|
|
arr = [1, "text", sum(10, 15), [], ["text", [90, [7 + 6, [50]]]]]
|
2016-01-09 14:11:15 +02:00
|
|
|
println arr + "\n"
|
2015-06-23 17:23:32 +03:00
|
|
|
arr[0] = arr[0] + 1000 + arr[2]
|
|
|
|
print arr + "\n"
|
2015-06-27 13:58:02 +03:00
|
|
|
print arr[4][1] + "\n"
|
|
|
|
arr[4][1] = "text"
|
2016-01-09 14:11:15 +02:00
|
|
|
println arr[4][1]
|
2015-06-27 13:58:02 +03:00
|
|
|
|
|
|
|
print "\n\n"
|
|
|
|
array = newarray(2, 2, 2, 2)
|
2016-01-09 14:11:15 +02:00
|
|
|
println array
|
2015-07-17 14:51:56 +03:00
|
|
|
|
2015-07-28 21:17:16 +03:00
|
|
|
add = def(a,b) = a+b
|
|
|
|
sub = def(a,b) = a-b
|
|
|
|
mul = def(a,b) = a*b
|
|
|
|
div = def(a,b) = a/b
|
|
|
|
cube = def(x) = x*mul(x, x)
|
2015-07-17 14:51:56 +03:00
|
|
|
print "\n\n"
|
2016-01-09 14:11:15 +02:00
|
|
|
println mul(8, 5)
|
|
|
|
println cube(2)
|
2015-07-17 14:51:56 +03:00
|
|
|
|
|
|
|
functions = [add, sub, mul, div]
|
2015-07-28 21:17:16 +03:00
|
|
|
def function(f, a, b) = f(a, b)
|
2015-07-17 14:51:56 +03:00
|
|
|
for i = 0, i < 4, i = i + 1 {
|
2016-01-09 14:11:15 +02:00
|
|
|
println functions[i]
|
|
|
|
println function(functions[i], 6, 3)
|
2016-01-08 12:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// map
|
2016-01-15 21:42:04 +02:00
|
|
|
map = {"+" : add, "-" : sub, "*" : mul, "/" : div}
|
2016-01-08 22:58:39 +02:00
|
|
|
map["%"] = def(x,y) = x % y
|
|
|
|
map["pow"] = def(x,y) = pow(x, y)
|
2016-01-09 14:11:15 +02:00
|
|
|
println map["+"]
|
|
|
|
println function(map["+"], 4, 5)
|
2016-01-08 21:32:57 +02:00
|
|
|
foreach(map, def(op, func) = echo (4, op, 5, "=", func(4,5)))
|
2016-01-08 21:58:20 +02:00
|
|
|
foreach(arr, ::echo)
|
2016-01-08 22:43:39 +02:00
|
|
|
|
|
|
|
arr1 = [1,2,3]
|
|
|
|
arr1 = arr1 :: 4
|
|
|
|
arr2 = [5,6,7]
|
2016-01-09 14:11:15 +02:00
|
|
|
println arr1
|
|
|
|
println arr1 << arr2
|
2016-01-08 22:58:39 +02:00
|
|
|
|
2016-01-09 13:13:04 +02:00
|
|
|
for op, func : map {
|
|
|
|
echo (4, op, 5, "=", func(4,5))
|
|
|
|
}
|
|
|
|
|
|
|
|
for v : arr1 print "" + v + ", "
|
|
|
|
print "\n"
|
|
|
|
for (v : arr1 << arr2) print "" + v + ", "
|
|
|
|
print "\n"
|
|
|
|
for v : [1,2,3,4,5,6,7,8,9] print "" + v + ", "
|
|
|
|
|
2016-01-09 14:00:07 +02:00
|
|
|
use "types"
|
2016-01-09 14:11:15 +02:00
|
|
|
println typeof(1)
|
|
|
|
println typeof("1")
|
|
|
|
println typeof(arr1)
|
|
|
|
println typeof({})
|
|
|
|
println typeof(add)
|
2016-01-09 14:00:07 +02:00
|
|
|
|
2016-01-09 14:11:15 +02:00
|
|
|
println typeof(number("1"))
|
2016-01-09 14:54:06 +02:00
|
|
|
println typeof(string(1))
|
|
|
|
|
|
|
|
thread(::inthread)
|
|
|
|
def inthread() = echo("this is a thread")
|
|
|
|
thread(def (str) { println str }, "this is a thread with arguments")
|
|
|
|
|
2016-01-09 21:21:17 +02:00
|
|
|
println "functional"
|
|
|
|
nums = [1,2,3,4,5,6,7,8,9,10]
|
|
|
|
nums = filter(nums, def(x) = x % 2 == 0)
|
|
|
|
squares = map(nums, def(x) = x * x)
|
|
|
|
foreach(squares, ::echo)
|
2016-01-12 21:14:05 +02:00
|
|
|
println "Sum: " + reduce(squares, 0, def(x, y) = x + y)
|
|
|
|
|
2016-01-19 14:59:04 +02:00
|
|
|
nums = [[1, 2], [3], [], [4, 5]]
|
|
|
|
nums = flatmap(nums, IDENTITY)
|
|
|
|
println "flatmap"
|
|
|
|
foreach(nums, ::echo)
|
2016-01-19 17:59:23 +02:00
|
|
|
println "sort"
|
|
|
|
foreach(sort(nums, def(a,b) = b - a), ::echo)
|
2016-01-19 14:59:04 +02:00
|
|
|
|
2016-01-12 21:14:05 +02:00
|
|
|
use "http"
|
|
|
|
|
2016-01-12 23:14:56 +02:00
|
|
|
/*http("http://jsonplaceholder.typicode.com/users", "POST", {"name": "OwnLang", "versionCode": 10}, def(v) {
|
2016-01-12 21:14:05 +02:00
|
|
|
println "Added: " + v
|
|
|
|
http("http://jsonplaceholder.typicode.com/users/2", "PATCH", {"name": "Patched Name"}, ::http_get_demo)
|
|
|
|
})
|
|
|
|
def http_get_demo(v) {
|
|
|
|
println "Updated: " + v
|
|
|
|
http("http://jsonplaceholder.typicode.com/users", ::echo)
|
2016-01-12 23:14:56 +02:00
|
|
|
}*/
|
|
|
|
|
|
|
|
use "json"
|
|
|
|
println "json"
|
|
|
|
println jsonencode({
|
|
|
|
"name": "JSON Example",
|
|
|
|
"version": 1,
|
|
|
|
"arrayData": [
|
|
|
|
1, 2, 3, 4
|
|
|
|
],
|
|
|
|
"objectData": {
|
|
|
|
"key": "value",
|
|
|
|
10: "1000"
|
|
|
|
}
|
2016-01-15 21:42:04 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
println ""
|
|
|
|
|
|
|
|
|
|
|
|
def fact1(n) = match n {
|
|
|
|
case 0: 1
|
|
|
|
case n: n * fact1(n - 1)
|
|
|
|
}
|
|
|
|
def fact2(n) = match n {
|
|
|
|
case n if n == 0: 1
|
|
|
|
case _: n * fact2(n - 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
println fact1(6)
|
2016-01-19 14:59:04 +02:00
|
|
|
println fact2(6)
|
|
|
|
|
|
|
|
class = {
|
|
|
|
"add": def(a, b) = a + b,
|
|
|
|
"sub": def(a, b) = a - b,
|
|
|
|
"mul": def(a, b) = a * b,
|
|
|
|
"div": def(a, b) = a / b
|
|
|
|
}
|
|
|
|
|
2016-01-22 18:15:58 +02:00
|
|
|
println class.add(2, class.mul(2, 2))
|
|
|
|
|
|
|
|
println split("1/2/3/4/5/6", "/")
|
|
|
|
println join(nums, ", ")
|
|
|
|
println join(nums, "|", "/")
|
2016-02-09 13:28:40 +02:00
|
|
|
println join(nums, ", ", "[", "]")
|
|
|
|
|
|
|
|
// Destructuring assignment
|
|
|
|
arr = ["a", "b", "c"]
|
|
|
|
extract(var1, var2, var3) = arr
|
|
|
|
echo(var1, var2, var3)
|
|
|
|
// Swap
|
|
|
|
extract(var2, var1) = [var1, var2]
|
|
|
|
echo(var1, var2)
|
|
|
|
|
|
|
|
extract(, , var4) = arr
|
2016-02-13 12:01:11 +02:00
|
|
|
println var4
|
|
|
|
|
|
|
|
array1 = [[1, 2], [3], [4, 5], [6]]
|
|
|
|
println array1[0][1]
|
|
|
|
object1 = {"arr": array1}
|
|
|
|
println object1.arr
|
|
|
|
println object1.arr[0][1]
|
|
|
|
object1.arr[0][1] = "str"
|
|
|
|
println object1.arr[0][1]
|
2016-02-13 15:19:59 +02:00
|
|
|
|
|
|
|
println arrayRecursive([1, 2, 3, 4, 5, 6, 7])
|
|
|
|
def arrayRecursive(arr) = match arr {
|
|
|
|
case [head :: tail]: "[" + head + ", " + arrayRecursive(tail) + "]"
|
|
|
|
case []: "[]"
|
|
|
|
case last: "[" + last + ", []]"
|
2016-02-13 18:08:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def funcWithOptionalArgs(str, count = 5, prefix = "<", suffix = ">") = prefix + (str * count) + suffix
|
|
|
|
|
|
|
|
println funcWithOptionalArgs("*")
|
|
|
|
println funcWithOptionalArgs("+", 2)
|
2016-02-13 18:46:41 +02:00
|
|
|
println funcWithOptionalArgs("*", 10, "<!")
|
|
|
|
|
|
|
|
v1 = v2 = v3 = v4 = 5
|
2016-02-13 21:57:42 +02:00
|
|
|
echo(v1, v2, v3, v4)
|
|
|
|
|
|
|
|
x = 5
|
|
|
|
x += 10
|
|
|
|
x *= 2
|
|
|
|
println x
|
|
|
|
|
|
|
|
xarr = [0, 5, 2]
|
|
|
|
xarr[0] += xarr[1]
|
|
|
|
xarr[0] *= xarr[2]
|
2016-02-14 20:17:54 +02:00
|
|
|
xarr[0]++
|
2016-02-18 13:19:57 +02:00
|
|
|
println xarr[0]
|
|
|
|
|
|
|
|
`extended word variable` = 9
|
2016-02-18 13:20:26 +02:00
|
|
|
println `extended word variable`
|
|
|
|
|
|
|
|
// Operator overloading
|
|
|
|
def `::`(v1, v2) = string(v1) + string(v2)
|
2016-02-18 22:02:37 +02:00
|
|
|
println 1 :: 2 :: 3
|
|
|
|
|
2016-02-19 12:20:20 +02:00
|
|
|
println "\u042a"
|
|
|
|
|
2016-03-04 20:21:31 +02:00
|
|
|
include "visitor.own"
|