Multisplit: Difference between revisions
m
→Python :: Functional: Tidied
m (→Python :: Functional: Tidied) |
|||
Line 1,623:
def multiSplit(separators):
'''List of triples:
[(token, separator, start index of separator
'''
def go(s):
Line 1,631:
inDelim = offset > i
return maybe(
(
tokens if inDelim
else c + tokens, parts, offset
)(
lambda x: (
'',
[(tokens, x, i)] + parts,
i + len(x)
)
)(
s[i:].startswith
)(separators)
Line 1,644 ⟶ 1,651:
# -------------------------
# main :: IO ()
def main():
Line 1,655 ⟶ 1,662:
# ------------------
#
def find(p):
'''Just the first element in the list that matches p,
or
'''
def go(xs):
return next(x for x in xs if p(x)
except
return
return
# maybe :: b -> (a -> b) ->
def maybe(v):
'''Either the default value v, if m is
or the application of f to x
▲ where m is Just(x).
'''
return lambda f: lambda m: v if (
None is m
) else f(m
|