我有非常通用的用例.我有一个conditionMethod返回Int 的方法
def conditionMethod(..):Int = {..}
Run Code Online (Sandbox Code Playgroud)
现在我有条件使用相同的方法
if (conditionMethod(..) > 0){
conditionMethod(..) + 23 // Any action
}
Run Code Online (Sandbox Code Playgroud)
问题是它调用方法conditionMethod两次.为了解决这个问题,另一种方法是
val tmp = conditionMethod(..)
if (tmp > 0){
tmp + 23 // Any action
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢的是我必须定义一个范围更大的变量.
我能做点什么吗
if ((val tmp = conditionMethod(..)) > 0){ // tmp variable in if condition itself
tmp + 23 // Any action
}
Run Code Online (Sandbox Code Playgroud)
Scala版本:2.11
MySQL(Innodb)使用倒排索引还是正向索引?
来自文章What's the difference between an inverted index and a plain old index?
,我的理解是,每当我从键(如字符串、int)获取记录时,它就是反向索引。这样取“倒排索引”意味着mySQL使用倒排索引。
那么为什么 mySQL 使用术语“索引”来代替“倒排索引”呢?
我有两个案例类Person和Employee
case class Person(identifier: String) {}
case class Employee (salary: Long) extends Person {}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Unspecified value parameters: identifier: String
Error: case class Employee has case ancestor Person, but case-to-case inheritance is prohibited. To overcome this limitation, use extractors to pattern match on non-leaf nodes
Run Code Online (Sandbox Code Playgroud)
我是 Scala 新手,无法理解我必须做什么。
版本:斯卡拉:2.11
我得到以下异常跟踪,但无法确定其根本原因:
WARN HttpChannel: //ip-10-0-1-137.ec2.internal:4040/api/v1/applications/application_1565230812086_0001/allexecutors?proxyapproved=true
javax.servlet.ServletException: java.lang.IndexOutOfBoundsException: 53
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:489)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
...
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IndexOutOfBoundsException: 53
at scala.collection.LinearSeqOptimized$class.apply(LinearSeqOptimized.scala:65)
at scala.collection.immutable.Stream.apply(Stream.scala:202)
...
Run Code Online (Sandbox Code Playgroud)
虽然这不是终止执行,但还是想知道它的原因。这会导致火花机性能低下吗?
版本:
Scala: 2.11
Spark: 2.2.1
Run Code Online (Sandbox Code Playgroud) 我使用泛型创建了接口和类的层次结构,并搞砸了所有内容.
最顶级的类是AbstractJpaEntity,它由所有域实体扩展
@MappedSuperclass
@EntityListeners({AbstractJpaEntity.AbstractEntityListener.class})
@SuppressWarnings("serial")
public class AbstractJpaEntity implements Serializable
Run Code Online (Sandbox Code Playgroud)
ProductTypeDomain类就像分隔出几个表实体的标记类一样.
@SuppressWarnings("serial")
@MappedSuperclass
@EntityListeners({ ProductTypeDomain.AbstractEntityListener.class })
public class ProductTypeDomain extends AbstractJpaEntity{}
Run Code Online (Sandbox Code Playgroud)
接口"GenericDao"定义
public interface GenericDao<T> {...
Run Code Online (Sandbox Code Playgroud)
抽象类GenericDaoImpl(此类具有通用函数,如persist,merge)
public abstract class GenericDaoImpl<T extends AbstractJpaEntity> implements GenericDao<T> {...
Run Code Online (Sandbox Code Playgroud)
接口ProductTypeDao
public interface ProductTypeDao<T extends ProductTypeDomain> extends GenericDao<T> {
Run Code Online (Sandbox Code Playgroud)
Spring存储库类ProductTypeDaoImpl
@Repository("productTypeDao")
public class ProductTypeDaoImpl extends GenericDaoImpl implements ProductTypeDao
{....
Run Code Online (Sandbox Code Playgroud)
在Spring服务类ProductManagerServiceimpl中,我是自动装配存储库productTypeDao
@Service("productManager")
public class ProductManagerServiceimpl implements ProductManagerService{
@Autowired
ProductTypeDao productTypeDao;
Run Code Online (Sandbox Code Playgroud)
在运行代码时,它给了我以下错误
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.jodo.cms.service.ProductManagerService com.jodo.cms.controllers.ProductController.productManager; nested exception is org.springframework.beans.factory.BeanCreationException: Error …
我有一个hashmap<String, String>包含大约一千个条目.现在我必须以不能在课外修改的方式公开它.所以我写得像
public static Map<String, String> getResponseCodeSource()
{
return Collections.unmodifiableMap(codeMsgMap);
}
Run Code Online (Sandbox Code Playgroud)
非常频繁地调用此方法.我的问题是
1.这是否会导致性能问题?
2.迭代Map的方法(unmodifiableMap)是否会以O(常量)复杂度执行其活动?
我以这种方式存储错误代码及其字符串消息:
object Error {
def FileNotFound(filename: String) = Error("ERR01", s"${filename} not found")
def UserNotExist(userName: String) = Error("ERR02", s"${userName} not exist")
}
case class Error(code: String, value: String) {}
Run Code Online (Sandbox Code Playgroud)
保持这样的好处是我可以将字符串值传递给错误消息。
我正在创造它
def validate(data: SomeType): List[Error] = {
var errors = ListBuffer[Error]()
if (validation1("abc") == false) {
errors+= Error.FileNotFound("abc")
}
if (validation2("lmn") == false) {
errors+= Error.UserNotExist("lmn")
}
errors.toList
}
Run Code Online (Sandbox Code Playgroud)
我是 Scala 和函数式编程的新手。像这样编写错误代码是正确的方法吗?它是否遵循函数式编程范式?
斯卡拉:2.11
我正在尝试创建一个非常简单的Web应用程序,基本上是为了理解HTML5,CSS和JavaScript编码的最佳实践.
我的应用程序有3-4页,每个页面使用相同的菜单标题.因此,我希望通过将其写入单独的文件(PHP或HTML)来使其可重用.
head.php(它可以重复使用):
<!DOCTYPE html>
<html>
<head>
<link href="../../css/headermenu.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<ul id="menu">
<li><a href="#" class="home">Home<span></span></a></li>
</ul>
<p>
</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
front.php:
<?php
include ("$_SERVER[DOCUMENT_ROOT]/page/common/head.php");
?>
Run Code Online (Sandbox Code Playgroud)
HTML标记(脏代码):
<!DOCTYPE html>
<html>
<head>
<!DOCTYPE html>
<html>
<head>
<link href="../../css/headermenu.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<ul id="menu">
<li><a href="#" class="home">Home<span></span></a></li>
</ul>
<p>
</p>
</body>
</html></head>
<body>
<div>
</div>
<p>
</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我有以下问题:
head.php有<body>和<head>标签.那么我应该在哪里编写这些PHP行来包含它?(in <head>或<body>)(我不想在最后一页中有多个<head>s和<body>s)
我应该遵循的其他最佳做法是什么?(欢迎参考的任何链接)
我已经读过w3schools了.
我有三节课
class C {
var id: String = _
}
class B {
var c: List[C] = _
}
class A {
var b: List[B] = _
}
Run Code Online (Sandbox Code Playgroud)
我想收集类"C"实例的所有"id",它们位于类"A"实例中
val c1 = new C
c1.id = "data1"
val c2 = new C
c2.id = "data2"
val b = new B
b.c = c1::c2::Nil
val a = new A
a.b = b::Nil
Run Code Online (Sandbox Code Playgroud)
这个示例代码的预期输出是具有两个元素的List [String](即data1,data2)在命令式编程中,我已经用下面的代码片段实现了相同的
def collectCId(a: A): List[String] = {
var collect = List[String]()
for(tmpb <- a.b){
for(tmpc <- tmpb.c){
collect …Run Code Online (Sandbox Code Playgroud) 我想尽可能地使用不可变的数据结构。在我们的代码库中非常普遍的情况是使用mutable.LinkedHashMap。我想在所有非性能关键代码中用immutable.ListMap替换它。
我们的代码具有的通用格式
示例代码:
def dummyFunction(): mutable.LinkedHashMap[Int, String] = {
val tmpListMap: mutable.LinkedHashMap[Int, String] = mutable.LinkedHashMap()
for (i <- 1 to 10) {
if (i%2 ==0) tmpListMap += (i -> "even")
else tmpListMap += (i -> "odd")
}
tmpListMap
}
Run Code Online (Sandbox Code Playgroud)
我想在所有这些地方使用 ListMap。我不想再次循环 tmpListMap 来创建 ListMap。
斯卡拉版本:2.11