独特的阵列COM prehension [英] Unique array comprehension

查看:108
本文介绍了独特的阵列COM prehension的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CoffeeScript中下面的数组:

I have the following array in CoffeeScript:

electric_mayhem = [ { name: "Doctor Teeth", instrument: "piano" },
                { name: "Janice", instrument: "piano" },
                { name: "Sgt. Floyd Pepper", instrument: "bass" },
                { name: "Zoot", instrument: "sax" },
                { name: "Lips", instrument: "bass" },
                { name: "Animal", instrument: "drums" } ]

我的目标是从这个数组得到所有乐器名称。

My goal is to get all instrument names from this array.

预期结果: ['钢琴','噌','萨克斯','鼓']

我的第一个解决方案:

names = (muppet.instrument for muppet in electric_mayhem)
output = {}
output[name] = name for name in names
names = (value for key, value of output)

这是相当长的。我looket在transpiled JavaScript和看到的第一行被翻译成:

This is rather long. I looket at the transpiled javascript, and saw the first line is translated to:

names = (function() {
  var _i, _len, _results;
  _results = [];
  for (_i = 0, _len = electric_mayhem.length; _i < _len; _i++) {
    muppet = electric_mayhem[_i];
    _results.push(muppet.instrument);
  }
  return _results;
})();

所以我做了一个使用transpilation过程中引入了 _results 变量超级哈克解决方案:

So I made up a super-hacky solution that uses the _results variable introduced during transpilation:

names = (muppet.instrument for muppet in electric_mayhem \
  when muppet.instrument not in _results)

它的工作原理,但我不喜欢它依赖于未记录的变量名称,因此它可能会CoffeeScript的未来版本的破解。

It works, but I don't like it relies on not documented variable name so it may break with future releases of CoffeeScript.

有没有一个班轮在非哈克的方式来实现独一无二的COM prehension?

Is there any one-liner to achieve "unique comprehension" in a non-hacky way?

推荐答案

这一个班轮可能会做的伎俩:

This one-liner will probably do the trick:

names = (key for key of (new -> @[o.instrument] = '' for o in electric_mayhem; @))
//                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                                 "object comprehension"

至于自己,我(AB)利用关联数组使用它的键作为组唯一值的。

As yourself, I (ab)used an associative array to use its keys as the set of unique values.

不过,真正的的这里采用的是一个对象COM prehension的构建阵列。或者至少,它最接近的等价形式目前市面上CoffeeScript的:

But, the real trick here is the use of an "object comprehension" to build that array. Or at least, its closest equivalent form currently available on CoffeeScript:

    (new -> @[o.instrument] = '' for o in electric_mayhem; @)
//   ^^^  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  ^  
//  create                 with                            |
//  a new                  that                            |
//  empty                anonymous                         |
//  object               constructor                       |
//                                           don't forget -/
//                                           to return the
//                                        newly created object

这篇关于独特的阵列COM prehension的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆