mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 00:34:20 +03:00
Добавлены примеры
This commit is contained in:
parent
a58e79a0d3
commit
1bcbe1978a
@ -1,5 +1,6 @@
|
||||
arr1 = [1, 2, 3, 4, 5]
|
||||
println arr1[0]
|
||||
println arr1[1]
|
||||
println arr1
|
||||
|
||||
use "std"
|
||||
@ -8,9 +9,11 @@ arr2[2] = 9
|
||||
arr2 = arr2 :: 4
|
||||
println arr2
|
||||
|
||||
// Append array
|
||||
arr3 = arr1 :: arr2
|
||||
arr4 = arr1 << arr2
|
||||
println arr3
|
||||
// Merge array
|
||||
arr4 = arr1 << arr2
|
||||
println arr4
|
||||
|
||||
|
6
examples/basics/extended_identifier.own
Normal file
6
examples/basics/extended_identifier.own
Normal file
@ -0,0 +1,6 @@
|
||||
`extended indetifier variable` = 9
|
||||
println `extended indetifier variable`
|
||||
|
||||
`(。◕‿◕。)` = 20
|
||||
`ʕ•ᴥ•ʔ` = 30
|
||||
println `(。◕‿◕。)` * `ʕ•ᴥ•ʔ`
|
11
examples/basics/operator_overloading.own
Normal file
11
examples/basics/operator_overloading.own
Normal 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]
|
@ -23,14 +23,15 @@ println match1("test")
|
||||
println match1("Two")
|
||||
println match1([2])
|
||||
|
||||
// Recursive factorial
|
||||
|
||||
println "\nRecursive factorial"
|
||||
println factorial(6)
|
||||
def factorial(n) = match n {
|
||||
case 0: 1
|
||||
case _: n * factorial(n - 1)
|
||||
}
|
||||
|
||||
// Pattern matching on arrays
|
||||
println "\nPattern matching on arrays"
|
||||
println printArrayRecursive([1, 2, 3, 4, 5, 6, 7])
|
||||
def printArrayRecursive(arr) = match arr {
|
||||
case [head :: tail]: "[" + head + ", " + printArrayRecursive(tail) + "]"
|
5
examples/basics/ternary_operator.own
Normal file
5
examples/basics/ternary_operator.own
Normal file
@ -0,0 +1,5 @@
|
||||
println "Ternary operator"
|
||||
a = 0
|
||||
b = 1
|
||||
println (a ? "text1" : "text2")
|
||||
println (b ? "text3" : "text4")
|
@ -3,8 +3,8 @@ use "std"
|
||||
def thread1() {
|
||||
i = 0
|
||||
while (i < 100) {
|
||||
print "i = " + i + "\n"
|
||||
i = i + 1
|
||||
println "i = " + i
|
||||
i++
|
||||
sleep(100)
|
||||
}
|
||||
}
|
||||
@ -14,8 +14,8 @@ thread(::thread1)
|
||||
|
||||
k = 0
|
||||
while (k < 10) {
|
||||
print "k = " + k + "\n"
|
||||
k = k + 1
|
||||
println "k = " + k
|
||||
k++
|
||||
sleep(1000)
|
||||
}
|
||||
|
11
examples/basics/types.own
Normal file
11
examples/basics/types.own
Normal 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))
|
@ -1,7 +0,0 @@
|
||||
use "std"
|
||||
|
||||
// Ternary operator
|
||||
a = 0
|
||||
b = 1
|
||||
print (a ? "text1" : "text2") + "\n"
|
||||
print (b ? "text3" : "text4") + "\n"
|
23
examples/functions/basics.own
Normal file
23
examples/functions/basics.own
Normal 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)))
|
@ -1,3 +1,4 @@
|
||||
use "std"
|
||||
use "functional"
|
||||
|
||||
data = [1,2,3,4,5,6,7,8,9]
|
||||
|
5
examples/functions/default_arguments.own
Normal file
5
examples/functions/default_arguments.own
Normal file
@ -0,0 +1,5 @@
|
||||
def funcWithOptionalArgs(str, count = 5, prefix = "<", suffix = ">") = prefix + (str * count) + suffix
|
||||
|
||||
println funcWithOptionalArgs("*")
|
||||
println funcWithOptionalArgs("+", 2)
|
||||
println funcWithOptionalArgs("*", 10, "<!")
|
7
examples/functions/filter_map.own
Normal file
7
examples/functions/filter_map.own
Normal 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)
|
6
examples/functions/flatmap.own
Normal file
6
examples/functions/flatmap.own
Normal file
@ -0,0 +1,6 @@
|
||||
use "std"
|
||||
use "functional"
|
||||
|
||||
nums = [[1, 2], [3], [], [4, 5]]
|
||||
nums = flatmap(nums, IDENTITY)
|
||||
foreach(nums, ::echo)
|
4
examples/functions/reduce.own
Normal file
4
examples/functions/reduce.own
Normal file
@ -0,0 +1,4 @@
|
||||
use "functional"
|
||||
|
||||
nums = [1,2,3,4,5]
|
||||
println "Sum: " + reduce(nums, 0, def(x, y) = x + y)
|
17
examples/functions/sortby.own
Normal file
17
examples/functions/sortby.own
Normal 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
11
examples/network/demo.own
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user