标签: seq

当我使用 Seq[T] 构造函数时创建了什么具体的数据类型?

Scala 允许我Seq使用以下语法直接实例化一个实现trait的对象:

val numbers: Seq[Int] = Seq(1, 2, 3)
val names: Seq[String] = Seq("Alice", "Bob", "Charles")
Run Code Online (Sandbox Code Playgroud)
  1. 既然Seq是 trait 而不是具体的实现,那么什么数据类型是numbers和 的基础names
  2. 我可以为自己解决这个问题的最佳方法是什么?
  3. 直接创建 trait 对象是惯用的吗?

谢谢!

collections scala traits seq

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

Nim:在数组或序列中查找项目的索引

是否有内置方法来查找数组或序列中项目的索引,相当于 Python index?(它可能返回第一次出现的索引,或所有索引。)

arrays seq nim-lang

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

如何在数据框中的两列上使用 seq 函数?

假设我有:

x <- c(1, 2, 3, 4, 5)
y <- c(2, 3, 6, 8, 10)
my.list <- list(start = x, end = y) %>% as.data.frame()

Run Code Online (Sandbox Code Playgroud)

我需要定义一个新变量,其中包含存储在该变量中的 seq(start,end) 或 start:end,我想要跨行的数字序列,例如,第一行为 1 2,第一行为 3 4 5 6第三排。

非常感谢

r seq

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

将List [Tuple2 [A,B]]转换为Tuple2 [Seq [A],Seq [B]]

陷入这里,尝试将一个case类元组的List转换为一个序列元组,并多次分配结果.

val items = repo.foo.list // gives me a List[(A,B)]
Run Code Online (Sandbox Code Playgroud)

我可以像这样完成多项任务:

val(a,b) = (items.map(_._1).toSeq, items.map(_._2).toSeq)
Run Code Online (Sandbox Code Playgroud)

但是,按照以下方式做一步就更好了:

val(a,b) = repo.foo.list.map{case(a,b) => (a,b)}
Run Code Online (Sandbox Code Playgroud)

scala tuples transform list seq

2
推荐指数
1
解决办法
243
查看次数

如何在没有"printf"的情况下调用Seq.whatever中的函数?

我是f#的新手,我试图编写一个程序,该程序应该遍历给定目录中的所有文件,并为每个类型为".txt"的文件添加一个id号+"DONE"到文件中.

我的节目:

//const:
[<Literal>]
let notImportantString= "blahBlah"
let mutable COUNT = 1.0

//funcs:
//addNumber --> add the sequence number COUNT to each file.
let addNumber (file : string)  =
 let mutable str = File.ReadAllText(file)
 printfn "%s" str//just for check
 let num = COUNT.ToString()
 let str4 = str + " " + num + "\n\n\n DONE"
 COUNT <- COUNT + 1.0
 let str2 =  File.WriteAllText(file,str4)
 file

//matchFunc --> check if is ".txt"
let matchFunc (file : string) =
 file.Contains(".txt") 

//allFiles --> …
Run Code Online (Sandbox Code Playgroud)

f# sequences seq

2
推荐指数
1
解决办法
1309
查看次数

R - 按组将日期范围扩展为面板数据

我有日期范围,由两个变量(idtype)分组,这两个变量当前存储在一个名为的数据框中data.我的目标是扩展日期范围,以便我在日期范围内每天都有一行,其中包括相同的idtype.

以下是重现数据框示例的代码段:

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("POSIXct", "POSIXt"
), tzone = ""), to = structure(c(1235372400, 1235545200, 1235631600, 
1235890800, 1236236400, 1242712800, 1243058400, 1243231200, 1243144800, 
1243576800), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("id", …
Run Code Online (Sandbox Code Playgroud)

r date seq

2
推荐指数
1
解决办法
1298
查看次数

R生成如111222333444555666777 ..... 505050的序列

我想生成一个序列,如:

c(rep(1,7),rep(2,7),rep(3,7),rep(4,7),rep(5,7),rep(6,7),rep(7,7)) 
Run Code Online (Sandbox Code Playgroud)

但对于大数而不是7,说100.

我怎么能这样做而不重复到100?

谢谢

sequences r repeat seq

2
推荐指数
1
解决办法
109
查看次数

在r中重复一些元素

我有一个包含以下内容的向量:

my.var <- c("a","b","c","d","e","f")
my.var
[1] "a" "b" "c" "d" "e" "f"
Run Code Online (Sandbox Code Playgroud)

我想只使用repseq函数来解决这个问题:

"a" "b" "c" "b" "c" "d" "c" "d" "e" "d" "e" "f"
Run Code Online (Sandbox Code Playgroud)

r vector seq rep

2
推荐指数
1
解决办法
88
查看次数

使用管道转发与嵌套序列

我是F#的新手,所以如果我使用不正确的名字,我会道歉.

我正在尝试使用F#来解析看起来像这样的网页:

<!--This is simplified, in reality there are more divs, anchors etc. -->
<html>
<body> 
    <div class="pr-single"><a href="http://google.ca">Google</a></div>
    <div class="pr-single"><a href="http://apple.com">Apple</a></div>
    <div class="pr-single"><a href="http://microsoft.com">Microsoft</a></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我宣布了一种类型

type PromoterPage = FSharp.Data.HtmlProvider<"http://somewebpage.com">
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试获取页面上所有链接的列表.我的思考过程是:

  1. 按类名获取所有外部div
  2. 获得所有这些div的后代
  3. 将这些后代收集到一个平面列表中
  4. 将此列表过滤为仅<a>标记

我的尝试如下:

let GetFirst (page:PromoterPage) = 
    page.Html.Descendants()
    |> Seq.filter(fun n -> n.HasClass("pr-single"))                 //Find the divs
    |> Seq.map(fun n -> n.Descendants())                            //Get the descendants
    |> Seq.collect(fun n -> n |> Seq.where(fun m -> m.HasName("a")) //Filter and collect the anchors
Run Code Online (Sandbox Code Playgroud)

问题似乎是你无法嵌套Seq函数或者我不能正确地执行它.我收到错误:

Incomplete …

f# seq

2
推荐指数
1
解决办法
128
查看次数

切片数组到seq没有副本

我试图得到的片Array作为Seq避免副本。我可以利用toSeq方法。

val array = Array[AnyRef](
  new Integer(1), 
  new Integer(2), 
  new Integer(3), 
  new Integer(4), 
  new Integer(5)
)
val seq = array.toSeq
array(1) = null
println(seq.mkString(",")) //1,null,3,4,5
Run Code Online (Sandbox Code Playgroud)

它工作正常:Ideone Live示例。数组未复制。但是当我尝试切成薄片时

val array = Array[AnyRef](
  new Integer(1),
  new Integer(2),
  new Integer(3),
  new Integer(4),
  new Integer(5)
)
val seq = array.toSeq.slice(0, 3)
array(1) = null
println(seq.mkString(",")) //1,2,3
Run Code Online (Sandbox Code Playgroud)

可以看到复制了:Ideone Live Example。我正在努力避免它。在Scala中有办法吗?

arrays scala seq

2
推荐指数
1
解决办法
95
查看次数

标签 统计

seq ×10

r ×4

scala ×3

arrays ×2

f# ×2

sequences ×2

collections ×1

date ×1

list ×1

nim-lang ×1

rep ×1

repeat ×1

traits ×1

transform ×1

tuples ×1

vector ×1