Software /
code /
prosody
Comparison
tools/migration/migrator/mtools.lua @ 4216:ff80a8471e86
tools/migration/*: Numerous changes and restructuring, and the addition of a Makefile
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 26 Feb 2011 00:23:48 +0000 |
parent | 4197:tools/migration/mtools.lua@bef732980436 |
child | 7881:4e3067272fae |
comparison
equal
deleted
inserted
replaced
4214:1674cd17557c | 4216:ff80a8471e86 |
---|---|
1 | |
2 | |
3 local print = print; | |
4 local t_insert = table.insert; | |
5 local t_sort = table.sort; | |
6 | |
7 module "mtools" | |
8 | |
9 function sorted(params) | |
10 | |
11 local reader = params.reader; -- iterator to get items from | |
12 local sorter = params.sorter; -- sorting function | |
13 local filter = params.filter; -- filter function | |
14 | |
15 local cache = {}; | |
16 for item in reader do | |
17 if filter then item = filter(item); end | |
18 if item then t_insert(cache, item); end | |
19 end | |
20 if sorter then | |
21 t_sort(cache, sorter); | |
22 end | |
23 local i = 0; | |
24 return function() | |
25 i = i + 1; | |
26 return cache[i]; | |
27 end; | |
28 | |
29 end | |
30 | |
31 function merged(reader, merger) | |
32 | |
33 local item1 = reader(); | |
34 local merged = { item1 }; | |
35 return function() | |
36 while true do | |
37 if not item1 then return nil; end | |
38 local item2 = reader(); | |
39 if not item2 then item1 = nil; return merged; end | |
40 if merger(item1, item2) then | |
41 --print("merged") | |
42 item1 = item2; | |
43 t_insert(merged, item1); | |
44 else | |
45 --print("unmerged", merged) | |
46 item1 = item2; | |
47 local tmp = merged; | |
48 merged = { item1 }; | |
49 return tmp; | |
50 end | |
51 end | |
52 end; | |
53 | |
54 end | |
55 | |
56 return _M; |