Abbreviations, automatic: Difference between revisions

Content added Content deleted
m (→‎Functional Python: Updated primitives)
m (→‎Functional Python: Updated one further primitive)
Line 2,394: Line 2,394:
(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):
(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>from itertools import (accumulate, chain)
<lang python>from itertools import (accumulate, chain)
from os.path import expanduser




Line 2,401: Line 2,402:
return next(
return next(
len(a[0]) for a in map(
len(a[0]) for a in map(
compose(nub)(
compose(nub)(map_(concat)),
map_(concat)
transpose(map(inits, xs))
),
transpose(
map(inits, xs)
)
) if n == len(a)
) if n == len(a)
)
)
Line 2,417: Line 2,414:
)))
)))
for i, n in enumerate(
for i, n in enumerate(
map(
map(compose(abbrevLen)(words), xs)
compose(abbrevLen)(words),
xs
)
):
):
print (n, ' ', xs[i])
print (n, ' ', xs[i])
Line 2,462: Line 2,456:
# readFile :: FilePath -> IO String
# readFile :: FilePath -> IO String
def readFile(fp):
def readFile(fp):
return open(fp).read()
with open(
expanduser(fp), 'r', encoding='utf-8'
) as f:
return f.read()