Skip to content

Instantly share code, notes, and snippets.

@sigsergv
Last active June 16, 2022 15:55
Show Gist options
  • Save sigsergv/eb6c4e351f3e881f8247013d7dca1251 to your computer and use it in GitHub Desktop.
Save sigsergv/eb6c4e351f3e881f8247013d7dca1251 to your computer and use it in GitHub Desktop.
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