小编pet*_*ord的帖子

你能更清楚地解释R函数运算符中的惰性求值吗?

如果我创建一个函数如下:

what_is_love <- function(f) {
  function(...) {
    cat('f is', f, '\n')
  }
}
Run Code Online (Sandbox Code Playgroud)

并称之为lapply: funs <- lapply(c('love', 'cherry'), what_is_love)

我得到了意外的输出:

> funs[[1]]()
f is cherry
> funs[[2]]()
f is cherry
Run Code Online (Sandbox Code Playgroud)

但请注意,当您不使用时,情况并非如此lapply:

> f1 <- what_is_love('love')
> f2 <- what_is_love('cherry')
> f1()
f is love
> f2()
f is cherry
Run Code Online (Sandbox Code Playgroud)

是什么赋予了?

我知道funs <- lapply(c('love', 'cherry'), what_is_love)可以写得更全面:

params <- c('love', 'cherry')
out <- vector('list', length(params))
for (i in seq_along(params)) {
  out[[i]] <- what_is_love(params[[i]])
}
out
Run Code Online (Sandbox Code Playgroud)

但是当我浏览时,我发现两个函数都有自己的环境: …

r lazy-evaluation environments

13
推荐指数
1
解决办法
1953
查看次数

ArgumentError:工厂未注册

我正在尝试使用FactoryGirl运行rspec,但我一直收到此错误:

1) Products Update with invalid information
     Failure/Error: let(:product) { FactoryGirl.create(:product) }
     ArgumentError:
       Factory not registered: product
     # ./spec/requests/products_spec.rb:47:in `block (3 levels) in <top (required)>'
     # ./spec/requests/products_spec.rb:52:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

-

这是带错误的测试(spec/requests/products_spec.rb):

describe "Read" do
    let(:product) { FactoryGirl.create(:product) }
    before { visit product_path(product) }
    it { should have_text(product.title) }
end
Run Code Online (Sandbox Code Playgroud)

-

这是工厂(spec/factories.rb):

FactoryGirl.define do
    factory :product do
        title "Lorem ipsum"
        description "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce vitae …
Run Code Online (Sandbox Code Playgroud)

ruby rspec ruby-on-rails factory-bot

9
推荐指数
1
解决办法
9426
查看次数

RSpec上的错误行为期望更改计数与销毁

我希望用户只能删除他们自己创建的作业(而不是其他人的作业).我已经验证我的应用程序通过"rails server"执行此操作.然而,测试结果很奇怪.

以下是有问题的测试:

require 'spec_helper'

describe "Authentication" do
  subject { page }

  describe "signin" do
    [...]

    describe "authorization" do
      [...]

      describe "correct user control" do
        let(:user) { FactoryGirl.create(:user) }
        let(:job) { FactoryGirl.create(:job, user: user) }
        let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
        let(:wrong_job) { FactoryGirl.create(:job, user: wrong_user) }
        before { sign_in user }
        [...]
        describe "users can only delete their own jobs" do
          it "should not change job count" do
            expect do
              delete job_path(wrong_job)
            end.to_not change(Job, :count)
          end
        end
        describe "users …
Run Code Online (Sandbox Code Playgroud)

ruby rspec ruby-on-rails

3
推荐指数
1
解决办法
3598
查看次数

如何在Excel中获取行中的最大N个数字?

MAX将取一个范围并告诉我最大的数字.但是,如果我想迭代该范围并连续找到最大的两个数字呢?

例如,如果我的范围为[0,2,5,6,9,3,8],则MAX为9,但MAX2为15(6 + 9).MAX3为20(5 + 6 + 9).

如何在Excel中编写MAX2,MAX3或MAXN?

excel excel-formula

3
推荐指数
1
解决办法
1044
查看次数