JavaScript函数式编程 - 链接函数和使用匿名函数 [英] JavaScript Functional Programming - Chaining Functions and using an anonymous function

查看:149
本文介绍了JavaScript函数式编程 - 链接函数和使用匿名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



所以我做了一个 em> pass()函数接受函数作为参数,以便我可以使用匿名函数来操作该值并返回它。因此,在这种情况下,它从reduce中获取值并将其传递给函数,以便它可以操纵该值然后返回它。



WORKS 但是我真的不想在Object原型中添加一个方法!

我怎么能以另一种方式做这件事,同时仍然保持功能的链接?

简单示例



  Object.prototype.pass = function(fn){
return fn(this);
};

var value = 1;

var new_value = value.pass(function(num){
return num + 1;
});

console.log(value,new_value); //输出:1 2



CodeWars上下文的问题



Object.prototype.pass = function(fn){return fn(this)}; function digPow(n,p){返回n .toString().split('').reduce(函数(total,num,i){return total + Math.pow(parseInt(num),(p + i))},0).pass(function (总数){return(total%n == 0)?Math.floor(total / n):-1;});} // digPow(89,1)应该返回1,因为8¹+9²= 89 = 89 * 1console.log(Test Case 1 returns(,digPow(89,1),)should return 1)// digPow(92,1)应该返回-1,因为没有k,例如9¹+2²等于92 * kconsole.log(测试用例2返回(,digPow(92,1),)应该返回-1)// digPow(695,2)应该返回2,因为6²+9³+ 5 4 = 1390 = 695 * 2console.log(测试用例3返回(,digPo (测试用例4返回(w(695,2),)应该返回2)// digPow(46288,3)应该返回51,因为4 3 + 6 4 + 2 5 + 8 8 + = 2360688 = 46288 * 51console.log ,digPow(46288,3),)应该返回51)

b
$ b

Code Wars Instructions




有些数字具有有趣的属性。例如:

89 - >8¹+9²= 89 * 1

695 - >6²+ 9 3 + 5 4 = 1390 = 695 * 2

46288 - > 4 3 + 6 4 + 2 5 5 + 8 8 + 8 8 = 2360688 = 46288 * 51给定
positive整数n写成abcd ...(a,b,c,d ...是数字)和
a正整数p我们希望找到一个正整数k,如果
存在,比如对于p的连续
次幂取n的数位之和等于k * n。换句话说:

是否有整数k,例如:(a ^ p + b ^(p + 1)+ c ^(p + 2)+ d ^
(p + 3)+ ...)= n * k如果是这种情况,我们将返回k,如果没有返回
-1。



注意:n,p将始终以严格正整数形式给出。


解决方案

解决方案只是不使用方法链 - 你想应用于结果的功能不是它的一种方法。有各种各样的方法:


  • 一个简单的变量:

    函数digPow(n,p){
    const total = n
    .toString()
    .split('')
    .reduce(函数(total,num,i){
    return total + Math.pow(parseInt(num),(p + i))
    },0);
    返回(总共%n == 0)? Math.floor(total / n):-1;
    }


  • IIFE调用(此方法对静态函数效果更好):

     函数digPow(n,p){
    return(函数(总计){
    return(total %n == 0)?Math.floor(total / n):-1;
    }(n
    .toString()
    .split('')
    .reduce (函数(total,num,i){
    return total + Math.pow(parseInt(num),(p + i))
    },0));
    }


  • 实验绑定运算符(使用静态方法更好):

      function digPow(n,p){
    return n
    .toString()
    .split('')
    .reduce(function(total,num,i){
    return total + Math.pow(parseInt(num),(p + i))
    },0)
    :: function(){
    return(this%n == 0)?Math.floor(this / n):-1;
    }();
    }




您也可以在传递方法中使用任何方法。


I was doing a question on CodeWars and practicing some functional programming when I encountered a problem while trying to apply a function to a value.

So I made a pass() function that accepts a function as an argument so that I could use an anonymous function to manipulate that value and then return it. So, in this case, it takes the value from reduce and passes it to a function so it can manipulate that value then return it.

It WORKS but I really don't want to add a method to the Object prototype!

How can I do this another way while still maintaining the chaining of functions?

Simple Example

Object.prototype.pass = function(fn) {
  return fn(this);
};

var value = 1;

var new_value = value.pass(function(num){
   return num + 1;
});

console.log(value, new_value); // Outputs: 1 2

CodeWars Problem for context

Object.prototype.pass = function(fn) {
  return fn(this)
};

function digPow(n, p) {
  return n
    .toString()
    .split('')
    .reduce(function(total, num, i) {
      return total + Math.pow(parseInt(num), (p + i))
    }, 0)
    .pass(function(total) {
      return (total % n == 0) ? Math.floor(total / n) : -1;
    });
}

//digPow(89, 1) should return 1 since 8¹ + 9² = 89 = 89 * 1
console.log("Test Case 1 returns (", digPow(89, 1), ") should return 1")

//digPow(92, 1) should return -1 since there is no k such as 9¹ + 2² equals 92 * k
console.log("Test Case 2 returns (", digPow(92, 1), ") should return -1")

//digPow(695, 2) should return 2 since 6² + 9³ + 5⁴= 1390 = 695 * 2
console.log("Test Case 3 returns (", digPow(695, 2), ") should return 2")

//digPow(46288, 3) should return 51 since 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
console.log("Test Case 4 returns (", digPow(46288, 3), ") should return 51")

Code Wars Instructions

Some numbers have funny properties. For example:

89 --> 8¹ + 9² = 89 * 1

695 --> 6² + 9³ + 5⁴= 1390 = 695 * 2

46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51 Given a positive integer n written as abcd... (a, b, c, d... being digits) and a positive integer p we want to find a positive integer k, if it exists, such as the sum of the digits of n taken to the successive powers of p is equal to k * n. In other words:

Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k If it is the case we will return k, if not return -1.

Note: n, p will always be given as strictly positive integers.

解决方案

The solution is just to not use method chaining - the functionality you want to apply to the result is not a method of it. There are various ways around:

  • A simple variable:

    function digPow(n, p) {
      const total = n
        .toString()
        .split('')
        .reduce(function(total, num, i) {
          return total + Math.pow(parseInt(num), (p + i))
        }, 0);
      return (total % n == 0) ? Math.floor(total / n) : -1;
    }
    

  • An IIFE call (this approach works better with static functions):

    function digPow(n, p) {
      return (function(total) {
        return (total % n == 0) ? Math.floor(total / n) : -1;
      }(n
        .toString()
        .split('')
        .reduce(function(total, num, i) {
          return total + Math.pow(parseInt(num), (p + i))
        }, 0));
    }
    

  • The experimental bind operator (works better with static "methods" as well):

    function digPow(n, p) {
      return n
        .toString()
        .split('')
        .reduce(function(total, num, i) {
          return total + Math.pow(parseInt(num), (p + i))
        }, 0)
        :: function() {
          return (this % n == 0) ? Math.floor(this / n) : -1;
        }();
    }
    

You also could use any of the approaches with your pass "method".

这篇关于JavaScript函数式编程 - 链接函数和使用匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆