在 JavaScript 中,使用 Prototype 库,以下函数构造是可能的:
var words = ["aqueous", "strength", "hated", "sesquicentennial", "area"];
words.pluck('length');
//-> [7, 8, 5, 16, 4]
请注意,此示例代码等效于
words.map( function(word) { return word.length; } );
我想知道在 F# 中是否有类似的东西:
let words = ["aqueous"; "strength"; "hated";"sesquicentennial"; "area"]
//val words: string list
List.pluck 'Length' words
//int list = [7; 8; 5; 16; 4]
无需写:
List.map (fun (s:string) -> s.Length) words
这对我来说似乎很有用,因为这样您就不必为每个属性编写函数来访问它们。