Permalink
Browse files

Extra list utilities, refactoring of some existing ones

  • Loading branch information...
1 parent 34de87a commit 43af5c3419bb6d46fb4abfc79159330c28378d2b @batterseapower batterseapower committed Dec 6, 2009
Showing with 13 additions and 19 deletions.
  1. +1 −1 pinyin/forms/preferencescontroller.py
  2. +2 −6 pinyin/tests/utils.py
  3. +10 −12 pinyin/utils.py
@@ -70,7 +70,7 @@ def setUpViewPreview(self):
keyedfieldnames.append((key, fieldname, wantcheckbox))
# Build the fields in order of the label we assign to them: better than hash order!
- checkwidgets = self.view.setupFields(sorted(keyedfieldnames, pinyin.utils.bySecond))
+ checkwidgets = self.view.setupFields(sorted(keyedfieldnames, pinyin.utils.using(pinyin.utils.snd)))
# Set up all the checkboxes to map to the corresponding control flags
for key, checkwidget in checkwidgets.items():
View
@@ -43,12 +43,8 @@ def testSortedUsing(self):
items = { "hello" : 1, "wide" : 2, "world" : 4 }
self.assertEquals(sorted(["wide", "hello", "world"], using(items.get, inReverse())), ["world", "wide", "hello"])
self.assertEquals(sorted(["wide", "hello", "world"], using(items.get)), ["hello", "wide", "world"])
-
- def testSortedByFirst(self):
- self.assertEquals(sorted([(5, "1"), (2, "4"), (3, "2"), (1, "5"), (4, "3")], byFirst), [(1, "5"), (2, "4"), (3, "2"), (4, "3"), (5, "1")])
-
- def testSortedBySecond(self):
- self.assertEquals(sorted([(5, "1"), (2, "4"), (3, "2"), (1, "5"), (4, "3")], bySecond), [(5, "1"), (3, "2"), (4, "3"), (2, "4"), (1, "5")])
+ self.assertEquals(sorted([(5, "1"), (2, "4"), (3, "2"), (1, "5"), (4, "3")], using(fst)), [(1, "5"), (2, "4"), (3, "2"), (4, "3"), (5, "1")])
+ self.assertEquals(sorted([(5, "1"), (2, "4"), (3, "2"), (1, "5"), (4, "3")], using(snd)), [(5, "1"), (3, "2"), (4, "3"), (2, "4"), (1, "5")])
def testSortedInReverse(self):
self.assertEquals(sorted([5, 2, 3, 1, 4], inReverse()), [5, 4, 3, 2, 1])
View
@@ -308,18 +308,6 @@ def parseHtmlColor(color):
def using(proj, how=cmp):
return lambda x, y: how(proj(x), proj(y))
-"""
-Sorting combinator: sort by first element
-"""
-def byFirst(x, y):
- return cmp(x[0], y[0])
-
-"""
-Sorting combinator: sort by second element
-"""
-def bySecond(x, y):
- return cmp(x[1], y[1])
-
"""
Sorting combinator: reverse order:
"""
@@ -389,6 +377,10 @@ def inplacefilter(pred, list):
if not pred(list[i - 1]):
del list[i - 1]
+fst = lambda x: x[0]
+snd = lambda x: x[1]
+thd = lambda x: x[2]
+
def first(f):
def go(xy):
x, y = xy
@@ -409,6 +401,12 @@ def cond(test, tb, fb):
else:
return fb()
+def concatmap(f, xs):
+ return concat(map(f, xs))
+
+def partition(p, xs):
+ return (filter(p, xs), filter(lambda x: not p(x), xs))
+
def intersperse(what, things):
first = True
result = []

0 comments on commit 43af5c3

Please sign in to comment.