Last active
June 16, 2022 15:55
-
-
Save sigsergv/eb6c4e351f3e881f8247013d7dca1251 to your computer and use it in GitHub Desktop.
python fold tasks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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