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)
Seq是 trait 而不是具体的实现,那么什么数据类型是numbers和 的基础names?谢谢!
是否有内置方法来查找数组或序列中项目的索引,相当于 Python index?(它可能返回第一次出现的索引,或所有索引。)
假设我有:
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第三排。
非常感谢
陷入这里,尝试将一个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) 我是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) 我有日期范围,由两个变量(id和type)分组,这两个变量当前存储在一个名为的数据框中data.我的目标是扩展日期范围,以便我在日期范围内每天都有一行,其中包括相同的id和type.
以下是重现数据框示例的代码段:
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) 我想生成一个序列,如:
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?
谢谢
我有一个包含以下内容的向量:
my.var <- c("a","b","c","d","e","f")
my.var
[1] "a" "b" "c" "d" "e" "f"
Run Code Online (Sandbox Code Playgroud)
我想只使用rep和seq函数来解决这个问题:
"a" "b" "c" "b" "c" "d" "c" "d" "e" "d" "e" "f"
Run Code Online (Sandbox Code Playgroud) 我是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)
现在我正在尝试获取页面上所有链接的列表.我的思考过程是:
<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 …
我试图得到的片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中有办法吗?