我对f#很陌生,而且我很难从列表中提取不同值的列表:
class A
def A.test(data)
puts data
end
end
def ps(klass, method_name)
klass.send(method_name, 3)
end
ps(A, :test) # => 3
我该怎么做?我看到let myList = [ 1; 2; 2; 3; 4; 3 ]
// desired list
[ 1; 2; 3; 4 ]
有一个seq
方法,但没有列表。
答案 0 :(得分:14)
let myList = [ 1; 2; 2; 3; 4; 3 ]
let distinctList = myList |> Seq.distinct |> List.ofSeq
结果:
>
val myList : int list = [1; 2; 2; 3; 4; 3]
val distinctList : int list = [1; 2; 3; 4]
下一个F#版本(4.0)将具有List.distinct
功能
答案 1 :(得分:3)
不确定是否比Petr answer更差或更好,但您也可以这样做:
let distinct xs = xs |> Set.ofList |> Set.toList
> distinct [ 1; 2; 2; 3; 4; 3 ];;
val it : int list = [1; 2; 3; 4]