我有带方括号的日期[2014-11-08 06:27:00.0],并且想要删除它。
预期输出为2014-11-08 06:27:00.0
val conf = new SparkConf(true)
.set("spark.cassandra.connection.host", "127.0.0.1").setAppName("CasteDate").setMaster("local[*]")
.set("spark.cassandra.connection.port", "9042")
.set("spark.driver.allowMultipleContexts", "true")
.set("spark.streaming.receiver.writeAheadLog.enable", "true")
val sc = new SparkContext(conf)
val ssc = new StreamingContext(sc, Seconds(1))
val csc=new CassandraSQLContext(sc)
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
var input: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S")
input.setTimeZone(TimeZone.getTimeZone("GMT"))
var dia: SimpleDateFormat = new SimpleDateFormat("dd")
var mes: SimpleDateFormat = new SimpleDateFormat("MM")
var ano: SimpleDateFormat = new SimpleDateFormat("yyyy")
var horas: SimpleDateFormat = new SimpleDateFormat("HH")
var minutos: SimpleDateFormat = new SimpleDateFormat("mm")
val data=csc.sql("SELECT timecol …Run Code Online (Sandbox Code Playgroud) 我的功能是
val f2 : (String, String) => Int = new Function2[String, String, Int] {
def apply(s1 : String, s2 : String) = s1.length + s2.length
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能在Scala中这样做
val listOfFullNames = List(("Mark","Smith"), ("Jim","Duggan"), ("Eddie","Murphy"), ("Sylvester","Stallone"))
val output3 = listOfFullNames.map(f2)
Run Code Online (Sandbox Code Playgroud)
错误消息很有意思,它说预期:(String,String)=> TypeInferredB,但是real(String,String)=> Int
//But this works fine
val output3 = listOfFullNames.map(x => f2(x._1, x._2))
Run Code Online (Sandbox Code Playgroud)
我这样做的原因纯粹是因为,这是有效的
val f :String => Int = new Function[String, Int] {
def apply(s : String) = s.length
}
Run Code Online (Sandbox Code Playgroud)
这可以这样使用
val listOfNames = List("Mark", "Jim", "Eddie", "Sylvester","Stallone")
val output …Run Code Online (Sandbox Code Playgroud) CodeWars 再次挑战。今天我遇到了这个问题:
“您的任务是将给定尺寸 nxm 的巧克力棒分成小方块。每个方块的大小为 1x1 且牢不可破。实现一个函数,该函数将返回所需的最少中断次数。
例如,如果给您一块 2 x 1 大小的巧克力棒,您可以在一次中断时将其拆分为单个方块,但对于 3 x 1 大小,您必须进行两次中断。
如果输入数据无效,您应该返回 0(因为如果我们没有任何可拆分的巧克力,则不需要中断)。输入将始终是一个非负整数。”
出于某种原因,无论我提供巧克力棒的哪一面,输出始终为 0。
我已经尝试过的:
object breakChocolate {
var result = 0
def breakChocolate(n: Int, m: Int) = {
var t = n*m
var i =0
def breaking(y:Int): Unit ={
if (t ==0 || t ==1)
result = i
else {
breaking(t%2)
i +=1
}
}
result
}
}
Run Code Online (Sandbox Code Playgroud)
以下是测试:
测试结果:TestCases breakChocolate(5, 5) 应该返回 24 Test Failed
0 不等于 24 堆栈跟踪在 38 毫秒内完成 breakChocolate(7, …
假设我有一个整数序列和一个 n < 30 的数字。如何生成一个数组(长度为 n),除了序列指定的索引(它应该是 1)外,所有地方都为 0?
例如
输入:
Seq(1, 2, 5)
7
Run Code Online (Sandbox Code Playgroud)
输出:
Array(0, 1, 1, 0, 0, 1, 0)
Run Code Online (Sandbox Code Playgroud) 所以这是代码:
package week4
object expr {
abstract class Expr[T] {
def eval:T = this match {
case Number(x) => x
case Sum(e1, e2) => e1.eval + e2.eval
}
def show: String = this match {
case Number(x) => "" + x
case Sum(e1, e2) => "(" + e1.show + "+" + e2.show + ")"
}
}
case class Number[T](val value: T) extends Expr {
}
case class Sum[T](val e1: Expr[T], val e2: Expr[T]) extends Expr {
}
}
Run Code Online (Sandbox Code Playgroud)
除了我得到所有案例比较的错误:
构造函数无法实例化为期望的类型; …
我一直试图理解为什么 Scala Futures 被认为是急切的并且违反了引用透明性。我想我理解这部分是合理的。但是,我无法理解这意味着什么:
(A => Unit) => Unit
关于未来。
我不确定这是否是正确的论坛,但感谢 ELI5 的回答
我需要获取从 10^-10 到 10^10 的 100 个值的随机序列,并Array使用 Scala存储到一个。我尝试跟随,但没有用
Array(scala.math.pow(10,-10).doubleValue to scala.math.pow(10,10).intValue by scala.math.pow(10,5).toLong)
Run Code Online (Sandbox Code Playgroud)
谁能帮我弄清楚如何正确地做到这一点?
在 Scala、Haskell 等 FP 语言中,使用了纯函数,这使得编译器可以优化代码。例如:
val x = method1()// a pure function call
val y = method2// another pure function call
val c = method3(x,y)
Run Code Online (Sandbox Code Playgroud)
由于method1和method2是纯函数,因此评估彼此独立,编译器可以并行化这两个调用。
像 Haskell 这样的语言内部有结构(比如 IO monad),它告诉函数是纯函数还是执行一些 IO 操作。但是 Scala 编译器如何检测一个函数是纯函数呢?
我写了以下简单的代码:
import cats.effect.IO
import cats.instances.either._
import cats.syntax.TraverseSyntax
object Test extends App with TraverseSyntax{
val e: Either[String, IO[Int]] = Right(IO(2))
e.sequence //error here
}
Run Code Online (Sandbox Code Playgroud)
不幸的是它拒绝编译
Error:(25, 94) value sequence is not a member of scala.util.Either
Run Code Online (Sandbox Code Playgroud)
你能解释一下原因吗?我导入了either包含的实例Traverse[Either[A, ?]].怎么了?
我正在尝试使用 Scala 练习递归和尾递归函数,我创建了一个尾递归函数来对两个列表中的值求和。我也尝试对递归执行相同的操作,但我能想到的唯一方法是每次调用方法时修改参数,如尾递归。你能帮我吗?
def callTailRecursive(list1 : List[Int], list2 : List[Int]) : List[Int] = {
def callHelper(list1 : List[Int], list2 : List[Int], outputList : List[Int]): List[Int] ={
if(!list1.isEmpty && !list2.isEmpty){
callHelper(list1.tail,list2.tail,outputList:+(list1.head + list2.head))
}else if(list1.isEmpty && !list2.isEmpty){
callHelper(list1,list2.tail,outputList:+(list2.head))
}else if(!list1.isEmpty && list2.isEmpty){
callHelper(list1.tail,list2,outputList:+(list1.head))
}else{
outputList
}
}
callHelper(list1,list2,List())
}
def callRecursive(n : Int, list1 : List[Int], list2 : List[Int], outputList : List[Int]): List[Int] = {
}
Run Code Online (Sandbox Code Playgroud)