scala

Why Kotlin,为什么你应该选择Kotlin ?

蓝咒 提交于 2021-02-06 01:28:38
为什么你应该选择Kotlin ? 编程语言设计是从满足机器需求到满足程序员需求的进化途径。 我们概述了编程语言的历史发展,以便您了解Kotlin适合的位置以及为什么要学习它。 这个原子介绍了一些主题,如果您是新手,现在可能看起来太复杂了。 在您阅读了更多本书之后,可以随时跳过此原子。 语言设计者发明了一种编程语言,并将其实现为 解释器 或 编译器 ,它们也是程序。 实施者通常至少在最初是语言设计者。 早期的语言着重于硬件限制。 随着计算机功能的增强,更新的语言将转向注重可靠性的更复杂的编程。 这些语言也可以根据编程的心理来开始选择功能。 每种编程语言都是实验的集合。 从历史上看,编程语言设计一直是关于使程序员提高生产率的一系列猜测和假设。 这些实验中有些失败,有些成功,有些非常成功。 我们从每种新语言的实验中学习。 一些语言解决的问题最终证明是偶然的,而不是根本的,或者环境发生了变化(更快的处理器,更便宜的内存,对编程和语言的新理解),并且该问题变得不那么重要,甚至变得无关紧要了。 如果这些想法过时并且语言没有发展,它就会从使用中消失。 最初的程序员直接使用代表处理器机器指令的数字进行工作。 这种方法产生了许多错误,并且 创建了 汇编语言 ,以助记符 操作码 替换数字( 程序员可以更轻松地记住和阅读的 单词 以及许多其他有用的工具)。 但是

Scala Parser Combinators tricks for recursive bnf?

送分小仙女□ 提交于 2021-02-05 20:36:41
问题 Im trying to match this syntax: pgm ::= exprs exprs ::= expr [; exprs] expr ::= ID | expr . [0-9]+ My scala packrat parser combinator looks like this: import scala.util.parsing.combinator.PackratParsers import scala.util.parsing.combinator.syntactical._ object Dotter extends StandardTokenParsers with PackratParsers { lexical.delimiters ++= List(".",";") def pgm = repsep(expr,";") def expr :Parser[Any]= ident | expr~"."~num def num = numericLit def parse(input: String) = phrase(pgm)(new

Typesafe config: Load additional config from path external to packaged scala application

南楼画角 提交于 2021-02-05 12:59:08
问题 My scala application will be packaged into a jar. When I run my app, it needs to read an additional config file stored externally to my app jar. I am looking for functionality similar to the Typesafe Config library but other solutions are welcome too ! Is there a way to do something like below: val hdfsConfig = ConfigFactory.load("my_path/hdfs.conf") 回答1: I think what you want is: val myCfg = ConfigFactory.parseFile(new File("my_path/hdfs.conf")) 回答2: If your external configuration is to add

Defining a function with multiple implicit arguments in Scala

谁说我不能喝 提交于 2021-02-05 12:55:09
问题 How can I define a function with multiple implicit arguments. def myfun(arg:String)(implicit p1: String)(implicit p2:Int)={} // doesn't work 回答1: They must all go in one parameter list, and this list must be the last one. def myfun(arg:String)(implicit p1: String, p2:Int)={} 回答2: There actually is a way of doing exactly what the OP requires. A little convoluted, but it works. class MyFunPart2(arg: String, /*Not implicit!*/ p1: String) { def apply(implicit p2: Int) = { println(arg+p1+p2) /*

Defining a function with multiple implicit arguments in Scala

时间秒杀一切 提交于 2021-02-05 12:55:08
问题 How can I define a function with multiple implicit arguments. def myfun(arg:String)(implicit p1: String)(implicit p2:Int)={} // doesn't work 回答1: They must all go in one parameter list, and this list must be the last one. def myfun(arg:String)(implicit p1: String, p2:Int)={} 回答2: There actually is a way of doing exactly what the OP requires. A little convoluted, but it works. class MyFunPart2(arg: String, /*Not implicit!*/ p1: String) { def apply(implicit p2: Int) = { println(arg+p1+p2) /*

How to implicitly figure out the type at the head of a shapeless HList

空扰寡人 提交于 2021-02-05 12:04:49
问题 Lets say I have the following: case class TestField(value: String) case class TestField2(value: String) implicit class ProductExtensions[T <: Product](val value T) extends AnyVal { def mapTo[R <: Product](implicit tGen: Generic.Aux[T, String :: HNil], rGen: Generic.Aux[R, String :: HNil]: R = ??? } val testField2 = TestField("my value").mapTo[TestField2] // TestField2("my value") Can I "genersize" the mapTo function to work for types other than String without having to specify the type? Note

How to implicitly figure out the type at the head of a shapeless HList

佐手、 提交于 2021-02-05 12:03:40
问题 Lets say I have the following: case class TestField(value: String) case class TestField2(value: String) implicit class ProductExtensions[T <: Product](val value T) extends AnyVal { def mapTo[R <: Product](implicit tGen: Generic.Aux[T, String :: HNil], rGen: Generic.Aux[R, String :: HNil]: R = ??? } val testField2 = TestField("my value").mapTo[TestField2] // TestField2("my value") Can I "genersize" the mapTo function to work for types other than String without having to specify the type? Note

Java 8 LocalDate.plusMonths is adding to days and months

喜夏-厌秋 提交于 2021-02-05 11:39:21
问题 I'm getting strange formatting issue when adding months to a LocalDate. Here is the Scala code and output: val virtualToday: LocalDate = LocalDate.parse("2015-01-01") val eightDaysFromToday: LocalDate = virtualToday.plusDays(8) val sixMonthsFromToday: LocalDate = virtualToday.plusMonths(6) println("virtualToday " + virtualToday) println("eightDaysFromToday " + eightDaysFromToday) println("sixMonthsFromToday " + sixMonthsFromToday) println( "virtualToday with formatting " + virtualToday

T <: A, return T method

大憨熊 提交于 2021-02-05 11:33:58
问题 here is some sample code: trait A trait B extends A def test[T <: A](): T = { new B {} } but I get an compile error: type mismatch; found : B required: T new B {} how to make it working ? ( but without doing asInstanceOf[T] at the end ) thanks! 回答1: The signature of your method def test[T <: A](): T promises that for any type T that is a subtype of A you return a value of this type T . And then you returned a value of type B . You violated the signature (there are many subtypes of A , not

T <: A, return T method

痞子三分冷 提交于 2021-02-05 11:33:05
问题 here is some sample code: trait A trait B extends A def test[T <: A](): T = { new B {} } but I get an compile error: type mismatch; found : B required: T new B {} how to make it working ? ( but without doing asInstanceOf[T] at the end ) thanks! 回答1: The signature of your method def test[T <: A](): T promises that for any type T that is a subtype of A you return a value of this type T . And then you returned a value of type B . You violated the signature (there are many subtypes of A , not