ATuin

Functional Programming with lua

Jun 25th, 2012
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.93 KB | None | 0 0
  1. #!/bin/lua5.2
  2. --
  3. --
  4. package.path = package.path .. ";lua/?.lua"
  5. local lfunc = require "luaFunctional"
  6. lfunc.loadglobally(lfunc)
  7. --
  8. --
  9. VERSION = "0.1b"
  10. NAME = "multiBox"
  11. DESC = [[
  12. Manager for multiple dropBox accounts on the same machine
  13. ]]
  14. LICENSE = "GNU General Public License"
  15. USAGE = string.format([[
  16. Usage: %s [account_name account_command | command]
  17.  
  18.     account_name     :: Profile name for any of the multiple accounts defined.
  19.  
  20.     account_commands
  21.        start         :: Starts dropbox for this account
  22.        stop          :: Stops dropbox for this account
  23.        delete        :: Deletes account profile
  24.        status        :: Gets status for this account
  25.  
  26.     command
  27.        version       :: Shows current version
  28.        help          :: Shows this help
  29.        accounts      :: Lists all profiles avaible in the system
  30.  
  31. %s
  32.  
  33. This software comes without any warranty. Use at your own risk.
  34. If you like it, or you want to contribute with it, please give feedback.
  35. ]], NAME, LICENSE)
  36. --
  37.  
  38. function usage(ss)
  39.   return function ()
  40.     print(USAGE)
  41.   end
  42. end
  43.  
  44. function status(account)
  45.   return function ()
  46.     print("Status")
  47.   end
  48. end
  49.  
  50. function start(account)
  51.   return function ()
  52.     print("Starting " .. account)
  53.   end
  54. end
  55.  
  56. function stop(account)
  57.   return function ()
  58.     print("Stoping DropBox account " .. account)
  59.   end
  60. end
  61.  
  62. function accounts()
  63.   return function ()
  64.     print("Listing accounts")
  65.   end
  66. end
  67.  
  68. function version()
  69.   return function ()
  70.     print(string.format("%s %s [%s]\n\n%s", NAME, VERSION, LICENSE, DESC))
  71.   end
  72. end
  73.  
  74. eq1 = curry(eq,2)
  75.  
  76. function prepareCommand(tuple)
  77.   local eqCurry = curry(eq,2)
  78.  
  79.   local eqCommand = function (cmd)
  80.     return function (t)
  81.       return eq1(cmd)(t)
  82.     end
  83.   end
  84.  
  85.   getComplexCommand = function (t)
  86.     return casef(
  87.       {eqCommand("start"), start},
  88.       {eqCommand("stop"),  stop},
  89.       {eqCommand("status"), status},
  90.       {eqCommand("delete"), function() print("DELETE not defined") end},
  91.       {1, usage})(t[1])
  92.   end
  93.  
  94.   getSimpleCommand = function (a)
  95.     return casef(
  96.     {eqCommand("accounts"), accounts},
  97.     {eqCommand("version"),  version},
  98.     {1, usage})(a)
  99.   end
  100.  
  101.   return casef(
  102.     {function(t) return type(t) == "table" end, getComplexCommand},
  103.     {true, getSimpleCommand})(tuple)
  104. end
  105.  
  106. function prepareAccount (argv)
  107.   local account = table.head(argv)
  108.   local toTuple = function (a) return a, account end
  109.   return maptup(toTuple, table.tail(argv))
  110. end
  111.  
  112. function main(argc, argv)
  113.   mapCurry = curry(map,2)
  114.   lenEq = curry(function(n, t1) return n == #t1 end, 2)
  115.   lenGt = curry(function(n, t1) return n < #t1 end, 2)
  116.   lenLt = curry(function(m, t1) return n > #t1 end, 2)
  117.  
  118.   return casef(
  119.     {lenGt(1), compose(mapCurry(prepareCommand), prepareAccount)},
  120.     {lenEq(1), mapCurry(prepareCommand)},
  121.     {1 , {usage()}})(argv)
  122. end
  123.  
  124. local a = main(#arg, arg)
  125. for _,v in ipairs(a) do print(v()) end
Advertisement
Add Comment
Please, Sign In to add comment