getify / Functional-Light-JS

Pragmatic, balanced FP in JavaScript. @FLJSBook on twitter.
http://FLJSBook.com
Other
16.6k stars 1.96k forks source link

I just couldn't find out the reason of the encapsulating. #166

Closed somarlyonks closed 5 years ago

somarlyonks commented 6 years ago

In ch2

function formatter(formatFn) {
    return function inner(str){
        return formatFn( str );
    };
}

var lower = formatter( function formatting(v){
    return v.toLowerCase();
} );

lower( "WOW" );             // wow

How could this be better than:

var lower = function formatting(v){
    return v.toLowerCase();
}

What does the formatter f => v => f(v) mean here ? I concluded this from

mostly-adequate-guide/ch02.md#a-quick-review

In other words, hi is already a function that expects one argument, why place another function around it that simply calls hi with the same bloody argument? It doesn't make any damn sense. It's like donning your heaviest parka in the dead of July just to blast the air and demand an ice lolly.

I think this might not be a good example for encapsulating a behavior, or did I misunderstand anything?