-
Notifications
You must be signed in to change notification settings - Fork 22
/
program.own
253 lines (210 loc) · 5.1 KB
/
program.own
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use math, std, functional
word = 2 + 2
word2 = PI + word
str = "a" * 5 + "ba" * 7 + "\n"
println str * "3" * 2
println "word = " + word
println "word2 = " + word2
println "1" > "abc"
if (1 <= 2) println "1 = 1"
else println "1 != 1"
if (40 < 50 || 50 > 60) {
println "true1"
println "true2"
i = 0
println "do while"
do {
println "i = " + i
i = i + 1
} while (i < 10)
i = 0
println "while"
while (i < 10) {
println "i = " + i + sin(i)
i = i + 1
}
println "for"
for i = 0, i < 10, i = i + 1 {
println "i = " + i
}
}
else print "false"
print "sin(PI) = " + sin(PI/2)
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)
print a + "\n"
arr = [1, "text", sum(10, 15), [], ["text", [90, [7 + 6, [50]]]]]
println arr + "\n"
arr[0] = arr[0] + 1000 + arr[2]
print arr + "\n"
print arr[4][1] + "\n"
arr[4][1] = "text"
println arr[4][1]
print "\n\n"
array = newarray(2, 2, 2, 2)
println array
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)
print "\n\n"
println mul(8, 5)
println cube(2)
functions = [add, sub, mul, div]
def function(f, a, b) = f(a, b)
for i = 0, i < 4, i = i + 1 {
println functions[i]
println function(functions[i], 6, 3)
}
// map
map = {"+" : add, "-" : sub, "*" : mul, "/" : div}
map["%"] = def(x,y) = x % y
map["pow"] = def(x,y) = pow(x, y)
println map["+"]
println function(map["+"], 4, 5)
foreach(map, def(op, func) = echo (4, op, 5, "=", func(4,5)))
foreach(arr, ::echo)
arr1 = [1,2,3]
arr1 = arr1 :: 4
arr2 = [5,6,7]
println arr1
println arr1 << arr2
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 + ", "
use types
println typeof(1)
println typeof("1")
println typeof(arr1)
println typeof({})
println typeof(add)
println typeof(number("1"))
println typeof(string(1))
thread(::inthread)
def inthread() = echo("this is a thread")
thread(def (str) { println str }, "this is a thread with arguments")
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)
println "Sum: " + reduce(squares, 0, def(x, y) = x + y)
nums = [[1, 2], [3], [], [4, 5]]
nums = flatmap(nums, IDENTITY)
println "flatmap"
foreach(nums, ::echo)
println "sort"
foreach(sort(nums, def(a,b) = b - a), ::echo)
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)
}*/
use json
println "json"
println jsonencode({
"name": "JSON Example",
"version": 1,
"arrayData": [
1, 2, 3, 4
],
"objectData": {
"key": "value",
10: "1000"
}
})
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)
println fact2(6)
functions = {
"add": def(a, b) = a + b,
"sub": def(a, b) = a - b,
"mul": def(a, b) = a * b,
"div": def(a, b) = a / b
}
println functions.add(2, functions.mul(2, 2))
println split("1/2/3/4/5/6", "/")
println join(nums, ", ")
println join(nums, "|", "/")
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
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]
println arrayRecursive([1, 2, 3, 4, 5, 6, 7])
def arrayRecursive(arr) = match arr {
case [head :: tail]: "[" + head + ", " + arrayRecursive(tail) + "]"
case []: "[]"
case last: "[" + last + ", []]"
}
def funcWithOptionalArgs(str, count = 5, prefix = "<", suffix = ">") = prefix + (str * count) + suffix
println funcWithOptionalArgs("*")
println funcWithOptionalArgs("+", 2)
println funcWithOptionalArgs("*", 10, "<!")
v1 = v2 = v3 = v4 = 5
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]
xarr[0]++
println xarr[0]
`extended word variable` = 9
println `extended word variable`
// Operator overloading
def `::`(v1, v2) = string(v1) + string(v2)
println 1 :: 2 :: 3
println "\u042a"
use date
d = newDate();
println d
println formatDate(d)
println formatDate(d, newFormat("yyyy-MM-dd HH:mm:ss, EEEE"))
println parseDate("2016/05/10", newFormat("yyyy/MM/dd"))
println toTimestamp(d)
println newDate(toTimestamp(d) - 100000)
try(def() = try + 2)
println try(def() = try(), def(type, message) = sprintf("Error handled:\ntype: %s\nmessage: %s", type, message))