我发现自己经常做以下事情:
val adjustedActions = actions.scanLeft((1.0, null: CorpAction)){
case ((runningSplitAdj, _), action) => action match {
case Dividend(date, amount) =>
(runningSplitAdj, Dividend(date, amount * runningSplitAdj))
case s @ Split(date, sharesForOne) =>
((runningSplitAdj * sharesForOne), s)
}
}
.drop(1).map(_._2)
Run Code Online (Sandbox Code Playgroud)
runningSplitAdj在这种情况下,我需要积累,以便纠正动作列表中的股息.在这里,我scan用来维护我需要的状态以纠正动作,但最后,我只需要动作.因此,我需要在状态中使用null作为初始操作,但最后,删除该项并映射所有状态.
是否有更优雅的方式来构建这些? 在RxScala Observables的上下文中,我实际上创建了一个新的运算符(在RxJava邮件列表的一些帮助之后):
implicit class ScanMappingObs[X](val obs: Observable[X]) extends AnyVal {
def scanMap[S,Y](f: (X,S) => (Y,S), s0: S): Observable[Y] = {
val y0: Y = null.asInstanceOf[Y]
// drop(1) because scan also emits initial state
obs.scan((y0, s0)){case ((y, s), x) …Run Code Online (Sandbox Code Playgroud) 生成整数因子的scala方法是什么?这是我的看法1:
def factorize(x: Int): List[Int] = {
def foo(x: Int, a: Int): List[Int] = {
if (a > Math.pow(x, 0.5))
return List(x)
x % a match {
case 0 => a :: foo(x / a, a)
case _ => foo(x, a + 1)
}
}
foo(x, 2)
}
Run Code Online (Sandbox Code Playgroud)
factorize(360)//列表(2,2,2,3,3,5)
根据@ SpiderPig和@ seth-tisue的评论取2
def factorize(x: Int): List[Int] = {
def foo(x: Int, a: Int): List[Int] = {
(a*a < x, x % a) match {
case (true, 0) => a …Run Code Online (Sandbox Code Playgroud) 当type参数为Int时,我无法弄清楚如何获取Option的type参数
我尝试了以下代码:
class TestClass {
val intField: Option[Int] = Some(1)
}
val cls = new TestClass
val field = cls.getDeclaredField("intField")
val typeParams = field.getGenericType.asInstanceOf[ParameterizedType].getActualTypeArguments
Run Code Online (Sandbox Code Playgroud)
typeParams给了我java.lang.Object
我的问题是如何让它将java.lang.Integer返回给我
说我有两种类型的演员:Master和Slave
我向Slaves派遣新职位,等待他们的回复并处理回复.Master所有奴隶完成后我应该如何完成循环?
例如:
class Slave extends Actor {
def act() {
loop { react {
...
sender ! FinishedAll // send mesage to the master
...
} }
}
}
class Master extends Actor {
loop { react {
...
case FinishedAll => exit // grrr!
...
}
Run Code Online (Sandbox Code Playgroud)
我从Scala和Actors开始,所以答案可能很简单:)
您能更正我的代码吗?我正在尝试通过拆分 x11 来打印 x22。
object ScalaString {
def main(args: Array[String]): Unit = {
var x =" Hello world"
x.filter(_!='l').foreach(println)
//for(c <- x) println(c)
println(x.stripPrefix(" ").drop(2).take(2).capitalize)
**var x11=" hello|world|india"
var x22=( x11.split("|").map(_.trim()))
println(x22.toString())**
}
}
output:
w
o
r
d
Ll
**[Ljava.lang.String;@56431753**
Run Code Online (Sandbox Code Playgroud) 尝试使用案例类在Scala中实现链表时,为什么会出现错误?
因为我对Haskell非常熟悉,所以我基本上试图将这个Haskell代码转换为Scala:
Data List = Cons Int List | Nil
instance Show List where
show x = helper x ""
where
helper Nil acc = acc
helper (Cons n xs) acc = let y = acc ++ " " ++ show n in helper xs y
Run Code Online (Sandbox Code Playgroud)
这是我的Scala代码:
abstract class List1
case class Cons(head : Int, tail : List1) extends List1
case object Nil extends List1
object thing {
def printList (x : List1) {
var acc = x
while (acc …Run Code Online (Sandbox Code Playgroud) 鉴于:
scala> trait Resource[A] { def f: String }
defined trait Resource
scala> case class Foo(x: String)
defined class Foo
Run Code Online (Sandbox Code Playgroud)
然后暗示:
scala> implicit def fooToResource(foo: Foo): Resource[Foo] =
new Resource[Foo] { def f = foo.x }
Run Code Online (Sandbox Code Playgroud)
以下作品:
scala> implicitly[Resource[Foo]](Foo("foo")).f
res2: String = foo
Run Code Online (Sandbox Code Playgroud)
我定义了一个函数:
scala> def f[A](x: A)(implicit ev: Resource[A]): String = ev.f
f: [A](x: A)(implicit ev: Resource[A])String
Run Code Online (Sandbox Code Playgroud)
但是,以下代码无法编译:
scala> f(Foo("foo"))
<console>:17: error: could not find implicit value for parameter ev: Resource[Foo]
f(Foo("foo"))
Run Code Online (Sandbox Code Playgroud)
其次,我尝试过:
scala> f2(Foo("bippy"))
<console>:17: error: could not …Run Code Online (Sandbox Code Playgroud)