DEV Community

Cover image for What Can Array Folding Do?

What Can Array Folding Do?

Neil Syiemlieh on April 20, 2019

This is Part 2 of the "Folds" series, where we look at how we could use the simple Fold pattern to perform a variety of array processing tasks. ...
Collapse
 
johnboy5358 profile image
John •

Thanks Neil for your interesting article.

Sounds as if you may be interested in Ian Hofmann-Hicks youtube channel (search evilsoft on youtube) and his Crocks A.D.T library github.com/evilsoft/crocks

But, of course, you may have encountered his offerings before.

Collapse
 
mebble profile image
Neil Syiemlieh •

Thanks! I had never heard of him but his content is exactly the kind of stuff I'm interested in. Reminds me of the Fantasy land spec I recently found

Collapse
 
johnboy5358 profile image
John •

Enjoy!
I will look forward to further articles from you soon, I hope.

Collapse
 
joshuakb2 profile image
Joshua Baker •

What's the benefit of doing this instead of just using the built in Array.prototype.reduce?

Collapse
 
mebble profile image
Neil Syiemlieh •

There is not benefit other than exploring it for fun!

Collapse
 
diek profile image
diek •

I don't understand the pipe one, can you explain it better, or at least say examples of usage?

Ty.

Collapse
 
mebble profile image
Neil Syiemlieh • • Edited

The pipe takes in an array of functions and "connects" them so that the output of a function within that array is passed as an input to the function after it.

// 
const squareThenIncrement = pipe([x => x * x, x => x + 1])
squareThenIncrement(4);  // 17
squareThenIncrement(9);  // 82

There are a few medium articles out there explaining it in more detail. I didn't want to make this post too detailed. I just wanted to show how much we could do using just that fold function