This is the third day of my participation in Gwen Challenge

Following the previous paper, this paper mainly introduces the function Coriolization, and the practical application of function Coriolization

What is a function Coriolization?

  • In computer science, Currying is a technique that converts a function that takes multiple arguments into a function that takes a single argument (the first argument of the original function), and returns a new function that takes the remaining arguments and returns a result. Generally speaking, it is to split a function into multiple functions, which are fixed partial parameters and return a function that accepts the remaining parameters, also known as partial calculation function. The purpose is to narrow the scope of application and create a more targeted function.
  • Why is there a function called Coriolization?
    • The significance of Currying is that it can completely change functions into a fixed form that takes an argument and returns a value, making it easier to discuss and optimize.
    • Focus your attention on the function itself rather than being distracted by redundant data parameters.
    • When functions become functional programming languages that can be used as arguments and return values of functions, it is inevitable that functions will be currified.

What are the benefits of curryization of functions?

  • Parameters of reuse
// Use encapsulation of regular checksum as an example
// Normal rege.test (TXT)

// The function is wrapped
function check(reg, txt) {
    return reg.test(txt)
}

check(/\d+/g.'test')       //false
check(/[a-z]+/g.'test')    //true

/ / after Currying
function curryingCheck(reg) {
    return function(txt) {
        return reg.test(txt)
    }
}

var hasNumber = curryingCheck(/\d+/g)
var hasLetter = curryingCheck(/[a-z]+/g)

hasNumber('test1')      // true
hasNumber('testtest')   // false
hasLetter('21212')      // false
Copy the code
  • Confirmation in advance
var on = function(element, event, handler) {
    if (document.addEventListener) {
        if (element && event && handler) {
            element.addEventListener(event, handler, false); }}else {
        if (element && event && handler) {
            element.attachEvent('on'+ event, handler); }}}var on = (function() {
    if (document.addEventListener) {
        return function(element, event, handler) {
            if (element && event && handler) {
                element.addEventListener(event, handler, false); }}; }else {
        return function(element, event, handler) {
            if (element && event && handler) {
                element.attachEvent('on'+ event, handler); }}; }}) ();// The isSupport parameter is set first
var on = function(isSupport, element, event, handler) {
    isSupport = isSupport || document.addEventListener;
    if (isSupport) {
        return element.addEventListener(event, handler, false);
    } else {
        return element.attachEvent('on'+ event, handler); }} we are in the process of project to do, some wrapped dom manipulation can be common, however, the first written above is common, but we look at the second kind of writing, it is relatively a first written since the execution and returns a new function, it is confirmed which one way can go ahead and avoid judging every time.Copy the code
  • Delay to run
    • The classic is the bind function
Function.prototype.bind = function (context) {
    var _this = this
    var args = Array.prototype.slice.call(arguments.1)
 
    return function() {
        return _this.apply(context, args)
    }
}
Copy the code
  • Improved applicability
// Common functions solve compatibility problems, but at the same time, we may need to use the same rule over and over again in different scenarios
// This can cause code repetitions such as

function square(i){ return i*i }/ / square
function dubble(i){ return i*2 }/ / double
function map(handler,list){
	// Handle is the rules for operations. List is the arrguments for operations
	return list.map(handler)
}

map(square,[1.2.3]);// Square each item of the array
map(dubble,[1.2.3]);// Double each item in the array
// This is generality. I can do many different operations with the same function
// But if we need to do a lot of squaring and then passing in the array every time we pass in the method, it's a waste of code
// At this point we use Currification to improve practicality

let mapSQ = currying(map,square);// A new trivial operation function defined directly
mapSQ([1.2.3]);// We don't need to pass in the operation method
Copy the code

A general encapsulation method for currization of functions

// Support multiple parameter passing
function fnCurrying(fn, args) {
    var _this = this
    var len = fn.length;
    var args = args || [];
    return function() {
        var _args = Array.prototype.slice.call(arguments);
        Array.prototype.push.apply(args, _args);
        // If the number of arguments is less than the original fn.length, the recursive call continues to collect arguments
        if (_args.length < len) {
            return fnCurrying.call(_this, fn, _args);
        }
        // After parameters are collected, run fn
        return fn.apply(this, _args); }}Copy the code

The performance problem of currization of functions

  • Accessing arguments objects is generally a bit slower than accessing named arguments
  • Some older browsers are quite slow to implement arguments.length
  • Use fn.apply(…) And fn. Call (…). It’s usually better than calling fn(…) directly. A little bit slowly
  • Creating lots of nested scopes and closures can be costly, both in memory and speed