Добавлены примеры

This commit is contained in:
Victor 2016-06-02 17:43:55 +03:00
parent a58e79a0d3
commit 1bcbe1978a
20 changed files with 118 additions and 14 deletions

View File

@ -1,5 +1,6 @@
arr1 = [1, 2, 3, 4, 5] arr1 = [1, 2, 3, 4, 5]
println arr1[0] println arr1[0]
println arr1[1]
println arr1 println arr1
use "std" use "std"
@ -8,9 +9,11 @@ arr2[2] = 9
arr2 = arr2 :: 4 arr2 = arr2 :: 4
println arr2 println arr2
// Append array
arr3 = arr1 :: arr2 arr3 = arr1 :: arr2
arr4 = arr1 << arr2
println arr3 println arr3
// Merge array
arr4 = arr1 << arr2
println arr4 println arr4

View File

@ -0,0 +1,6 @@
`extended indetifier variable` = 9
println `extended indetifier variable`
`(。◕‿◕。)` = 20
`ʕ•ᴥ•ʔ` = 30
println `(。◕‿◕。)` * `ʕ•ᴥ•ʔ`

View File

@ -0,0 +1,11 @@
use "types"
use "math"
println "Operator overloading"
def `::`(v1, v2) = string(v1) + string(v2)
print "1 :: 2 :: 3 = "
println 1 :: 2 :: 3
def `^`(v1, v2) = pow(v1[0], v2[0])
print "[2] ^ [7] = "
println [2] ^ [7]

View File

@ -23,14 +23,15 @@ println match1("test")
println match1("Two") println match1("Two")
println match1([2]) println match1([2])
// Recursive factorial
println "\nRecursive factorial"
println factorial(6) println factorial(6)
def factorial(n) = match n { def factorial(n) = match n {
case 0: 1 case 0: 1
case _: n * factorial(n - 1) case _: n * factorial(n - 1)
} }
// Pattern matching on arrays println "\nPattern matching on arrays"
println printArrayRecursive([1, 2, 3, 4, 5, 6, 7]) println printArrayRecursive([1, 2, 3, 4, 5, 6, 7])
def printArrayRecursive(arr) = match arr { def printArrayRecursive(arr) = match arr {
case [head :: tail]: "[" + head + ", " + printArrayRecursive(tail) + "]" case [head :: tail]: "[" + head + ", " + printArrayRecursive(tail) + "]"

View File

@ -0,0 +1,5 @@
println "Ternary operator"
a = 0
b = 1
println (a ? "text1" : "text2")
println (b ? "text3" : "text4")

View File

@ -3,8 +3,8 @@ use "std"
def thread1() { def thread1() {
i = 0 i = 0
while (i < 100) { while (i < 100) {
print "i = " + i + "\n" println "i = " + i
i = i + 1 i++
sleep(100) sleep(100)
} }
} }
@ -14,8 +14,8 @@ thread(::thread1)
k = 0 k = 0
while (k < 10) { while (k < 10) {
print "k = " + k + "\n" println "k = " + k
k = k + 1 k++
sleep(1000) sleep(1000)
} }

11
examples/basics/types.own Normal file
View File

@ -0,0 +1,11 @@
use "std"
use "types"
println typeof(1)
println typeof("1")
println typeof([])
println typeof({})
println typeof(def() = 0)
println typeof(number("1"))
println typeof(string(1))

View File

@ -1,7 +0,0 @@
use "std"
// Ternary operator
a = 0
b = 1
print (a ? "text1" : "text2") + "\n"
print (b ? "text3" : "text4") + "\n"

View File

@ -0,0 +1,23 @@
use "std"
use "math"
use "functional"
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)
println mul(8, 5)
println cube(2)
functions = [add, sub, mul, div]
for f : functions {
println f
println f(6, 3)
}
map = {"+" : add, "-" : sub, "*" : mul, "/" : div}
map["%"] = def(x,y) = x % y
map["pow"] = def(x,y) = pow(x, y)
foreach(map, def(op, func) = echo (4, op, 5, "=", func(4,5)))

View File

@ -1,3 +1,4 @@
use "std"
use "functional" use "functional"
data = [1,2,3,4,5,6,7,8,9] data = [1,2,3,4,5,6,7,8,9]

View File

@ -0,0 +1,5 @@
def funcWithOptionalArgs(str, count = 5, prefix = "<", suffix = ">") = prefix + (str * count) + suffix
println funcWithOptionalArgs("*")
println funcWithOptionalArgs("+", 2)
println funcWithOptionalArgs("*", 10, "<!")

View File

@ -0,0 +1,7 @@
use "std"
use "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)

View File

@ -0,0 +1,6 @@
use "std"
use "functional"
nums = [[1, 2], [3], [], [4, 5]]
nums = flatmap(nums, IDENTITY)
foreach(nums, ::echo)

View File

@ -0,0 +1,4 @@
use "functional"
nums = [1,2,3,4,5]
println "Sum: " + reduce(nums, 0, def(x, y) = x + y)

View File

@ -0,0 +1,17 @@
use "std"
use "functional"
nums = [1,2,3,4,5]
println "Sort numbers in descending order"
numsDesc = sortby(nums, def(x) = -x)
foreach(numsDesc, ::echo)
str = ["http", "android", "types", "std", "canvas"]
println "\nSort strings in ascending order"
strSorted = sortby(str, IDENTITY)
foreach(strSorted, ::echo)
println "\nSort strings by length"
strSortedByLength = sortby(str, def(x) = length(x))
//strSortedByLength = sortby(str, ::length) // short syntax
foreach(strSortedByLength, ::echo)

11
examples/network/demo.own Normal file
View File

@ -0,0 +1,11 @@
use "std"
use "http"
http("http://jsonplaceholder.typicode.com/users", "POST", {"name": "OwnLang", "versionCode": 10}, def(v) {
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)
}