如何存储功能链的数据? [英] How to store data of a functional chain?

查看:146
本文介绍了如何存储功能链的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下简单函数:

const L = a => L;

表格

L
L(1)
L(1)(2)
...

这似乎形成一个列表,但实际数据根本不存储,所以如果需要存储数据,如 [1,2] ,完成任务最聪明的做法是什么?

This seems to form a list but the actual data is not stored at all, so if it's required to store the data such as [1,2], what is the smartest practice to have the task done?

const L = (a) => {
 // do somthing
  return L;
};

我更喜欢这种简洁的箭头功能样式,并且不希望破坏外部结构可能。当然,我理解一些外部结构修改是必需的,但我很好奇什么是可能的,特别是在功能风格而不是OO。

I would prefer this concise arrow function style, and do not want to destroy the outer structure as much as possible. Surely, I understand some outer structure modification is required, but I am curious what is possible especially in functional style not OO.

规范只是存储函数的数据链。

The specification is simply to store data of the function chain.

任何想法?谢谢。

最初的最简单方法是:

const L = (a) => {
  L.val = a;
  return L;
};
L.val = L;

可以做一些,但没有数据累积。

can do some, but no data accumulation.

{ [Function: L] val: [Circular] }
{ [Function: L] val: 1 }
{ [Function: L] val: 2 }

注意:

每个清单应该是独立的积累。

Every list should be independent for the accumulation.

L(3)(4)

将返回 [3,4] 不是 [2,3,3,4] 先前累积了另一个列表。

will return [3,4] not [2,3,3,4] with prior accumulation of another lists.

如何存储Monoidal List功能链的数据?

推荐答案

函数currying和variadic参数并不能真正协同工作。一旦您意识到以下两个表达式不兼容,这是一个明显的限制

Function currying and variadic arguments don't really work together. It's a restriction made obvious once you realize that the following two expressions are incompatible

L (1)     -> [ 1 ]
L (1) (2) -> [ 1, 2 ]

高于 L(1)返回一个列表,但在第二个表达式中,我们希望 L(1)是一个我们可以应用于 2 L(1)可以是列表它可以是生成列表的函数;它不能同时出现。

Above L (1) returns a list, but in the second expression we expect L (1) to be a function that we can apply to 2. L (1) can be a list or it can be a function that produces a list; it cannot be both at the same time.

这就是为什么其他人提出像 .list 之类的东西来获得实际价值。您可以这样做,但知道使用对象属性或依赖突变是不必要的。您可以使用您选择的任何信号

This is why others have proposed things like .list to get the actual value out. You can do that but know that using object properties or relying upon mutation is not necessary. You can use any signal of your choosing

const L = (x, acc = []) =>
  x === undefined
    ? acc
    : y => L (y, [...acc, x])
    
console.log
  ( L ()              // []
  , L (1) ()          // [ 1 ]
  , L (1) (2) ()      // [ 1, 2 ]
  , L (1) (2) (3) ()  // [ 1, 2, 3 ]
  )

我们可以使用辅助辅助函数抽象出可选参数。这种技术类似于你找到的解决方案,但在这里我们避免将值分配给函数属性,而是使用简单变量和非变异动作

We can abstract away the optional argument by using an auxiliary helper function. This technique is similar to the solution you found but here we avoid awkward assignment of values to function properties and instead use simple variables and non-mutating actions

const L = init =>
{ const loop = (acc, x) =>
    x === undefined
      ? acc
      : y => loop ([...acc, x], y)
  return loop ([], init)
}

console.log
  ( L ()              // []
  , L (1) ()          // [ 1 ]
  , L (1) (2) ()      // [ 1, 2 ]
  , L (1) (2) (3) ()  // [ 1, 2, 3 ]
  )

或者看到您的要求有些灵活,可以通过更灵活的编码获得创意

Or seeing as though your requirements are somewhat flexible, get creative with a more flexible encoding

const List = x =>
  k => k (x)
  
const append = x => xs =>
  List ([ ...xs, x ])

const prepend = x => xs =>
  List ([ x, ...xs ])
  
List ([]) (append (1)) (console.log)
// [ 1 ]

List ([ 2, 3 ]) (append (4)) (append (5)) (prepend (1)) (console.log)
// [ 1, 2, 3, 4, 5 ]

将JavaScript的许可语法推到极限是很有趣的,但是使用扩展参数最好定义可变参数函数

It's fun to push JavaScript's permissive syntaxes to their limits, but variadic functions are best defined using spread arguments

const L = (...values) =>
  values

console.log
  ( L ()         // []
  , L (1)        // [ 1 ]
  , L (1, 2)     // [ 1, 2 ]
  , L (1, 2, 3)  // [ 1, 2, 3 ]
  )

一个不太做作的例子展示了一个更好的用例

A less contrived example demonstrates a better use case

const max = (x, ...ys) =>
  ys.length === 0
    ? x
    : max2 (x, max (...ys))
    
const max2 = (x, y) =>
   x > y ? x : y
   
console.log
  ( max (1, 5, 3)     // 5
  , max (5, 2, 9, 7)  // 9
  , max (4)           // 4
  , max ()            // undefined
  )

这篇关于如何存储功能链的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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