在朱莉娅(Julia),一个人可以在haskell术语中写下“章节”中的内容吗?

发布于 2025-02-13 21:13:03 字数 1099 浏览 0 评论 0原文


根据 haskell

在Haskell中,infix操作员的部分应用称为部分。

考虑Haskell Expression 过滤器(\ n - > n> 0)[-3,-4,5,6,-7,8],它评估为[5,6 ,8]

使用 ,可以在haskell中重写为filter(> 0)[-3,-4,5,6,-7,-7, 8]

Julia 中,可以写filter(n - > n> 0,[-3,-4,5,6,-7,8] )

可以使用Haskell部分(> 0)在Julia中重新编写这最后写吗?

以下会产生语法错误……

filter( (>0), [-3,-4,5,6,-7,8] )

更新

,在Haskell中可以重新编写…

过滤器(\ list - >长度列表> 2)[[2,3],[5,7,11],[13],[17,19,23,29]]

…作为 …

过滤器((&gt; 2).length)[[2,3],[5,7,11],[13],[17,19,23,29]] < /p>

in Julia ,可以使用部分和函数组成类似地重写吗?



According to A Gentle Introduction to Haskell

In Haskell the partial application of an infix operator is called a section.

Consider the Haskell expression filter (\n -> n > 0) [-3,-4,5,6,-7,8], which evaluates to [5,6,8] .

Using a section, this may be re-written in Haskell as filter (>0) [-3,-4,5,6,-7,8] .

In Julia, one may write filter( n -> n > 0, [-3,-4,5,6,-7,8] ) .

Can this last be re-written in Julia using an equivalent of the Haskell section (>0) ?

The following yields a syntax error …

filter( (>0), [-3,-4,5,6,-7,8] )

Update

Also, in Haskell one can re-write …

filter (\list -> length list > 2) [ [2,3], [5,7,11], [13], [17,19,23,29] ]

… as …

filter ((>2).length) [ [2,3], [5,7,11], [13], [17,19,23,29] ]

In Julia, can one similarly re-write, using a section and function composition?


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

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

发布评论

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

评论(2

蓝戈者 2025-02-20 21:13:03

不是句法,否。但是,一些操作员有用于部分应用“逻辑”参数的方法,在这些比较运算符中,从基本的所有比较运算符中:

julia> >(0)
(::Base.Fix2{typeof(>), Int64}) (generic function with 1 method)

julia> filter(>(0), [-3,-4,5,6,-7,8])
3-element Vector{Int64}:
 5
 6
 8

但是,一个可以自由编写实现某些语法技巧的宏。例如,来自 underscores.jl

@_ people |> filter(_.age > 40, __) |> map(_.name, __)

Not syntactically, no. But some operators have methods for partial application of the "logical" argument, among these all the comparison operators from Base:

julia> >(0)
(::Base.Fix2{typeof(>), Int64}) (generic function with 1 method)

julia> filter(>(0), [-3,-4,5,6,-7,8])
3-element Vector{Int64}:
 5
 6
 8

However, one is free to write macros that implement some syntactic tricks. E.g., from Underscores.jl:

@_ people |> filter(_.age > 40, __) |> map(_.name, __)
从来不烧饼 2025-02-20 21:13:03

对于您的第一个景象,您可以写作:

julia> filter(>(0), [-3,-4,5,6,-7,8])
3-element Vector{Int64}:
 5
 6
 8

这是有效的,因为根据帮助:

julia> ?
help?> >
>(x)

  Create a function that compares its argument to x using >, i.e. a function equivalent to y -> y > x. The returned function is of type Base.Fix2{typeof(>)}, which can be used to implement specialized methods.

  │ Julia 1.2
  │
  │  This functionality requires at least Julia 1.2.

因此,如果您想要第二个景象类似的东西,则可能需要定义自己的类似功能:

julia> length_sup(x) = y -> length(y) > x
length_sup (generic function with 1 method)

然后您可以做:

julia> filter(length_sup(2), [ [2,3], [5,7,11], [13], [17,19,23,29] ])
2-element Vector{Vector{Int64}}:
 [5, 7, 11]
 [17, 19, 23, 29]

但是,是否是一个好主意,或者不仅为某些糖连接的定制功能创建自定义功能将取决于您。您最终可能需要编码宏来简化此任务。

For your first exemple, you can write:

julia> filter(>(0), [-3,-4,5,6,-7,8])
3-element Vector{Int64}:
 5
 6
 8

This works because according to the help:

julia> ?
help?> >
>(x)

  Create a function that compares its argument to x using >, i.e. a function equivalent to y -> y > x. The returned function is of type Base.Fix2{typeof(>)}, which can be used to implement specialized methods.

  │ Julia 1.2
  │
  │  This functionality requires at least Julia 1.2.

So if you want something similar for your second exemple, you may need to define yourself a similar function like this:

julia> length_sup(x) = y -> length(y) > x
length_sup (generic function with 1 method)

And then you can do:

julia> filter(length_sup(2), [ [2,3], [5,7,11], [13], [17,19,23,29] ])
2-element Vector{Vector{Int64}}:
 [5, 7, 11]
 [17, 19, 23, 29]

However, whether it is a good idea or not to create custom functions just for some sugar-syntaxing will be up to you. You may eventually want to code macro to simplify this task.

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