Advertisement
Guest User

LinqArray examples

a guest
Feb 4th, 2012
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.42 KB | None | 0 0
  1. Employee = class()
  2.  
  3. function Employee:init(name, position, address)
  4.     self.name = name
  5.     self.position = position
  6.     self.address = address
  7. end
  8.  
  9. Address = class()
  10.  
  11. function Address:init(houseNum, street, city, state, zip)
  12.     self.houseNum = houseNum
  13.     self.street = street
  14.     self.city = city
  15.     self.state = state
  16.     self.zip = zip
  17. end
  18.  
  19. function Address:hash()
  20.     return self.houseNum .. "_" .. self.street .. "_" .. self.city .. "_" .. self.state .. "_" .. self.zip
  21. end
  22.  
  23. function setup()
  24.     local emps = LinqArray()
  25.     print(emps:any())
  26.     emps:add(Employee("Bob", "Accountant", Address(111, "Example St.", "New York", "NY", 123456)))
  27.     emps:add(Employee("John", "CSR", Address(150, "Sample Dr.", "Buffalo", "NY", 123599)))
  28.     emps:add(Employee("Jane", "Accountant", Address(66, "Example St.", "New York", "NY", 123456)))
  29.     emps:add(Employee("Mary", "Secretary", Address(5, "Test Ln.", "Trenton", "NJ", 123305)))
  30.     emps:add(Employee("Mark", "CEO", Address(5, "Test Ln.", "Trenton", "NJ", 123305)))
  31.     print("any emps named Eric: ", emps:any(function(e) return e.name == "Eric" end))
  32.     
  33.     print("Emp count: ", emps:count())
  34.     
  35.     print()
  36.     nyEmps = emps:where(function(e) return e.address.state == "NY" end)
  37.     print("NY emp count: ", nyEmps:count())
  38.     print("First NY emp: ", nyEmps:first().name)
  39.     
  40.     print()
  41.     njEmps = emps:where(function(e) return e.address.state == "NJ" end)
  42.     print("NJ emp count: ", njEmps:count())
  43.     print("First NJ emp: ", njEmps:first().name)
  44.     
  45.     nyEmpCities = emps:where(function(e) return e.address.state == "NY" end):
  46.                        select(function(e) return e.address.city end):distinct()
  47.     print()
  48.     print("Distinct NY cities:")
  49.     for k, v in ipairs(nyEmpCities) do
  50.         print(v)
  51.     end
  52.     
  53.     print()
  54.     print("Distinct address hashes:")
  55.     local addrs = emps:select(function(e) return e.address end):distinct(Address.hash)
  56.     for k, v in ipairs(addrs) do
  57.         print(v:hash())
  58.     end
  59.     
  60.     print()
  61.     local css = LinqArray()
  62.     css:add({ name = "color", value = "red"})
  63.     css:add({ name = "text-align", value = "center"})
  64.     css:add({ name = "width", value = "150px"})
  65.     local cssDict = css:toDictionary(function(c) return c.name  end, 
  66.                                      function(c) return c.value end)
  67.     print("text-align: " .. cssDict["text-align"])
  68.     print("color: " .. cssDict["color"])
  69.     
  70.     print()
  71.     local lineItems = LinqArray()
  72.     lineItems:add({ recurAmount = 10, setupFee = 5 })
  73.     lineItems:add({ recurAmount = 17, setupFee = 9 })
  74.     lineItems:add({ recurAmount = 0, setupFee = 24 })
  75.     lineItems:add({ recurAmount = 168, setupFee = 47 })
  76.     print("sum recur amounts: ", lineItems:sum(function(i) return i.recurAmount end))
  77.     print("max recur amount: ", lineItems:max(function(i) return i.recurAmount end))
  78.     print("min recur amount: ", lineItems:min(function(i) return i.recurAmount end))
  79.     print("avg setup feea: ", lineItems:avg(function(i) return i.setupFee end))
  80.     
  81.     lineItems:removeWhere(function(i) return i.setupFee <= 10 end)
  82.     print("line item count with setup fee > 10: " .. lineItems:count())
  83.  
  84. end
  85.  
  86. function draw()
  87.     background(40, 40, 50)
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement