mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 08:44:20 +03:00
Добавлены примеры
This commit is contained in:
parent
a5a0219c11
commit
ff8c2d39cd
46
examples/functions/calculator.own
Normal file
46
examples/functions/calculator.own
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
// Simple parser example
|
||||||
|
use "std"
|
||||||
|
use "types"
|
||||||
|
|
||||||
|
operations = {
|
||||||
|
"+" : def(a,b) = a+b,
|
||||||
|
"-" : def(a,b) = a-b,
|
||||||
|
"*" : def(a,b) = a*b,
|
||||||
|
"/" : def(a,b) = a/b,
|
||||||
|
"%" : def(a,b) = a%b,
|
||||||
|
">" : def(a,b) = a>b,
|
||||||
|
"<" : def(a,b) = a<b
|
||||||
|
}
|
||||||
|
|
||||||
|
def calculate(expression) {
|
||||||
|
pos = 0
|
||||||
|
len = length(expression)
|
||||||
|
|
||||||
|
def isDigit(c) = 48 <= c && c <= 57
|
||||||
|
|
||||||
|
def parseNumber() {
|
||||||
|
buffer = ""
|
||||||
|
while (pos < len && isDigit(charAt(expression, pos))) {
|
||||||
|
buffer += toChar(charAt(expression, pos))
|
||||||
|
pos++
|
||||||
|
}
|
||||||
|
return number(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
def parseOperation() {
|
||||||
|
while (pos < len && !arrayKeyExists(toChar(charAt(expression, pos)), operations)) {
|
||||||
|
pos++
|
||||||
|
}
|
||||||
|
return operations[toChar(charAt(expression, pos++))]
|
||||||
|
}
|
||||||
|
|
||||||
|
num1 = parseNumber()
|
||||||
|
op = parseOperation()
|
||||||
|
num2 = parseNumber()
|
||||||
|
return op(num1, num2)
|
||||||
|
}
|
||||||
|
|
||||||
|
println calculate("2+2")
|
||||||
|
println calculate("400*16")
|
||||||
|
println calculate("400/160")
|
||||||
|
println calculate("3>4")
|
10
examples/functions/chain.own
Normal file
10
examples/functions/chain.own
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
use "functional"
|
||||||
|
|
||||||
|
data = [1,2,3,4,5,6,7,8,9]
|
||||||
|
chain(data,
|
||||||
|
::filter, def(x) = x % 2 == 0,
|
||||||
|
::map, def(x) = [x, x * x, x * x * x],
|
||||||
|
::sortby, def(x) = -x[2],
|
||||||
|
::foreach, ::echo
|
||||||
|
)
|
||||||
|
|
36
examples/network/telegram_api.own
Normal file
36
examples/network/telegram_api.own
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
use "std"
|
||||||
|
use "http"
|
||||||
|
use "json"
|
||||||
|
use "functional"
|
||||||
|
|
||||||
|
// Telegram API example
|
||||||
|
|
||||||
|
token = "YOUR_TOKEN"
|
||||||
|
|
||||||
|
def toParams(obj) {
|
||||||
|
str = ""
|
||||||
|
for k, v : obj
|
||||||
|
str += k + "=" + v + "&"
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
def createRawUrl(method, params, token = "") = "https://api.telegram.org/bot" + token + "/" + method + "?"+params+"access_token="+token
|
||||||
|
def createUrl(method, params, token = "") = createRawUrl(method, toParams(params), token)
|
||||||
|
def invokeJson(method, params, callback) = http(createUrl(method, params, token), combine(::jsondecode, callback))
|
||||||
|
def invoke(method, params, callback) = http(createUrl(method, params, token), callback)
|
||||||
|
|
||||||
|
def sendMessage(text = "", chatId = 1) {
|
||||||
|
invoke("sendMessage", {
|
||||||
|
"chat_id": chatId,
|
||||||
|
"text": text
|
||||||
|
}, ::echo)
|
||||||
|
}
|
||||||
|
|
||||||
|
def getUpdates() = invoke("getUpdates", {}, ::echo)
|
||||||
|
|
||||||
|
|
||||||
|
// Get updates in chat
|
||||||
|
getUpdates()
|
||||||
|
// Send message to chatId 1
|
||||||
|
sendMessage("Hello", 1)
|
||||||
|
// Send message to channel
|
||||||
|
sendMessage("Hello", "@telegram_channel")
|
Loading…
Reference in New Issue
Block a user