Abbreviations, automatic: Difference between revisions
m
→Functional Python: Updated primitives
m (→Functional Python: Updated primitives) |
|||
Line 2,393:
Composition of existing generics – just one new function.
(An optimisation for higher levels of code reuse, faster code development, and easier refactoring. Note that the generic primitives are curried, allowing for more flexible composition):
<lang python>
# abbrevLen :: [String] -> Int
def abbrevLen(xs):
Line 2,402:
len(a[0]) for a in map(
compose(nub)(
),
transpose(
Line 2,409:
) if n == len(a)
)
# TEST ----------------------------------------------------
def main():
Line 2,423:
):
print (n, ' ', xs[i])
# GENERIC -------------------------------------------------
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
def compose(g):
return lambda f: lambda x: g(f(x))
# concat :: [String] -> String
def concat(xs):
return ''.join(xs)
# inits :: [a] -> [[a]]
def inits(xs):
Line 2,443:
[]
)(list(xs))
# lines :: String -> [String]
def lines(s):
return s.splitlines()
# map :: (a -> b) -> [a] -> [b]
def
return lambda xs: list(map(f, xs))
# nub :: [a] -> [a]
def nub(xs):
return list(dict.fromkeys(xs))
# readFile :: FilePath -> IO String
def readFile(fp):
return open(fp).read()
# scanl is like reduce, but returns a succession of
# intermediate values, building from the left.
Line 2,470:
def scanl(f):
return lambda a: lambda xs: (
)
# strip :: String -> String
def strip(s):
return s.strip()
# transpose :: [[a]] -> [[a]]
def transpose(xs):
return map(list, zip(*xs))
# words :: String -> [String]
def words(s):
return s.split()
# MAIN ---
if __name__ == '__main__':
|