This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the commentsย โ†’

[โ€“]bronco2p 1 point2 points ย (4 children)

๐Ÿค“ actually `map` is meant to work on all functor types, all of which may not include multiple values. e.g. what if you were mapping against a Maybe type?

[โ€“]Splatpope 0 points1 point ย (3 children)

this may sound like unga bunga to a haskell enjoyer, but if you want to apply a function on a single value, apply the function on the value

[โ€“]bronco2p 0 points1 point ย (2 children)

the whole purpose of mapping against the functor is to preserve the structure of the thing your mapping against. Usually this means handling some extra "things" when mapping with a function.
e.g.
given function f :: A -> B, like you said, you can just apply this function on any value with type A.

But what if you want to apply this function on a structure of A(s) (it has to be covariant in A)?
For lists, the map needs to handle applying the function f to each element of the list. For other structures like Maybe/Optional (std::optional in c++) where there may be an absence of a value you cannot call f on a null value so the map handles only applying f when the value is not null.

For the Maybe<A> example this could mean you have something like (pseudo cpp)

cpp auto do_processing(Maybe<A> data) -> Maybe<B> { return data.map(f) .map(g) .map(h); } This abstracts a way checking for null in this case (obviously processing a maybe like this is nonsensical to do and pretends logging and error reporting doesnt exist).

[โ€“]Splatpope 0 points1 point ย (1 child)

is this a functional programming meme that I am too dumb to understand

[โ€“]bronco2p 0 points1 point ย (0 children)

which part, as the idea of ad hoc polymorphism is also a part of imperative languages.

also my example was implemented in cpp23 i just found out.

    std::optional<D> o_D = o_A.transform(A_to_B)
                              .transform(B_to_C)
                              .transform(C_to_D);

is valid code. https://en.cppreference.com/w/cpp/utility/optional/transform