在Clojure中,使用惰性序列构造函数很容易创建无限序列.例如,
(def N (iterate inc 0))
Run Code Online (Sandbox Code Playgroud)
返回一个N等价于无限序列的数据对象
(0 1 2 3 ...)
Run Code Online (Sandbox Code Playgroud)
评估该值会N导致无限循环.评估(take 20 N)返回前20个数字.由于序列是惰性的,因此inc只有在您要求时才会迭代该函数.由于Clojure是同质的,因此递归地存储惰性序列.
在R中,是否可以做类似的事情?你能提出一些样本R代码,它产生的数据对象N相当于完全无限的自然数序列吗?评估完整对象N应该会产生一个循环,但类似的东西head(N)应该只返回前导数字.
注意:我对懒惰序列而不是自然数本身更感兴趣.
编辑:以下是Clojure的来源lazy-seq:
(defmacro lazy-seq
"Takes a body of expressions that returns an ISeq or nil, and yields
a Seqable object that will invoke the body only the first time seq
is called, and will cache the result and return it on all subsequent
seq calls. See also …Run Code Online (Sandbox Code Playgroud) 我正在尝试运行一些R代码,因为内存而崩溃.我得到的错误是:
Error in sendMaster(try(lapply(X = S, FUN = FUN, ...), silent = TRUE)) :
long vectors not supported yet: memory.c:3100
Run Code Online (Sandbox Code Playgroud)
产生麻烦的功能如下:
StationUserX <- function(userNDX){
lat1 = deg2rad(geolocation$latitude[userNDX])
long1 = deg2rad(geolocation$longitude[userNDX])
session_user_id = as.character(geolocation$session_user_id[userNDX])
#Find closest station
Distance2Stations <- unlist(lapply(stationNDXs, Distance2StationX, lat1, long1))
# Return index for closest station and distance to closest station
stations_userX = data.frame(session_user_id = session_user_id,
station = ghcndstations$ID[stationNDXs],
Distance2Station = Distance2Stations)
stations_userX = stations_userX[with(stations_userX, order(Distance2Station)), ]
stations_userX = stations_userX[1:100,] #only the 100 closest stations...
row.names(stations_userX)<-NULL
return(stations_userX)
} …Run Code Online (Sandbox Code Playgroud)