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

This commit is contained in:
Victor 2016-04-08 19:12:41 +03:00
parent a5a0219c11
commit ff8c2d39cd
3 changed files with 92 additions and 0 deletions

View 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")

View 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
)

View 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")