我只看了List.scala的实现foldRight().
override def reverse: List[A] = {
var result: List[A] = Nil
var these = this
while (!these.isEmpty) {
result = these.head :: result
these = these.tail
}
result
}
override def foldRight[B](z: B)(op: (A, B) => B): B =
reverse.foldLeft(z)((right, left) => op(left, right))
Run Code Online (Sandbox Code Playgroud)
据我了解,呼吁foldRight对List结果调用theList.reverse.foldLeft(...).
是List.foldRight与实施foldLeft以便利用一个单一的堆栈帧,而不是使用多个堆栈帧与foldLeft?
在创建Amazon S3 Bucket之后my_bucket,我通过cli创建了一个Elastic Map Reduce集群:
aws emr create-cluster --name"Hive testing"--ami-version 3.3 --applications Name = Hive --use-default-roles --instance-type m3.xlarge --instance-count 3 --steps Type = Hive,Name ="Hive Program",Args = [-d,INPUT = s3://my_bucket/input,-d.OUTPUT=s3:// my_bucket/input,-d-LIBS = s3:// my_bucket/serde_libs ]
请注意,我没有指定hive *.q文件.制作S3和EMR群集后,我将登录EMR框,然后以hive交互方式运行.
注意 - 我假设有一个可以记录的EMR盒子.
但是,当我运行时aws emr describe-cluster --cluster-id XYZ,我在输出中看到了这个错误:
"State": "TERMINATED_WITH_ERRORS",
"StateChangeReason": {
"Message": "EMR service role arn:aws:iam::xyz:role/EMR_DefaultRole
is invalid",
"Code": "VALIDATION_ERROR"
}
Run Code Online (Sandbox Code Playgroud)
什么会导致这个错误?我是否需要在S3存储桶上打开权限才能让EMR集群访问它?
在ghci,我跑了:
ghci> :t Right 5
Right 5 :: Num b => Either a b
Run Code Online (Sandbox Code Playgroud)
这是什么意思a?
它与Scala的版本相比如何?
scala> Right(5)
res0: scala.util.Right[Nothing,Int] = Right(5)
Run Code Online (Sandbox Code Playgroud) 通读这篇文章,我无法弄清楚如何将我转换Some(JsValue)为String.
例:
val maybeString: Option[JsValue] = getSomeJsValue(); // returns Some(JsValue)
val str: String = maybeString match {
case Some(x) => x.as[String]
case _ => "0"
}
Run Code Online (Sandbox Code Playgroud)
运行时错误:
play.api.Application$$anon$1: Execution exception[[JsResultException: JsResultException(errors:List((,List(ValidationErr
or(validate.error.expected.jsstring,WrappedArray())))))]]
at play.api.Application$class.handleError(Application.scala:289) ~[play_2.10.jar:2.1.3]
Run Code Online (Sandbox Code Playgroud) 我试图java用测试厨房覆盖食谱中的属性.
当我尝试运行时kitchen converge default-centos-64,会出现错误的YAML错误.
---
driver:
name: vagrant
customize:
memory: 1024
cpuexecutioncap: 50
provisioner:
name: chef_solo
platforms:
- name: centos-6.4
suites:
- name: default
run_list:
- recipe[java::default]
- recipe[maven::default]
attributes: {
java.install_flavor: "oracle",
java.jdk_version: "7"
}
Run Code Online (Sandbox Code Playgroud)
我将上述内容粘贴到http://yamllint.com/.当我点击"Go"时,它会删除所有以"attributes"开头的行,然后显示绿色的"Valid YAML".
Haskell 提供类型化的漏洞。
例子:
f :: Int -> String -> Bool
f x y = if (x > 10) then True else (g y)
g :: String -> Bool
g s = _
Run Code Online (Sandbox Code Playgroud)
汇编:
Prelude> :l HoleEx.hs
[1 of 1] Compiling Main ( HoleEx.hs, interpreted )
HoleEx.hs:6:7:
Found hole `_' with type: Bool
Relevant bindings include
s :: String (bound at HoleEx.hs:6:3)
g :: String -> Bool (bound at HoleEx.hs:6:1)
In the expression: _
In an equation for `g': …Run Code Online (Sandbox Code Playgroud) 鉴于:
case class Foo(a: Option[Int], b: Option[Int], c: Option[Int], d: Option[Int])
Run Code Online (Sandbox Code Playgroud)
我想只允许构造一个Foo 只有至少一个参数Some,即不是所有字段都是None.
编写代数数据类型,然后为每个变体创建子类将是相当多的代码:
sealed trait Foo
case class HasAOnly(a: Int) extends Foo
case class HasAB(a: Int, b: Int) extends Foo
// etc...
Run Code Online (Sandbox Code Playgroud)
是否有更清洁,即更少的代码,以解决我的问题使用shapeless?
Hibernate 文档显示了这个例子:
session = sessionFactory.openSession();
session.beginTransaction();
List result = session.createQuery( "from Event" ).list();
for ( Event event : (List<Event>) result ) {
System.out.println( "Event (" + event.getDate() + ") : " +
event.getTitle() );
}
session.getTransaction().commit();
session.close();
Run Code Online (Sandbox Code Playgroud)
为什么有必要执行session.getTransaction().commit()即使事件列表只是打印出来?
在以下代码中(来自Scala中的Functional Programming):
trait Functor[F[_]] {
def map[A,B](fa: F[A])(f: A => B): F[B]
}
trait Monad[F[_]] {
def unit[A](a: => A): F[A]
def flatMap[A,B](ma: F[A])(f: A => F[B]): F[B]
def apply[A](a: => A): F[A]
}
Run Code Online (Sandbox Code Playgroud)
我看到以下警告:
[warn] C:\...\Monad.scala:3: higher-kinded type should be enabled
[warn] by making the implicit value scala.language.higherKinds visible.
[warn] This can be achieved by adding the import clause 'import scala.language.higherKinds'
[warn] or by setting the compiler option -language:higherKinds.
[warn] See the Scala docs for value scala.language.higherKinds …Run Code Online (Sandbox Code Playgroud) 假设我有以下内容newtype:
newtype Foo = Foo Integer deriving (Eq, Show)
有没有简洁的方法来添加两个Foo:
(Foo 10) + (Foo 5) == Foo 15
或获得最大值:
max (Foo 10) (Foo 5) == Foo 5?
我很好奇是否可以轻松使用afor的功能newtype a而不是:
addFoo :: Foo -> Foo -> Foo
addFoo (Foo x) (Foo y) = Foo $ x + y
Run Code Online (Sandbox Code Playgroud)