结合这些特定的javascript函数 [英] Combine these specific javascript functions

查看:74
本文介绍了结合这些特定的javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这两个单独的 javascript 函数,它们使用数字或空格执行相同的任务.我对函数不是很熟悉,所以我的问题是如何将以下两个合二为一?

function padnum(string, length){而 (string.length < 长度) {字符串 += Math.floor(Math.random() * 10);}返回字符串;}函数 ws(字符串,长度){而 (string.length < 长度) {字符串 += " ";}返回字符串;}

解决方案

考虑您要组合的两个功能有何不同.然后分解"这些差异以创建通用函数.在您的情况下,唯一的区别在于要使用的填充规则 - 空格或随机数字.

理论上,您可以添加某种布尔标志或类型"参数,然后在您的函数中执行 ifswitch 以选择所需的行为.这是一个可怕的设计.随着您添加更多选项,您的功能将不断增长.这是一个答案所采用的方法,不幸的是,这是目前公认的方法.

相反,由于在这种情况下,差异在于行为,因此将这些不同的行为表示为要回调的函数.这就是函数的用途——代表行为!然后,您的函数将具有签名 pad(string, length, padFunc).这是一种更好的方法,也是其他一些答案所采用的方法.然而,恕我直言,这些答案中至少有一个在将 padFunc 的含义重载为函数或字符串的方式上设计得有点糟糕.

一种现代的函数式方法是编写一个高阶函数,该函数根据下面称为 fn 的规则创建填充函数:

function padder(fn) {返回函数(字符串,长度){while (string.length < length) string += fn(string, length);};}

(我将 stringlength 传递给填充函数,以防万一它想将它们用于任何事情.或者它可以忽略它们.)

现在制作一个填充空白的填充器:

const blankPadder = padder(() => ' ');

或者制作一个填充随机数的填充器:

const randomPadder = padder(() => Math.floor(Math.random() * 10));

顽固的函数式程序员可能会将这些写成:

const K = k =>() =>k;const random = n =>() =>Math.floor(Math.random() * n);const blankPadder = padder(K(' '));const randomPadder = padder(random(10));

这里的 KK 组合子的传统名称, 返回一个总是返回相同值的函数的函数.

I have these two separate javascript functions that perform the same task using either numbers or white space. I'm not all that familiar with functions so my question is how would one combine the following two into one?

function padnum(string, length)
{
    while (string.length < length) {
        string += Math.floor(Math.random() * 10);
    }
    return string;
}

function ws(string, length)
{    
    while (string.length < length) {
       string += " ";
    }
    return string;
}

解决方案

Consider how the two functions you want to combine differ. Then "factor out" those differences to create a generic function. In your case the single difference is in the padding rule to use--either a space, or a random digit.

In theory you could add some kind of boolean flag or "type" parameter, and then do an if or switch inside your function to choose the desired behavior. That is a horrible design. Your function will grow and grow as you add more options. This is the approach taken by one answer, which unfortunately is the currently accepted one.

Instead, since in this case the differences are in behavior, represent those different behaviors as functions to be called back. That's what functions are for--to represent behaviors! Your function would then have the signature pad(string, length, padFunc). This is a better approach, and is the one taken by some other answers. However, IMHO at least one of those answers is a bit poorly designed in the way it overloads the meaning of padFunc to be either a function or a string.

A modern, functional approach would be to write a higher-order function which creates a padding function based on a rule, called fn in the below:

function padder(fn) {
  return function(string, length) {
    while (string.length < length) string += fn(string, length);
  };
}

(I'm passing string and length to the padding function, just in case it wants to use them for anything. Or it could ignore them.)

Now to make a padder which pads with blanks:

const blankPadder = padder(() => ' ');

Or to make a padder which pads with random numbers:

const randomPadder = padder(() => Math.floor(Math.random() * 10));

A die-hard functional programmer might write these as:

const K      = k => () => k;
const random = n => () => Math.floor(Math.random() * n);

const blankPadder  = padder(K(' '));
const randomPadder = padder(random(10));

Here K is the traditional name for the K combinator, the function which returns a function which always returns the same value.

这篇关于结合这些特定的javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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