-
-
Save oubiwann/4747567 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmodule lfe_riak | |
(import (rename erlang ((list_to_binary 1) l2b)) | |
(rename riak_object ((get_value 1) get)) | |
(from lists (foreach 2) (map 2) (foldl 3)) | |
(rename dict | |
((merge 3) make-merged-dict) | |
((new 0) make-dict) | |
((from_list 1) make-dict-from-list) | |
((to_list 1) make-list-from-dict))) | |
(export (hello 0) | |
(groceries 0))) | |
(defmacro with-local-client (body) | |
`(let ((client (: riak local_client))) | |
,body)) | |
;; (defmacro with-local-client (body) | |
;; `(let (((tuple 'ok client) (: riak local_client))) | |
;; ,body)) | |
(defmacro with-bucket (name body) | |
`(let ((bucket (list_to_binary ,name))) | |
,body)) | |
(defmacro create (key list) | |
`(list (l2b ,bucket) (l2b ,key) (list ,list)) | |
;; `(call ,client 'put | |
;; (: riak_object new (list (l2b ,bucket) (l2b ,key) (list ,list))) | |
;; 1) | |
) | |
(defmacro create-key (key) | |
`(list (list_to_binary ,bucket) (list_to_binary ,key))) | |
(defmacro keys (key . keys) | |
`(cons (key ,key) (keys ,keys))) | |
(defmacro map-reduce (keys map-fn reduce-fn) | |
`(let ((('ok reduced-data) | |
(call client 'mapred | |
,keys | |
(list | |
(tuple 'map (tuple 'qfun ,map-fn) 'none 'false) | |
(tuple 'reduce (tuple 'qfun ,reduce-fn) 'none 'true))))) | |
reduced-data)) | |
;; demo code below | |
(defun hello () | |
(with-local-client | |
(with-bucket | |
'"test" | |
(create '"hello" '"world")))) | |
(defun groceries () | |
(with-local-client | |
(with-bucket | |
'"groceries" | |
(create '"mine" '"eggs" '"sausage" '"bread" '"jelly" '"bacon" '"oj") | |
(create '"yours" '"eggs" '"bacon" '"bread" '"butter") | |
(create '"theirs" '"bread") | |
(flet ((map-fn ((groceries 'undefined 'none) | |
(make-dict-from-list | |
(map (get groceries) (lambda (i) (tuple i 1)))))) | |
(reduce-fn ((groceries 'none) | |
(foldl (lambda (item acc) | |
(make-merged-dict (lambda (_ x y) (+ x y)) | |
item acc)) | |
(make-dict) | |
groceries)))) | |
(let ((reduced-groceries (map-reduce (keys '"mine" '"yours") | |
map-fn reduce-fn))) | |
(make-list-from-dict reduced-groceries)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment