如何在Ramda地图期间访问该集合?

时间:2017-05-24 23:29:36

标签: javascript ramda.js

给出一个非常简单的例子:

R.map((a, b) => {
  console.log(a, b) // 1, undefined
}, [1, 2, 3])

如何在地图中整体访问该集合?

使用该集合的一个人为设想的例子可能是:

R.map((item, collection) => {
  console.log(item === collection.length)
}, [1, 2, 3])

我可以将它存储在变量之前,但这是函数链的末尾

2 个答案:

答案 0 :(得分:2)

/Users/lizbayardelle/Dropbox/Code/OCR/config/initializers/zillow_api.rb:5:in `block in <top (required)>': uninitialized constant Zillow (NameError) from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:446:in `instance_exec' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:446:in `block in make_lambda' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:192:in `block in simple' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `block in call' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `each' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `call' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:92:in `__run_callbacks__' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:778:in `_run_prepare_callbacks' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:81:in `run_callbacks' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/actionpack-4.2.6/lib/action_dispatch/middleware/reloader.rb:83:in `prepare!' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/actionpack-4.2.6/lib/action_dispatch/middleware/reloader.rb:55:in `prepare!' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/application/finisher.rb:50:in `block in <module:Finisher>' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `instance_exec' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `run' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/initializable.rb:55:in `block in run_initializers' from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each' from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component' from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from' from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component' from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:347:in `each' from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:347:in `call' from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component' from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each' from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/initializable.rb:54:in `run_initializers' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/application.rb:352:in `initialize!' from /Users/lizbayardelle/Dropbox/Code/OCR/config/environment.rb:5:in `<top (required)>' from /Users/lizbayardelle/Dropbox/Code/OCR/config.ru:3:in `block in <main>' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/rack-1.6.5/lib/rack/builder.rb:55:in `instance_eval' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/rack-1.6.5/lib/rack/builder.rb:55:in `initialize' from /Users/lizbayardelle/Dropbox/Code/OCR/config.ru:in `new' from /Users/lizbayardelle/Dropbox/Code/OCR/config.ru:in `<main>' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/rack-1.6.5/lib/rack/builder.rb:49:in `eval' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/rack-1.6.5/lib/rack/builder.rb:49:in `new_from_string' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/rack-1.6.5/lib/rack/builder.rb:40:in `parse_file' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/rack-1.6.5/lib/rack/server.rb:299:in `build_app_and_options_from_config' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/rack-1.6.5/lib/rack/server.rb:208:in `app' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/commands/server.rb:61:in `app' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/rack-1.6.5/lib/rack/server.rb:336:in `wrapped_app' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/commands/server.rb:139:in `log_to_stdout' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/commands/server.rb:78:in `start' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:80:in `block in server' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:in `tap' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:in `server' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!' from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>' from bin/rails:9:in `require' from bin/rails:9:in `<main>' 不允许您访问映射函数中的集合,但您可以在映射函数周围创建一个闭包,以便为自己提供为集合指定名称的机会。

COUNTIF

答案 1 :(得分:1)

正如评论所暗示的那样,Ramda默认不包含此内容是有充分理由的。但是有一个名为addIndex的装饰器,它添加了索引和集合:

const fn = R.addIndex(map)((elt, idx, coll) => elt == coll.length);
fn([1, 2, 3]); //=> [false, false, true]