Absolutely stuck at higher-order functions

Alright then a video game example . You found a 10lb hunk of raw gold. You’re thrilled but you quickly realize you can’t reasonably buy anything from any of the shops in town.

You think I should turn this into gold coins. The only issue is to make gold coins you need bars of gold not hunks.

craftCoins = (goldbars) =>  10 x goldbars

So first you need to smelt your gold hunk into gold bars

smeltGold = (rawGold) => rawGold.weight 

So first you smelt your gold and you get 10 bars of gold and then you use that to craft 100 gold coins.

Goldbars = smeltGold(hunk)
GoldCoins = craftCoins(Goldbars)

So, since smeltGold(hunk) returns goldbars and you want to then use gold bars to make coins you can compose the functions together for the same effect.

craftCoins(smeltGold(hunk))

The code is all nonsense but that’s the idea.

/r/learnjavascript Thread Parent