函数式java:P1是什么东西?

发布于 2024-11-03 06:52:48 字数 187 浏览 4 评论 0原文

我正在查看 Function Java,但不明白 是什么P1 是。有人可以解释和/或举个例子吗?

(背景:我确实知道柯里化和闭包是什么)

I'm looking at Functional Java and I don't understand what a P1 is. Could anyone explain and/or give an example?

(background: I do know what currying and closures are)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

依 靠 2024-11-10 06:52:48

这直接取自 Functional Java 的 Google Code 项目:

联合联合类型(元组)是其他类型的产品。提供了元数1-8的乘积(fj.P1 - fj.P8)。当您想要从函数返回多个值时,或者当您想要在实现只接受一个参数的接口方法时接受多个值时,这些非常有用。它们还可以用于通过其他数据类型获取产品,例如列表(zip 函数)。

// Regular Java
public Integer albuquerqueToLA(Map<String, Map<String, Integer>> map) {
  Map m = map.get("Albuquerque");
  if (m != null)
     return m.get("Los Angeles"); // May return null.
}

// Functional Java with product and option types.
public Option<Integer> albuquerqueToLA(TreeMap<P2<String, String>, Integer>() map) {
  return m.get(p("Albuquerque", "Los Angeles"));
}

This is taken straight from the Google Code project for Functional Java:

Joint union types (tuples) are products of other types. Products of arities 1-8 are provided (fj.P1 - fj.P8). These are useful for when you want to return more than one value from a function, or when you want to accept several values when implementing an interface method that accepts only one argument. They can also be used to get products over other datatypes, such as lists (zip function).

// Regular Java
public Integer albuquerqueToLA(Map<String, Map<String, Integer>> map) {
  Map m = map.get("Albuquerque");
  if (m != null)
     return m.get("Los Angeles"); // May return null.
}

// Functional Java with product and option types.
public Option<Integer> albuquerqueToLA(TreeMap<P2<String, String>, Integer>() map) {
  return m.get(p("Albuquerque", "Los Angeles"));
}
没有你我更好 2024-11-10 06:52:48

P1 看起来像 1 元素、琐碎的产品类型 。在 Haskell 中,它会写成:

data P1 a = P1 a

(Haskell 中的 Identity 类型)。

也就是说,它是一个容纳其他类型a的容器。

这种类型还实现了最简单的 monad,Identity,它允许将函数不透明地应用于框的内容。

从计算角度来看,没有理由使用 Identity monad,而不是简单地将函数应用于其参数的简单行为,但是,它在 monad 转换器堆栈的设计中可能很有用。

身份 monad 的 monad 实现很简单,

return a     = P1 a
(P1 m) >>= k = k m

如您所见,这只是函数应用。

P1 looks like the 1-element, trivial product type. In Haskell it would be written as:

data P1 a = P1 a

(the Identity type in Haskell).

that is, it is a container that holds some other type a.

This type also implements the simplest monad, Identity, which allows for functions to be opaquely applied to the contents of the box.

Computationally, there is no reason to use the Identity monad instead of the much simpler act of simply applying functions to their arguments, however, it can be useful in the design of monad transformer stacks.

The monad implementation of the identity monad is trivial,

return a     = P1 a
(P1 m) >>= k = k m

As you can see, this is just function application.

同展鸳鸯锦 2024-11-10 06:52:48

啊哈哈,找到了这篇文章

<前><代码>>>>而且,P1 可能是懒惰的。我们用它来实现
>>>>>例如,流。

因此,我可以使用返回 P1 的内容,而不是直接返回类型 T,就像 Google Collections Supplier,并仅在 P1 时计算包含的值._1() 被调用。

(呵呵,这篇博文 Java 中的延迟错误处理也很有趣……)

aha, found this post:

>>> Also, P1 is potentially lazy. We use it for the implementation of
>>> Stream, for example. 

So instead of returning type T directly, I can have something that returns P1<T>, much like Google Collections Supplier<T>, and have it compute the contained value only when P1._1() is called.

(huh, this blog post Lazy Error Handling in Java was interesting too.....)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文