Last active
December 10, 2015 00:39
-
-
Save defHLT/4353111 to your computer and use it in GitHub Desktop.
This file contains 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
# See "Lisp Macros in 20 Minutes" | |
# at http://www.slideshare.net/pcalcado/lisp-macros-in-20-minutes-featuring-clojure-presentation | |
# Ruby implementation var. 1 { | |
def from(names) | |
keys, vals = [], [] | |
i = -1 | |
names.each do |name| | |
r = yield name | |
if r[:where] | |
vals << r[:select] | |
keys << r[:orderby] | |
end | |
end | |
vals.sort_by { keys[i+=1] } | |
end | |
# } | |
# Use it like: | |
names = %w{Burke Connor Frank Everett Albert George Harris David} | |
query = from(names) do |n| { | |
where: n.length==5, | |
orderby: n, | |
select: n.upcase} | |
end | |
puts query | |
puts | |
# Ruby implementation var. 2 { | |
def from(names, r) | |
names.select! { |x| r[:where][x] } | |
names.sort_by! { |x| r[:orderby][x] } | |
names.map { |x| r[:select][x] } | |
end | |
# } | |
# Use it like: | |
names = %w{Burke Connor Frank Everett Albert George Harris David} | |
query = from(names, | |
where: -> x {x.length == 5}, | |
orderby: -> x {x}, | |
select: -> x {x.upcase}) | |
puts query | |
# File output: { | |
#BURKE | |
#DAVID | |
#FRANK | |
#BURKE | |
#DAVID | |
#FRANK | |
# } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment