Skip to content

Instantly share code, notes, and snippets.

@sigsergv
Last active Jun 16, 2022
Embed
What would you like to do?
python fold tasks
def flen(lst):
return reduce(lambda acc, x: acc + 1, lst, 0)
def flpos(lst, item):
def f(acc, x):
found, pos = acc
if found != -1:
return acc
if x == item:
return pos, pos
else:
return -1, pos + 1
res, _ = reduce(f, lst, (-1, 0))
return res
def frpos(lst, item):
def f(acc, x):
found, pos = acc
if x == item:
return pos, pos + 1
else:
return found, pos + 1
res, _ = reduce(f, lst, (-1, 0))
return res
def fmap(lst, fun):
def f(acc, x):
acc.append(fun(x))
return acc
return reduce(f, lst, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment