Compare to the non-duplicated code in my Lisp redis client: https://github.com/mattsta/er/blob/681e4f91d0601d2997b2b5b7a... . In the Lisp version, each redis-cmd-* call is a macro created by a macro which calls another macro to create the function in a polymorphic way.
You are one step closer to Lisp enlightenment.
basic_execute_command = {
# snip
### SET COMMANDS ###
"sadd": "Add ``value`` to set ``name``"
, "scard": "Return the number of elements in set ``name``"
, "sismember": "Return a boolean indicating if ``value`` is a member of set ``name``"
, "smembers": "Return all members of the set ``name``"
, "smove": "Move ``value`` from set ``src`` to set ``dst`` atomically"
, "spop": "Remove and return a random member of set ``name``"
, "srandmember": "Return a random member of set ``name``"
, "srem": "Remove ``value`` from set ``name``"
# snip
}
def _basic_execute_command (self, cmd):
return lambda *args: self.execute_command(cmd.upper(), *args)
# snip
def __init__ (self, *args, **kwargs):
# snip
for cmd in self.basic_execute_command:
self.__dict__[cmd] = self._basic_execute_command(cmd)
# snip
This is pretty memory-inefficient if you have several redis objects, but I'm sure you could come up with something better than what I hacked together in a few minutes.I know that Lisp ranks above Python on the metaprogramming scale, but simply adding easy object introspection can allow for cool factoring constructs that a Java (or similar) programmer wouldn't even consider. It's possible to make copy-pasta in any language.
You could have all the redis connection instances share the same command dict, or make it a variable in the package.
---R: Return the string value of the key
function Sidereal:get(key) end
cmd("GET", "k")
---R: Set a key to a string returning the old value of the key
function Sidereal:getset(key, value) end
cmd("GETSET", "kv")
---R: Multi-get, return the strings values of the keys
function Sidereal:mget(key_list) end
cmd("MGET", "K")
(The comment and function lines are only there for luadoc; it could just be the cmd lines.)FWIW, I need to update it - antirez changed the protocol in a recent version, and I haven't gotten back to it yet. It should remove a good portion of the code, though - he switched everything to using the bulk form.
Building larger programs is about clean abstractions. What your doing only needs basic functions for abstraction, but when you try to do something larger you'll begin to need/want more ways to abstract your code. Then the differences will become clear.
But, you punted by not actually handling the errors. As others comments show, your example may need more abstraction tools once you try to handle the errors :)
getData = fetchDataFromSource . validateData . ProcessData
See http://www.haskell.org/haskellwiki/Function_composition function getData() {
return processData(validateData(fetchDataFromSource()));
}For a simple example like this, that might be the cleanest solution. For anything more complicated (like recursive descent parsing), a processing chain like this is an ideal application for a monad, where the binding between functions would be customized to avoid calling ProcessData altogether.
getData = fetchDataFromSource =<< validateData =<< ProcessData
By the way, the original getData with (.) is backwards. It should be: getData = ProcessData . validateData . fetchDataFromSource
I remember thinking when I first read through pg's essays a few years back and again now that you can do this in declarative languages as well using functions.
For example, my programs often looks something like this:
Could anyone explain why functional languages are better for this? It seems to me this is the same as "building the language up to your program".