Recently I'm having fun with map, filter and reduce, but are the more functions like these that could make programming more fun?
If you know about other built-in functions or functions that you have created, write them in the comments.
Recently I'm having fun with map, filter and reduce, but are the more functions like these that could make programming more fun?
If you know about other built-in functions or functions that you have created, write them in the comments.
For further actions, you may consider blocking this person and/or reporting abuse
Nico Prat -
Anwer Solangi -
Rowsan Ali -
M Sea Bass -
Top comments (4)
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.
Try out
pipe
andtap
.pipe
takes an array of functions and lets you chain them together so that the return value of one function becomes the first argument to the next.tap
takes a function and some input, calls the function with input, and returns the original input (good for effects). Both of these functions used together can get you started composing functions pretty fast. Here are some implementations for the functions.Example with
pipe
,tap
,map
, andfilter
I created a library with async-capable versions of the above and more. Check it out at rubico.land