From 4de7383ed16f9908c34afb893374f4dd01fbfc06 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Fri, 11 Mar 2016 12:23:55 +0100 Subject: Simplify JSON result interpretation logic --- bot.scm | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/bot.scm b/bot.scm index 1802277..bbd658d 100644 --- a/bot.scm +++ b/bot.scm @@ -1,42 +1,39 @@ (include "telebot.scm") (import (prefix telebot telebot:)) -(use vector-lib) (use loops) +(use vector-lib) +(use data-structures) (define (assure-list value) (if (list? value) value (list))) -(define (find-pair symbol nodes) - (find (lambda (x) (equal? symbol (car x))) - (filter pair? nodes))) - (define (resolve-query query tree) - (fold-right (lambda (x y) (find-pair x (assure-list y))) + (fold-right (lambda (x y) (alist-ref x (assure-list y))) tree (reverse query))) (define (updates-for-each func updates) (vector-for-each (lambda (i u) (func u)) - (cdr updates))) + updates)) (define offset 0) (define token (car (command-line-arguments))) (define (print-message msg) - (print (cdr (resolve-query (list 'message 'from 'first_name) msg)) - ": " - (cdr (resolve-query (list 'message 'text) msg)) - " (" - (cdr (resolve-query (list 'update_id) msg)) - ")")) + (print (resolve-query (list 'message 'from 'first_name) msg) + ": " + (resolve-query (list 'message 'text) msg) + " (" + (resolve-query (list 'update_id) msg) + ")")) (define (echo-message msg) - (let ((chat_id (cdr (resolve-query (list 'message 'from 'id) msg))) - (text (cdr (resolve-query (list 'message 'text) msg))) - (message_id (cdr (resolve-query (list 'message 'message_id) msg)))) + (let ((chat_id (resolve-query (list 'message 'from 'id) msg)) + (text (resolve-query (list 'message 'text) msg)) + (message_id (resolve-query (list 'message 'message_id) msg))) (telebot:sendMessage token chat_id: chat_id text: text @@ -47,7 +44,7 @@ (begin (print-message u) (echo-message u) (set! offset - (+ 1 (cdr (resolve-query (list 'update_id) u)))))) + (+ 1 (resolve-query (list 'update_id) u))))) (resolve-query (list 'result) (telebot:getUpdates token offset: offset -- cgit v1.2.3