0

当我在尝试将函数应用于值时遇到问题时,我正在做一个关于 CodeWars 的问题并练习一些函数式编程。

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

它可以工作,但我真的不想向对象原型添加方法!

如何在保持函数链接的同时以另一种方式做到这一点?

简单示例

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 上下文问题

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")

代码大战说明

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

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

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

46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51 给定一个正整数 n,写成 abcd...(a, b, c, d... 是数字)和一个正整数 p 我们想求一个正整数k,如果它存在的话,比如n的位数对p的连续幂的和等于k * n。换句话说:

是否有整数k如:(a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k 如果是的话我们将返回 k,如果不返回 -1。

注意:n、p 将始终作为严格的正整数给出。

4

2 回答 2

2

解决方案就是不使用方法链接——您要应用于结果的功能不是它的方法。有多种解决方法:

  • 一个简单的变量:

    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;
    }
    
  • IIFE 调用(这种方法更适用于静态函数):

    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));
    }
    
  • 实验性绑定运算符(也可以更好地使用静态“方法”):

    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;
        }();
    }
    

您也可以将任何方法与您的pass“方法”一起使用。

于 2017-04-10T21:18:11.463 回答
2

您还可以使用 Identity 函子。这为您提供了一个可链接的界面,但不涉及原生原型

const Identity = x => ({
  runIdentity: x,
  map: f => Identity(f(x))
})

const digPow = (n, p) =>
  Identity(n)
    .map(n => n.toString())
    .map(str => str.split(''))
    .map(nums => nums.reduce((total, num, i) =>
      total + Math.pow(parseInt(num, 10), p + i), 0))
    .map(total => total % n === 0 ? Math.floor(total / n) : -1)
    .runIdentity

console.log(digPow(89, 1))  // 1
console.log(digPow(695, 2)) // 2


如果你定义了一些可重用的函数,它会稍微清理一下代码

const Identity = x => ({
  runIdentity: x,
  map: f => Identity(f(x))
})

const split = x => str => str.split(x)
const reduce = f => y => xs => xs.reduce(f, y)

const digPow = (n, p) =>
  Identity(n)
    .map(String)
    .map(split(''))
    .map(reduce((acc, x, i) => acc + Math.pow(Number(x), p + i)) (0))
    .map(x => x % n === 0 ? Math.floor(x / n) : -1)
    .runIdentity

console.log(digPow(89, 1))  // 1
console.log(digPow(695, 2)) // 2

于 2017-04-10T22:15:30.980 回答