2011/04/06

json に変換可能な alist を walk

コード垂れ流し。
(use srfi-43)

(define (walk-json-alist alist :key (key-fun identity)(val-fun identity))
  (define (junction x)
    (cond ((pair? x)(walk-json-alist x :key-fun key-fun :val-fun val-fun))
          ((vector? x)(vector-case x))
          (else (val-fun x))))
  (define (vector-case v)
    (vector-map (^ (idx e)
                   (junction e))
                v))
  (map (^l (cons (key-fun (car l))
                 (junction (cdr l))))
       alist))

(define json1
  '((query . ((where . #(and #(< hoge 10)
                             #(= fuga foo)
                             #(< 5 baz 10)))
              (orderby . #(#(fugafuga desc)
                           #(hogehoge asc)))
              (limit . ALL)
              (offset . 0)))))

(walk-json-alist json1 :key-fun x->string :val-fun x->string)
;; (("query" ("where" . #("and" #("<" "hoge" "10") #("=" "fuga" "foo") #("<" "5" "baz" "10"))) ("orderby" . #(#("fugafuga" "desc") #("hogehoge" "asc"))) ("limit" . "ALL") ("offset" . "0")))

(walk-json-alist json1 :key-fun x->string)
;; (("query" ("where" . #(and #(< hoge 10) #(= fuga foo) #(< 5 baz 10))) ("orderby" . #(#(fugafuga desc) #(hogehoge asc))) ("limit" . ALL) ("offset" . 0)))

Scheme手習い

0 件のコメント:

コメントを投稿