Update u2/list_ops.py
This commit is contained in:
parent
4133dc0af9
commit
a62a61ecc8
@ -6,45 +6,52 @@ def append(list1, list2):
|
||||
result += [item]
|
||||
return result
|
||||
|
||||
|
||||
def concat(lists):
|
||||
result = []
|
||||
for sublist in lists:
|
||||
result = append(result, sublist)
|
||||
return result
|
||||
|
||||
|
||||
def filter(function, lst):
|
||||
result = []
|
||||
for item in list:
|
||||
for item in lst:
|
||||
if function(item):
|
||||
result += [item]
|
||||
return result
|
||||
|
||||
|
||||
def length(lst):
|
||||
result = 0
|
||||
for item in lst:
|
||||
for _ in lst:
|
||||
result += 1
|
||||
return result
|
||||
|
||||
|
||||
def map(function, lst):
|
||||
result = []
|
||||
for item in lst:
|
||||
result += [function(item)]
|
||||
return result
|
||||
|
||||
|
||||
def foldl(function, lst, initial):
|
||||
acc = initial
|
||||
for item in lst:
|
||||
acc = function(acc, item)
|
||||
return acc
|
||||
|
||||
def foldr(function, lst, initial):
|
||||
acc = initial
|
||||
for acc in lst:
|
||||
acc = function(acc, item)
|
||||
return acc
|
||||
|
||||
def reverse(lst):
|
||||
result = []
|
||||
for item in reverse(lst):
|
||||
for item in lst:
|
||||
result = [item] + result
|
||||
return result
|
||||
|
||||
|
||||
def foldr(function, lst, initial):
|
||||
acc = initial
|
||||
for item in reverse(lst):
|
||||
acc = function(acc, item)
|
||||
return acc
|
||||
Loading…
Reference in New Issue
Block a user