scala-2.10

How to remove an item from a list in Scala having only its index?

我们两清 提交于 2019-12-18 03:16:24
问题 I have a list as follows: val internalIdList: List[Int] = List() internalIdList = List(11, 12, 13, 14, 15) From this list would remove the third element in order to obtain: internalIdList = List(11, 12, 14, 15) I can not use a ListBuffer , are obliged to maintain the existing structure. How can I do? Thanks to all 回答1: Simply use val trunced = internalIdList.take(index) ++ internalIdList.drop(index + 1) This will also work if index is larger than the size of the list (It will return the same

Flatten Scala Try

安稳与你 提交于 2019-12-18 02:19:40
问题 Is there a simple way to flatten a collection of try's to give either a success of the try values, or just the failure? For example: def map(l:List[Int]) = l map { case 4 => Failure(new Exception("failed")) case i => Success(i) } val l1 = List(1,2,3,4,5,6) val result1 = something(map(l1)) result1: Failure(Exception("failed")) val l2 = List(1,2,3,5,6) val result2 = something(map(l2)) result2: Try(List(1,2,3,5,6)) And can how would you handle multiple Failures in the collection? 回答1: Maybe not

How do you update multiple columns using Slick Lifted Embedding?

邮差的信 提交于 2019-12-17 21:55:02
问题 How do you update multiple columns using Slick Lifted Embedding ? This document doesn't say much. I expected it to be something like this Query(AbilitiesTable).filter((ab: AbilitiesTable.type) => ab.id === ability_id).map((ab: AbilitiesTable.type) => (ab.verb, ab.subject)).update("edit", "doc") 回答1: I figured it out. It should be like this val map = Query(AbilitiesTable) .filter(_.id === ability_id) .map(ab => ab.verb ~ ab.context) map.update(("", "")) Typesafe, why your documentation is so

Scala Constructor Parameters

折月煮酒 提交于 2019-12-17 19:57:02
问题 What is the difference between a private var constructor parameter and a constructor parameter without val/var? Are they same in terms of scope/visibility? Ex: class Person(private var firstName:String, lastName:String) 回答1: Yes, there are two important differences. First for the easy one: constructor parameters without the var or val keywords are not mutable variables—their values can't be changed in the body of the class. Even if we restrict ourselves to the val keyword, though, there's

Scala 2.10 reflection, how do I extract the field values from a case class, i.e. field list from case class

戏子无情 提交于 2019-12-17 10:26:47
问题 How can I extract the field values from a case class in scala using the new reflection model in scala 2.10? For example, using the below doesn't pull out the field methods def getMethods[T:TypeTag](t:T) = typeOf[T].members.collect { case m:MethodSymbol => m } I plan to pump them into for {field <- fields} { currentMirror.reflect(caseClass).reflectField(field).get } 回答1: MethodSymbol has an isCaseAccessor method that allows you to do precisely this: def getMethods[T: TypeTag] = typeOf[T]

How to get ClassTag form TypeTag, or both at same time?

我的未来我决定 提交于 2019-12-17 07:43:06
问题 I have some code like this: class ReflectiveJsonFormat[T:TypeTag] extends JsonFormat[T] { def write(x: T) : JsValue = { val t = typeOf[T] val getters = t.declarations.filter { s => s.isMethod && s.asMethod.isGetter } val mirror = runtimeMirror(this.getClass.getClassLoader) val instanceMiror = mirror.reflect(x) } } That last line fails with: No ClassTag available for T I thought TypeTag was more info than a ClassTag ? Can I get the ClassTag from the TypeTag ? If not, is there some syntax for

Finding type parameters via reflection in Scala 2.10?

谁都会走 提交于 2019-12-17 04:00:11
问题 Using type tags, I'm able to see the parameters of some type: scala> import scala.reflect.runtime.universe._ import scala.reflect.runtime.universe._ scala> typeOf[List[Int]] res0: reflect.runtime.universe.Type = List[Int] But I just can't quite figure out how to programmatically get that "Int" out of there, in a general way. (I've been wandering around in REPL for an hour now, trying permutations on Type, to see what I can obtain from it... I get a lot of things which indicate this is a "List

Generating a class from string and instantiating it in Scala 2.10

核能气质少年 提交于 2019-12-17 03:52:06
问题 In Scala 2.10 how do I generate a class from string (probably, using the Toolbox api) later to be instantiated with Scala's reflection? 回答1: W.r.t compilation toolboxes can only run expressions = return values, but not resulting classes or files/byte arrays with compilation results. However it's still possible to achieve what you want, since in Scala it's so easy to go from type level to value level using implicit values: Edit . In 2.10.0-RC1 some methods of ToolBox have been renamed.

Problems using scala to remotely issue commands via ssh

非 Y 不嫁゛ 提交于 2019-12-13 16:14:16
问题 I have a problem with scala when I want to create a directory remotely via ssh. ssh commands via scala, such as date or ls, work fine. However, when I run e.g "ssh user@Main.local 'mkdir Desktop/test'".! I get: bash: mkdir Desktop/test: No such file or directory res7: Int = 127 When I copy-paste the command into my shell it executes without any problems. Does anybody know what is going on?? EDIT: I found this post : sbt (Scala) via SSH results in command not found, but works if I do it myself

Why doesn't scala infer the type members of an inherited trait?

假如想象 提交于 2019-12-13 14:17:44
问题 I have a group of types that each have their own type member: sealed trait FieldType { type Data def parse(in: String): Option[Data] } object Name extends FieldType { type Data = String def parse(in: String) = Some(in) } object Age extends FieldType { type Data = Int def parse(in: String) = try { Some(in.toInt) } catch { case _ => None } } And I have a group of types that operate on sets of the FieldType s (using boilerplate rather than abstracting over arity): sealed trait Schema { type