我想要做的是创建一个函数zip(现在注意这不是作业),它同时迭代多个列表,将函数应用于每个元素列表,如下所示:
(zip f '(1 2 3) '(4 5 6) '(7 8 9)) = (list (f 1 4 7) (f 2 5 8) (f 3 6 9))
(zip f '(1 2 3 4) '(5 6) '(7 8 9)) = (list (f 1 5 7) (f 2 6 8))
Run Code Online (Sandbox Code Playgroud)
基本上,当任何列表用完元素时它会停止.这是我目前的尝试:
(defun zip (f &rest lists)
(if (apply and lists) ; <- Here is where I think the problem is.
(cons (apply f (mapcar #'car lists)) (zip f &rest (mapcar #'cdr …Run Code Online (Sandbox Code Playgroud) 我最近在Tomcat的JRuby上遇到了运行Sinatra的permgen内存泄漏.问题与Sinatra用来支持各种模板选项的Tilt库有关.旧代码(此处未包含)正在生成内存泄漏.新代码(下面)没有,事实上我发现permgen GC现在正在运行.
Ruby应该是自我描述的,但我无法通过阅读它来弄清楚这些代码.有嵌套的类逃逸.为什么?为什么定义一个方法然后解除绑定?
为什么代码编译了一堆模板并保留它们以便重复使用这么复杂的外观?
另外:如果有任何GitHub员工正在查看此问题,您能否向GitHub添加一些功能,允许用户在代码片段中插入问题?
(此代码取自https://github.com/rtomayko/tilt/blob/master/lib/tilt.rb)
def compile_template_method(locals)
source, offset = precompiled(locals)
offset += 5
method_name = "__tilt_#{Thread.current.object_id.abs}"
Object.class_eval <<-RUBY, eval_file, line - offset
#{extract_magic_comment source}
TOPOBJECT.class_eval do
def #{method_name}(locals)
Thread.current[:tilt_vars] = [self, locals]
class << self
this, locals = Thread.current[:tilt_vars]
this.instance_eval do
#{source}
end
end
end
end
RUBY
unbind_compiled_method(method_name)
end
Run Code Online (Sandbox Code Playgroud)