From ff8c2d39cd276d2265f95cba0c076f795b6ccc4c Mon Sep 17 00:00:00 2001 From: Victor Date: Fri, 8 Apr 2016 19:12:41 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/functions/calculator.own | 46 +++++++++++++++++++++++++++++++ examples/functions/chain.own | 10 +++++++ examples/network/telegram_api.own | 36 ++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 examples/functions/calculator.own create mode 100644 examples/functions/chain.own create mode 100644 examples/network/telegram_api.own diff --git a/examples/functions/calculator.own b/examples/functions/calculator.own new file mode 100644 index 0000000..571cbe8 --- /dev/null +++ b/examples/functions/calculator.own @@ -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) = a4") \ No newline at end of file diff --git a/examples/functions/chain.own b/examples/functions/chain.own new file mode 100644 index 0000000..cf8ac83 --- /dev/null +++ b/examples/functions/chain.own @@ -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 +) + diff --git a/examples/network/telegram_api.own b/examples/network/telegram_api.own new file mode 100644 index 0000000..6619d28 --- /dev/null +++ b/examples/network/telegram_api.own @@ -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") \ No newline at end of file