使用 Ramda 在循环中从多个数组中取出前 X 个总项目

cpo*_*ver 3 javascript arrays functional-programming ramda.js

我有一个数组数组,想要编写一个函数x,通过按顺序从每个数组中获取项目来返回最高数量的项目。

这是我所追求的一个例子:

    const input = [
      ["1a", "2a", "3a", "4a", "5a"],
      ["1b", "2b", "3b", "4b", "5b"],
      ["1c", "2c", "3c", "4c", "5c"],
      ["1d", "2d", "3d", "4d", "5d"]
    ];

    const takeRoundRobin = count => arr => {
      // implementation here
    };

    const actual = takeRoundRobin(5)(input);

    const expected = [
      "1a", "1b", "1c", "1d", "2a"
    ];
Run Code Online (Sandbox Code Playgroud)

我看到了一个 Scala 问题的建议,它解决了这个问题zip,但在 Ramda 中你只能传递 2 个列表来压缩。

Sco*_*yet 6

在这里,Ramda'stranspose可以成为您的基地。添加一团unnest、一点take,你会得到这个:

const {take, unnest, transpose} = R

const takeRoundRobin = (n) => (vals) => take(n, unnest(transpose(vals)))

const input = [
  ['1a', '2a', '3a', '4a', '5a'],
  ['1b', '2b', '3b', '4b', '5b'],
  ['1c', '2c', '3c', '4c', '5c'],
  ['1d', '2d', '3d', '4d', '5d']
]

console.log(takeRoundRobin(5)(input))
Run Code Online (Sandbox Code Playgroud)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
Run Code Online (Sandbox Code Playgroud)

另请注意,这可以处理不同长度的数组:


如果您希望能够回到开头并继续获取值,您可以替换takerecursiveTake如下所示:

const {take, unnest, transpose, concat } = R

//recursive take
const recursiveTake = (n) => (vals) => {
  const recur = (n,vals,result) =>
    (n<=0)
      ? result
      : recur(n-vals.length,vals,result.concat(take(n,vals)))
  return recur(n,vals,[]);
};

const takeRoundRobin = (n) => (vals) => 
  recursiveTake(n)(unnest(transpose(vals)));

const input = [
  ['1a', '2a', '3a', '4a'],
  ['1b'],
  ['1c', '2c', '3c', '4c', '5c'],
  ['1d', '2d']
]

console.log(takeRoundRobin(14)(input))
Run Code Online (Sandbox Code Playgroud)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
Run Code Online (Sandbox Code Playgroud)

该函数的另一个版本(没有显式递归)如下所示:

const takeCyclic = (n) => (vals) => take(
  n,
  unnest(times(always(vals), Math.ceil(n / (vals.length || 1))))
)
Run Code Online (Sandbox Code Playgroud)

  • @HMR:`recursiveTake`的另一个版本,没有显式递归,看起来像`(n) =&gt; (vals) =&gt; take(n, unnest(times(always(vals), Math.ceil(n / (vals) .length || 1))))` ` (2认同)