借助Play框架的JSON库,我怎么可以创建一个Reads和Writes对于没有领域一个Java枚举?
public enum EnumNoFields {
RED,
WHITE,
BLUE
}
implicit val EnumNoFieldsReads: Reads[EnumNoFields] = ?
implicit val EnumNoFieldsWrites: Writes[EnumNoFields] = ?
Run Code Online (Sandbox Code Playgroud) object Test {
def main(args: Array[String]) {
val list: List[Double] = List(1.0, 2.0, 3.0, 4.0)
val none = None
case class Test()
val test = Test()
def f(x: Any) = x match {
case _: Some[Test] => println("_ matched")
case None => println("None matched")
}
f(list)
f(none)
f(test)
}
}
Run Code Online (Sandbox Code Playgroud)
尝试编译上面的代码会导致"擦除"编译时警告.
$>scalac Test.scala
Test.scala:11: warning: non-variable type argument Test in type pattern
Some[Test] is unchecked since it is eliminated by erasure
case _: Some[Test] => println("_ matched")
^
one warning …Run Code Online (Sandbox Code Playgroud) 我打电话tail给Array,但看到了一个警告.
scala> val arr = Array(1,2)
arr: Array[Int] = Array(1, 2)
scala> arr tail
warning: there were 1 feature warning(s); re-run with -feature for details
res3: Array[Int] = Array(2)
Run Code Online (Sandbox Code Playgroud)
Scaladocs for Array显示了一个UnsupportedOperationException [will be thrown]
if the mutable indexed sequence is empty.
是否存在安全,不会抛出异常,获取数组尾部的方法?
我Named Arguments在Scala中深入研究这个例子:
scala> class Parent {
| def foo(bar: Int = 1, baz: Int = 2): Int = bar + baz
| }
defined class Parent
scala> class Child extends Parent {
| override def foo(baz: Int = 3, bar: Int = 4): Int = super.foo(baz, bar)
| }
defined class Child
scala> val p = new Parent
p: Parent = Parent@6100756c
scala> p.foo()
res1: Int = 3
scala> val x = new Child
x: Child …Run Code Online (Sandbox Code Playgroud) 看一下Learn You a Haskell的一个kind例子:
data Frank a b = Frank {frankField :: b a} deriving (Show)
LYAH讨论:
我们看到弗兰克有一种* - >(* - >*) - >*第一个*代表a而(* - >*)代表b.
当我想到一个例子时,这对我来说很有意义:Frank String Maybe = Frank {frankField :: Maybe String}.
但我对如何思考kind右手方面感到困惑
... = Frank {frankField :: b a}.
由于返回类型frankField :: b a,怎么能kind的Frank为:
* -> (* -> *) -> * -- what does the right-most * represent?
为什么右手边kind等于* …
鉴于List[Option[Int]]:
scala> list
res8: List[Option[Int]] = List(Some(1), Some(2), None)
Run Code Online (Sandbox Code Playgroud)
我可以得到List(1,2),即提取list通道flatMap和flatten:
scala> list.flatten
res9: List[Int] = List(1, 2)
scala> list.flatMap(x => x)
res10: List[Int] = List(1, 2)
Run Code Online (Sandbox Code Playgroud)
鉴于[Maybe Int]Haskell中的以下内容,我该如何执行上述操作?
我尝试了下面的失败:
import Control.Monad
maybeToList :: Maybe a -> [b]
maybeToList Just x = [x]
maybeToList Nothing = []
flatten' :: [Maybe a] -> [a]
flatten' xs = xs >>= (\y -> y >>= maybeToList)
Run Code Online (Sandbox Code Playgroud) 我正在使用Node-Postgres客户端连接我的 AWS Redshift 数据库。
在本地,我可以在 中运行以下代码node,获取“>>已连接”和“>>>>成功查询的打印语句。jsonResult:”。
但是,当我在 Amazon Lambda 中运行此代码时,除了“尝试连接...”之外,我没有看到任何日志语句。
console.log("trying to connect...");
var r = pg.connect(conString, function(err, client) {
if(err) {
return console.log('>> could not connect to redshift', err);
}
console.log(">> connected");
client.query('SELECT * FROM my_table', function(err, result) {
if(err) {
return console.log('error running query', err);
}
var jsonResult = JSON.stringify( result );
console.log(">>> successful query. jsonResult: " + jsonResult);
client.end();
return jsonResult;
});
});
Run Code Online (Sandbox Code Playgroud)
我很困惑除了“>>尝试连接...”之外如何没有打印语句出现在这段代码中。
鉴于以下内容:
> mvn clean compile
...
[ERROR] Failure executing javac, but could not parse the error:
/bin/sh: c:/progra~1/java/jdk1.6.0_24/bin/javac: No such file or directory
Run Code Online (Sandbox Code Playgroud)
检查我mvn和java版本是否正确:
$mvn -version
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T07:57:37-04:00)
Maven home: /Users/kevin/Downloads/apache-maven-3.3.3
Java version: 1.8.0_51, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.10.4", arch: "x86_64", family: "mac"
$java -version
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM …Run Code Online (Sandbox Code Playgroud) 鉴于以下特征:
scala> trait Foo { self =>
| def f: String = self.getClass.getName
| }
defined trait Foo
scala> trait Bar {
| def f: String = this.getClass.getName
| }
defined trait Bar
Run Code Online (Sandbox Code Playgroud)
然后创建扩展它们的类:
scala> class FooImpl extends Foo {}
defined class FooImpl
scala> class BarImpl extends Bar {}
defined class BarImpl
Run Code Online (Sandbox Code Playgroud)
然后f在新实例上调用它们的方法:
scala> (new FooImpl).f
res1: String = FooImpl
scala> (new BarImpl).f
res4: String = BarImpl
Run Code Online (Sandbox Code Playgroud)
REPL显示它们打印出相同的值 - 类的名称.
也许这不是一个好例子.但是,什么是使用的差异self在上面Foo相比Bar,它使用this …
给出以下AST Success和Failure:
sealed trait Success
case object FooGood extends Success
case object BarGood extends Success
sealed trait Failure
case object FooBad extends Failure
case object BarBad extends Failure
Run Code Online (Sandbox Code Playgroud)
方法签名:
def go[A <: Failure, B <: Success](x: Int): Either[A, B] = ???
Run Code Online (Sandbox Code Playgroud)
但是,我想约束Left和Right类型特定于Foo或Bar.
但是下面的代码编译(违背我的意愿):
scala> go[FooBad.type, BarGood.type](5)
scala.NotImplementedError: an implementation is missing
Run Code Online (Sandbox Code Playgroud)
如何在编译时实现此约束?