不多说上代码:
using Pipe
a=[[2,2],[1,3],[3,1]] #向量的向量,每个元素都是一个向量
@pipe a |> sort(_, lt=(x,y)->isless(x[1], y[1])) .|> last #以向量的第一个元素为依据排序,然后提取出第二个元素
到这里报错:
ERROR: MethodError: objects of type Vector{Vector{Int64}} are not callable
Use square brackets for indexing an Array.
但是奇怪的是拆成两个管道命令就可以:
b = @pipe a |> sort(_, lt=(x,y)->isless(x[1], y[1]))
b .|> last
甚至简单粗暴的@pipe b .|>_[2]就行。试来试去,只有这种方式可以一次性出结果:
@pipe a |> sort(_, lt=(x,y)->isless(x[1], y[1])) |> map(x->x[2], _)
这是为什么?
刚才试了下,向量也有类似问题:
a=[3,2,1]
这两种都报错
@pipe a |> sort(_, lt=(x,y)->isless(x, y)) .|> println
@pipe a |> sort(_) .|> println
但是a |> sort .|> println正常