Skip to content

Commit

Permalink
Extra list utilities, refactoring of some existing ones
Browse files Browse the repository at this point in the history
  • Loading branch information
batterseapower committed Dec 6, 2009
1 parent 34de87a commit 43af5c3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pinyin/forms/preferencescontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
8 changes: 2 additions & 6 deletions pinyin/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
22 changes: 10 additions & 12 deletions pinyin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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
Expand All @@ -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 = []
Expand Down

0 comments on commit 43af5c3

Please sign in to comment.