Skip to content

Instantly share code, notes, and snippets.

@dysinger
Created December 30, 2009 18:18
Show Gist options
  • Save dysinger/266262 to your computer and use it in GitHub Desktop.
Save dysinger/266262 to your computer and use it in GitHub Desktop.
(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