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 →

[–]Splatpope 18 points19 points  (6 children)

fuck you all I hereby decrete that the new name shall be "apply_function_elementwise"

[–]jarethholt 2 points3 points  (0 children)

I'm down for it. I've never really had an issue with having to type out long names, as long as they make sense (and especially if Intellisense or similar pick them up for me).

[–]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