Differences with R
Types
Durability
ztsdb array types can be persisted to disk. See the reference section Durability and the discussion about persistence in the reference section Arrays of the reference document.
Locking
ztsdb arrays can be locked for copying. See run section Locking.
New types
connection
, timer
, zts
are new types in ztsdb.
Type limitations in R
In ztsdb, nanoduration
, nanoperiod
, nanoival
, nanotime
are
array types. zts
is a combined type with a nanotime
index and
double
data part. These types are true arrays; they are n-dimensional and
have array indexing semantics, etc..
This is not the case in R for non-base types; due to limitations in R,
the types nanoduration
, nanoperiod
, nanoival
, nanotime
are
vectors and cannot be used in matrices or arrays. Similarly zts
maps
to a data.table
with a first column of nanotime
type and the
subsequent columsn with type numeric
. Here again R is not able to
represent n-dimensionality.
NA
ztsdb does not have NA. All values of nanoduration
, nanoperiod
,
nanoival
, nanotime
, character
and logical
are valid. double
has
NaN, but not NA. The NaN semantic is that of the CPU's floating point
unit.
Type conversion
Since any object may be permanent and potentially very large, ztsdb does not allow a type conversion that modifies the type of the object on the left hand side.
### in R
a <- c(1,2,3,4)
a[3] <- "a" # a is now the character array [1] "1" "2" "a" "4"
### in ztsdb
a <- c(1,2,3,4)
a[3] <- "a" # Error: conversion not defined for string to double
But type conversions that do not modify the target work like in R:
a <- c("a","b","c","d")
a[3] <- 20 # character array, 20 becomes a string: [1] "a" "b" "20" "d"
list
list
cannot have a dimensionality associated with it. As a consequence, the bind functions will not work with lists.### in R: rbind(list(1,2), list(3,4)) ### [,1] [,2] ### [1,] 1 2 ### [2,] 3 4 ### in ztsdb: rbind(list(1,2), list(3,4)) ### Error: cbind incorrect type
list
can only be extended when using acharacter
index.### in R: l <- list(a=1, b=2, c=3) l[[4]] <- 4 ### ### $a ### [1] 1 ### ### $b ### [1] 2 ### ### $c ### [1] 3 ### ### [[4]] ### [1] 4 ### in ztsdb: l <- list(a=1, b=2, c=3) l[[4]] <- 4 ### ### Error: 1.4: recursive indexing failed at level 1 ### 1: l[[4]] <- 4 ### ### but the following works: l[["d"]] <- 4 ### $a ### [1] 1 ### ### $b ### [1] 2 ### ### $c ### [1] 3 ### ### $d ### [1] 4 ###
Functions and scoping
Arguments
Arguments can be passed by reference with the
--
operator. See run section .ztsdb arguments are immediately evaluated; there is no concept of lazy evaluation. As a corallary, the order of evaluation is not determined by when the argument is accessed.
There is no partial matching of variable names in functions.
Functions are not closures.
In R, left hand assignment to a function value is not truly general, but it is in ztsdb.
### does not work in R: names(matrix(1, 2, 2, dimnames=list(c("a","b"), c("1","2")))) <- list(NULL, c("hoho", "haha")) ### Error in names(matrix(1, 2, 2, dimnames = list(c("a", "b"), c("1", "2")))) <- list(NULL, : ### target of assignment expands to non-language object ### works in ztsdb
Scoping
Because it simplifies significantly the interpreter, ztsdb is dynamically scoped whereas R is lexically scoped. The following code yields 5 in ztsdb and 4 in R:
a <- 1; b <- 2 f<-function(x) a*x + b g<-function(x) { a <- 2; b <- 1; f(x) } g(2)
Control structures
for loops in R "freeze" the array/list object, but ztsdb operates directly on the object:
### in R: b <- c(1,2,3) for (a in b) { b[2]<-10; print(a) } # prints # [1] 1 # [1] 2 # [1] 3 ### in ztsdb: b <- c(1,2,3) for (a in b) { b[2]<-10; print(a) } # prints # [1] 1 # [1] 10 # [1] 3
The
return
keyword is not implemented; functions always return the result of the last statement evaluated.break
andnext
are not implemented. This will be reconsidered.
Miscellaneous
The visibility rules, i.e. when the REPL prints the result of an evaluation, are slightly different. The general rule is that the results of assignments are not printed. R will also not print the result if it is enclosed in braces, but it will print it when in parenthesis. ztsdb will not print assignment expressions in parenthesis). For example:
### in R: 3 # prints (3) # prints {3} # prints (a <- 2) # prints a <- 2 # doesn't print {a <- 2} # doesn't print ### in ztsdb 3 # prints (3) # prints {3} # prints (a <- 2) # doesn't print a <- 2 # doesn't print {a <- 2} # doesn't print