Skip to content

[list-ops]: change // to / and swap foldl/foldr arg order to both call func(el, acc) #3403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions exercises/practice/list-ops/.meta/additional_tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"input": {
"list": ["e", "x", "e", "r", "c", "i", "s", "m"],
"initial": "!",
"function": "(x, y) -> x + y"
"function": "(acc, el) -> el + acc"
},
"expected": "exercism!"
},
Expand All @@ -19,4 +19,4 @@
"expected": [1, "cat", 4.0, "xyz"]
}
]
}
}
2 changes: 1 addition & 1 deletion exercises/practice/list-ops/.meta/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def foldl(function, list, initial):
if len(list) == 0:
return initial
else:
return foldl(function, list[1:], function(initial, list[0]))
return foldl(function, list[1:], function(list[0], initial))


def foldr(function, list, initial):
Expand Down
9 changes: 7 additions & 2 deletions exercises/practice/list-ops/.meta/template.j2
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{%- import "generator_macros.j2" as macros with context -%}
{% macro swap_args(function) -%}
{% set function = function.replace("(acc, el)", "(el, acc)") %}
{% set function = function.replace("(x, y)", "(y, x)") %}
{{function}}
{%- endmacro %}

{% macro lambdify(function) -%}
{% set function = function.replace("(", "", 1).replace(")", "", 1).replace(" ->", ":") %}
{% set function = function.replace("modulo", "%") %}
{% set function = function.replace("/", "//") %}
lambda {{function}}
{%- endmacro %}

Expand Down Expand Up @@ -31,7 +36,7 @@
{%- elif case["property"] == "length" or case["property"] == "reverse" -%}
{{ input["list"] }}
{%- elif case["property"] == "foldl" or case["property"] == "foldr" -%}
{{ lambdify(input["function"]) }}, {{ input["list"] }}, {{ stringify(input["initial"]) }}
{{ lambdify(swap_args(input["function"])) }}, {{ input["list"] }}, {{ stringify(input["initial"]) }}
{%- endif -%}
),
{{ stringify(case["expected"]) }}
Expand Down
2 changes: 0 additions & 2 deletions exercises/practice/list-ops/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ reimplements = "e56df3eb-9405-416a-b13a-aabb4c3b5194"
[d7fcad99-e88e-40e1-a539-4c519681f390]
description = "folds (reduces) the given list from the left with a function -> direction dependent function applied to non-empty list"
reimplements = "d2cf5644-aee1-4dfc-9b88-06896676fe27"
include = false

[aeb576b9-118e-4a57-a451-db49fac20fdc]
description = "folds (reduces) the given list from the right with a function -> empty list"
Expand All @@ -96,7 +95,6 @@ reimplements = "c4b64e58-313e-4c47-9c68-7764964efb8e"
[8066003b-f2ff-437e-9103-66e6df474844]
description = "folds (reduces) the given list from the right with a function -> direction dependent function applied to non-empty list"
reimplements = "be396a53-c074-4db3-8dd6-f7ed003cce7c"
include = false

[94231515-050e-4841-943d-d4488ab4ee30]
description = "reverse the elements of the list -> empty list"
Expand Down
18 changes: 13 additions & 5 deletions exercises/practice/list-ops/list_ops_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,22 @@ def test_map_non_empty_list(self):
self.assertEqual(list_ops_map(lambda x: x + 1, [1, 3, 5, 7]), [2, 4, 6, 8])

def test_foldl_empty_list(self):
self.assertEqual(foldl(lambda acc, el: el * acc, [], 2), 2)
self.assertEqual(foldl(lambda el, acc: el * acc, [], 2), 2)

def test_foldl_direction_independent_function_applied_to_non_empty_list(self):
self.assertEqual(foldl(lambda acc, el: el + acc, [1, 2, 3, 4], 5), 15)
self.assertEqual(foldl(lambda el, acc: el + acc, [1, 2, 3, 4], 5), 15)

def test_foldl_direction_dependent_function_applied_to_non_empty_list(self):
self.assertEqual(foldl(lambda el, acc: el / acc, [1, 2, 3, 4], 24), 64)

def test_foldr_empty_list(self):
self.assertEqual(foldr(lambda acc, el: el * acc, [], 2), 2)
self.assertEqual(foldr(lambda el, acc: el * acc, [], 2), 2)

def test_foldr_direction_independent_function_applied_to_non_empty_list(self):
self.assertEqual(foldr(lambda acc, el: el + acc, [1, 2, 3, 4], 5), 15)
self.assertEqual(foldr(lambda el, acc: el + acc, [1, 2, 3, 4], 5), 15)

def test_foldr_direction_dependent_function_applied_to_non_empty_list(self):
self.assertEqual(foldr(lambda el, acc: el / acc, [1, 2, 3, 4], 24), 9)

def test_reverse_empty_list(self):
self.assertEqual(reverse([]), [])
Expand All @@ -84,7 +90,9 @@ def test_reverse_list_of_lists_is_not_flattened(self):

def test_foldr_foldr_add_string(self):
self.assertEqual(
foldr(lambda x, y: x + y, ["e", "x", "e", "r", "c", "i", "s", "m"], "!"),
foldr(
lambda el, acc: el + acc, ["e", "x", "e", "r", "c", "i", "s", "m"], "!"
),
"exercism!",
)

Expand Down