Skip to content

Commit

Permalink
Implement map
Browse files Browse the repository at this point in the history
  • Loading branch information
dantp-ai committed Mar 3, 2024
1 parent 852fc98 commit c3ba313
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 5 additions & 2 deletions minitorch/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,11 @@ def map(fn: Callable[[float], float]) -> Callable[[Iterable[float]], Iterable[fl
A function that takes a list, applies `fn` to each element, and returns a
new list
"""
# TODO: Implement for Task 0.3.
raise NotImplementedError("Need to implement for Task 0.3")

def new_fn(ls: Iterable[float]) -> Iterable[float]:
return [fn(x) for x in ls]

return new_fn


def negList(ls: Iterable[float]) -> Iterable[float]:
Expand Down
19 changes: 18 additions & 1 deletion tests/test_operators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable, List, Tuple
from typing import Callable, List, Tuple, Iterable

import pytest
from hypothesis import given
Expand All @@ -15,6 +15,7 @@
log_back,
lt,
max,
map,
mul,
neg,
negList,
Expand Down Expand Up @@ -162,6 +163,22 @@ def test_other(a: float, b: float, c: float) -> None:
# properties.


@pytest.mark.task0_3
@given(lists(small_floats, min_size=0, max_size=100), small_floats)
def test_map(ls: Iterable[float], a: float) -> None:
def fn(x: float) -> float:
return x + a

def fn2(x: float) -> float:
return x * a

expected1 = [fn(x) for x in ls]
expected2 = [fn2(x) for x in ls]

assert expected1 == map(fn)(ls)
assert expected2 == map(fn2)(ls)


@pytest.mark.task0_3
@given(small_floats, small_floats, small_floats, small_floats)
def test_zip_with(a: float, b: float, c: float, d: float) -> None:
Expand Down

0 comments on commit c3ba313

Please sign in to comment.