seq

Time cost of Haskell `seq` operator

江枫思渺然 提交于 2019-12-03 11:15:56
This FAQ says that The seq operator is seq :: a -> b -> b x seq y will evaluate x, enough to check that it is not bottom, then discard the result and evaluate y. This might not seem useful, but it means that x is guaranteed to be evaluated before y is considered. That's awfully nice of Haskell, but does it mean that in x `seq` f x the cost of evaluating x will be paid twice ("discard the result")? The seq function will discard the value of x , but since the value has been evaluated, all references to x are "updated" to no longer point to the unevaluated version of x , but to instead point to

Adding an item to an immutable Seq

走远了吗. 提交于 2019-12-03 07:21:15
问题 Say, I have a sequence of strings as an input and I want to get a new immutable Seq which consists of elements of the input and an item "c" . Here are two methods that I've discovered to be working: assert(Seq("a", "b", "c") == Seq("a", "b") ++ Seq("c")) - the problem with this one is that it seems that instantiating a temporary sequence ( Seq("c") ) just for the sake of the operation is rendundant and will result in overhead assert(Seq("a", "b", "c") == List("a", "b") ::: "c" :: Nil) - this

第十章 Scala 容器(一):整体介绍

别来无恙 提交于 2019-12-03 04:11:04
1. 整体架构 Scala容器类是非常丰富的,整体架构也比较复杂,下面我们来根据图(10-2)来认识一下。Scala的容器类都是从Traversable和Iterable这两个trait开始的,然后分为三大类,分别是Seq,Set和Map,然后Seq又分为IndexedSeq和LinearSeq两种。其中IndexedSeq可以理解为数组形式,类似于Java中的ArrayList,而LinearSeq是以链表的形式存储的,类似于Java中的LinkedList。然后我们回来再看一下顶端的两个trait,其中Traversable允许你通过for each来重复遍历容器中的内容,而Iterable允许你使用Iterable遍历整个容器,区别是实用iterable遍历容器时,对于容器内的每个元素只能遍历到一次,这一点鹤Java的Iterable保持一致。 2. Seq 我们来看一下Seq的架构图,刚才已经提到Seq分类IndexedSeq和LinearSeq两大类。 其中IndexSeq允许你通过索引(下标)来随机访问,比如你想获取容器内下标为5000的值,你可以直接使用IndexedSeq(5000)来实现,IndexdSeq的默认实现为Vector。 scala> val x = IndexedSeq(1,2,3) x: IndexedSeq[Int] = Vector(1, 2,

Adding an item to an immutable Seq

雨燕双飞 提交于 2019-12-02 21:01:38
Say, I have a sequence of strings as an input and I want to get a new immutable Seq which consists of elements of the input and an item "c" . Here are two methods that I've discovered to be working: assert(Seq("a", "b", "c") == Seq("a", "b") ++ Seq("c")) - the problem with this one is that it seems that instantiating a temporary sequence ( Seq("c") ) just for the sake of the operation is rendundant and will result in overhead assert(Seq("a", "b", "c") == List("a", "b") ::: "c" :: Nil) - this one restricts the type of input collection to be a List , so Seq("a", "b") ::: "c" :: Nil won't work.

R — Expand date range into panel data by group

耗尽温柔 提交于 2019-12-02 05:00:44
问题 I have date ranges that are grouped by two variables ( id and type ) that are currently stored in a data frame called data . My goal is to expand the date range such that I have a row for each day within the range of dates, which includes the same id and type . Here is a snippet to reproduce an example of the data frame: data <- structure(list(id = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2), type = c("a", "a", "b", "c", "b", "a", "c", "d", "e", "f"), from = structure(c(1235199600, 1235545200, 1235545200

R — Expand date range into panel data by group

老子叫甜甜 提交于 2019-12-02 00:25:43
I have date ranges that are grouped by two variables ( id and type ) that are currently stored in a data frame called data . My goal is to expand the date range such that I have a row for each day within the range of dates, which includes the same id and type . Here is a snippet to reproduce an example of the data frame: data <- structure(list(id = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2), type = c("a", "a", "b", "c", "b", "a", "c", "d", "e", "f"), from = structure(c(1235199600, 1235545200, 1235545200, 1235631600, 1235631600, 1242712800, 1242712800, 1243058400, 1243058400, 1243231200), class = c(

Creating a repeating vector sequence in R [duplicate]

99封情书 提交于 2019-12-01 11:56:22
This question already has an answer here: R: generate a repeating sequence based on vector 1 answer I need some help. How to create the following vector sequence: 1 1 1 1 2 2 2 3 3 4 I tried to use (rep) and (seq) but still unsucessfull. Try this: rep(1:4,4:1) Output: [1] 1 1 1 1 2 2 2 3 3 4 or less concisely: c(rep(1, 4), rep(2,3), rep(3, 2), 4) output: [1] 1 1 1 1 2 2 2 3 3 4 来源: https://stackoverflow.com/questions/46167499/creating-a-repeating-vector-sequence-in-r

Creating a repeating vector sequence in R [duplicate]

半腔热情 提交于 2019-12-01 10:19:26
问题 This question already has an answer here : R: generate a repeating sequence based on vector (1 answer) Closed 2 years ago . I need some help. How to create the following vector sequence: 1 1 1 1 2 2 2 3 3 4 I tried to use (rep) and (seq) but still unsucessfull. 回答1: Try this: rep(1:4,4:1) Output: [1] 1 1 1 1 2 2 2 3 3 4 回答2: or less concisely: c(rep(1, 4), rep(2,3), rep(3, 2), 4) output: [1] 1 1 1 1 2 2 2 3 3 4 来源: https://stackoverflow.com/questions/46167499/creating-a-repeating-vector

Missing last sequence in seq() in R

烂漫一生 提交于 2019-12-01 09:17:29
I have this example data by<-200 to<-seq(from=by,to=35280,by=by) Problem is that to ends at 35200 and ignore the last 80 which I need to involve in as last value. Is there any straigthforward way how to achieve it? I have tried along.with and length.out parameters but I cannot go trough. You can place if statement for the last element of the vector such as in the following function : seqlast <- function (from, to, by) { vec <- do.call(what = seq, args = list(from, to, by)) if ( tail(vec, 1) != to ) { return(c(vec, to)) } else { return(vec) } } Then by <- 200 to <- seqlast(from=by,to=35280,by

Clustering rows by group based on column value

天大地大妈咪最大 提交于 2019-12-01 08:25:37
I have the following: df <- data.frame(ID = c(1,1,1,1,1,1,1,1,1,1,2,2,2), Obs = c(0,1, 1, 0, 1,0,0, 1, 1, 1, 0,0,1)) And I want this: df <- data.frame(ID = c(1,1,1,1,1,1,1,1,1,1,2,2,2), Obs = c(0,1, 1, 0, 1,0,0, 1, 1, 1, 0,0,1), Cluster = c(0,1,1,1,2,2,2,3,3,3,0,0,1)) How can I obtain 'Cluster' column in which I have to sequence the number of 1 until the first 0 appears, with dplyr? Consecutive 0's have to maintain the value until a new one appears. EDIT How can I do that, with many columns? Suppose that I have 99 obs columns, and I would like to create 99 clusters, one for each column. Like