In some functional programming languages there is this one function called ap, which is short for apply, is basically .map's long lost brother.
You see with .map you apply a callback to a value inside a data structure, and it all works great. But what if your callback is also trapped inside a data structure? that's where .ap comes in, it will figure out all the details to get the values out of the data structures and apply them.
An interesting example where this could be useful is if you try to make your own improvised "validation framework". Using plain objects as our data structures, we could implement map and ap for them.
That will return an object of the same shape but with the result data of each validation. Since these are all plain functions you can do all sorts of crazy stuff inside them, you can have multiple validations inside one function, you can have it return a lot metadata... well anything you can think of.
There is a whole bunch where that came from. In the javascript world we have a specification for them, is called Fantasy Land. It's geared towards library authors but it could still be useful if you know how to read those weird signatures. Someone actually took the time to go through the spec and made a series posts about it.
Oh, and if you want to check out a cool library with utility functions, go see ramda.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
In some functional programming languages there is this one function called
ap
, which is short for apply, is basically.map
's long lost brother.You see with
.map
you apply a callback to a value inside a data structure, and it all works great. But what if your callback is also trapped inside a data structure? that's where.ap
comes in, it will figure out all the details to get the values out of the data structures and apply them.An interesting example where this could be useful is if you try to make your own improvised "validation framework". Using plain objects as our data structures, we could implement
map
andap
for them.Now imagine we have this input.
If you want to validate that all you have to do is wrap your validation functions inside an object of the same shape.
With that in place you just apply them.
That will return an object of the same shape but with the result data of each validation. Since these are all plain functions you can do all sorts of crazy stuff inside them, you can have multiple validations inside one function, you can have it return a lot metadata... well anything you can think of.
Nice, didn't knew this one, thank you for your answer
There is a whole bunch where that came from. In the javascript world we have a specification for them, is called Fantasy Land. It's geared towards library authors but it could still be useful if you know how to read those weird signatures. Someone actually took the time to go through the spec and made a series posts about it.
Oh, and if you want to check out a cool library with utility functions, go see ramda.