Skip to content

Commit ff8c2d3

Browse files
committed
Добавлены примеры
1 parent a5a0219 commit ff8c2d3

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

examples/functions/calculator.own

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Simple parser example
2+
use "std"
3+
use "types"
4+
5+
operations = {
6+
"+" : def(a,b) = a+b,
7+
"-" : def(a,b) = a-b,
8+
"*" : def(a,b) = a*b,
9+
"/" : def(a,b) = a/b,
10+
"%" : def(a,b) = a%b,
11+
">" : def(a,b) = a>b,
12+
"<" : def(a,b) = a<b
13+
}
14+
15+
def calculate(expression) {
16+
pos = 0
17+
len = length(expression)
18+
19+
def isDigit(c) = 48 <= c && c <= 57
20+
21+
def parseNumber() {
22+
buffer = ""
23+
while (pos < len && isDigit(charAt(expression, pos))) {
24+
buffer += toChar(charAt(expression, pos))
25+
pos++
26+
}
27+
return number(buffer)
28+
}
29+
30+
def parseOperation() {
31+
while (pos < len && !arrayKeyExists(toChar(charAt(expression, pos)), operations)) {
32+
pos++
33+
}
34+
return operations[toChar(charAt(expression, pos++))]
35+
}
36+
37+
num1 = parseNumber()
38+
op = parseOperation()
39+
num2 = parseNumber()
40+
return op(num1, num2)
41+
}
42+
43+
println calculate("2+2")
44+
println calculate("400*16")
45+
println calculate("400/160")
46+
println calculate("3>4")

examples/functions/chain.own

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use "functional"
2+
3+
data = [1,2,3,4,5,6,7,8,9]
4+
chain(data,
5+
::filter, def(x) = x % 2 == 0,
6+
::map, def(x) = [x, x * x, x * x * x],
7+
::sortby, def(x) = -x[2],
8+
::foreach, ::echo
9+
)
10+

examples/network/telegram_api.own

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use "std"
2+
use "http"
3+
use "json"
4+
use "functional"
5+
6+
// Telegram API example
7+
8+
token = "YOUR_TOKEN"
9+
10+
def toParams(obj) {
11+
str = ""
12+
for k, v : obj
13+
str += k + "=" + v + "&"
14+
return str
15+
}
16+
def createRawUrl(method, params, token = "") = "https://api.telegram.org/bot" + token + "/" + method + "?"+params+"access_token="+token
17+
def createUrl(method, params, token = "") = createRawUrl(method, toParams(params), token)
18+
def invokeJson(method, params, callback) = http(createUrl(method, params, token), combine(::jsondecode, callback))
19+
def invoke(method, params, callback) = http(createUrl(method, params, token), callback)
20+
21+
def sendMessage(text = "", chatId = 1) {
22+
invoke("sendMessage", {
23+
"chat_id": chatId,
24+
"text": text
25+
}, ::echo)
26+
}
27+
28+
def getUpdates() = invoke("getUpdates", {}, ::echo)
29+
30+
31+
// Get updates in chat
32+
getUpdates()
33+
// Send message to chatId 1
34+
sendMessage("Hello", 1)
35+
// Send message to channel
36+
sendMessage("Hello", "@telegram_channel")

0 commit comments

Comments
 (0)