与我之前的问题相同的想法,只是更高级一点。我想用 Java 编写以下 Scala 代码:
Option(System.getProperty("not.present")).orElse(Some("default")).get
Run Code Online (Sandbox Code Playgroud)
我试过这个:
final Function0<Option<String>> elseOption = new Function0<Option<String>>() {
@Override public Option<String> apply() {
return new Some<String>("default");
}
};
final Option<String> notPresent =
new Some<String>(System.getProperty("not.present")).
orElse(elseOption).get();
Run Code Online (Sandbox Code Playgroud)
但我得到:
<anonymous OptionDemo$1> is not abstract and does not override abstract method apply$mcD$sp() in scala.Function0
[error] orElse(new Function0<Option<String>>() {
Run Code Online (Sandbox Code Playgroud) 我想使用与生产中使用的相同的数据库设置为Play 2 Scala应用程序运行单元测试:与Postgres一起使用Slick.以下操作失败,出现"java.sql.SQLException:尝试从已关闭的池中获取连接".在第二次测试.
package controllers
import org.specs2.mutable._
import play.api.db.DB
import play.api.Play.current
import play.api.test._
import play.api.test.Helpers._
import scala.slick.driver.PostgresDriver.simple._
class BogusTest extends Specification {
def postgresDatabase(name: String = "default",
options: Map[String, String] = Map.empty): Map[String, String] =
Map(
"db.test.driver" -> "org.postgresql.Driver",
"db.test.user" -> "postgres",
"db.test.password" -> "blah",
"db.test.url" -> "jdbc:postgresql://localhost/blah"
)
def fakeApp[T](block: => T): T =
running(FakeApplication(additionalConfiguration =
postgresDatabase("test") ++ Map("evolutionplugin" -> "disabled"))) {
def database = Database.forDataSource(DB.getDataSource("test"))
database.withSession { implicit s: Session => block }
}
"Fire 1" should { …Run Code Online (Sandbox Code Playgroud) 我的哈巴狗 mixintweet通常只生成这个 HTML:
<div class='col-md-3'></div>
Run Code Online (Sandbox Code Playgroud)
我传递tweet了参数index,它是一个从零开始的正数。当index等于tweetData.index(在别处定义)时,我希望生成div的发光,如下所示:
<div class='blueGlow col-md-3'></div>
Run Code Online (Sandbox Code Playgroud)
这是我的尝试:
mixin tweet(index)
div.collapse(class= tweetData.index === index ? "blueGlow" : undefined).col-md-3(data-index=index)
Run Code Online (Sandbox Code Playgroud)
错误信息是: You should not have pug tags with multiple attributes.
Java程序如何将值包装到scala.Either?例如,下面的Scala代码将如何用Java编写?
Right("asdf")
Left(new Exception())
Run Code Online (Sandbox Code Playgroud)
以下失败,"无法找到符号方法apply(java.lang.String)"
Right.apply("asdf");
Run Code Online (Sandbox Code Playgroud)
以下失败,"无法找到符号方法apply(java.lang.Exception)"
Left.apply(new Exception());
Run Code Online (Sandbox Code Playgroud) 我想显示一个HTML复选框,然后是右边的标签.
我跟着佩尔Villega的建议,并修改的播放2.1快照版本views.helper.checkbox.scala的views.helper.twitterCheckbox.scala.我的项目使用Webjars提供的Twitter Bootstrap版本:
@**
* Generate an HTML input checkbox for Twitter Bootstrap.
*
* Example:
* {{{
* @checkbox(field = myForm("done"))
* }}}
*
* @param field The form field.
* @param args Set of extra HTML attributes ('''id''' and '''label''' are 2 special arguments).
* @param handler The field constructor.
*@
@(field: play.api.data.Field, args: (Symbol,Any)*)(implicit handler: FieldConstructor, lang: play.api.i18n.Lang)
@boxValue = @{ args.toMap.get('value).getOrElse("true") }
@input(field, args:_*) { (id, name, value, …Run Code Online (Sandbox Code Playgroud) 我想要一个dict(或OrderedDict)来自ruamel.yaml. 我正在使用 Python 3.8,如果有帮助的话,很乐意切换到 3.9。
from ruamel.yaml import YAML
from collections import OrderedDict
with open(self.filename) as f:
data = yaml.load(f)
print(type(data)) # <class 'ruamel.yaml.comments.CommentedMapItemsView'>
data = dict(yaml.load(f))
print(type(data)) # <class 'dict_items'>
data = yaml.load(f).items() # AttributeError: 'NoneType' object has no attribute 'items'
Run Code Online (Sandbox Code Playgroud) 我看到其他人也有类似的问题,但我还没有找到解决方案。
\n我按照以下说明nvm在 Ubuntu / WSL2上执行了以下安装:node
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash\n\nmkdir ~/.npm-global\n\nnpm config set prefix ~/.npm-global\n\ncat >> ~/.bashrc <<EOF\nexport PATH="$HOME/.npm-global/bin:$PATH"\nexport NVM_DIR="$HOME/.nvm"\n[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" 1>&2 # Loads nvm\nEOF\n\nnvm install node\nRun Code Online (Sandbox Code Playgroud)\n$ which npm\n/home/mslinn/.nvm/versions/node/v17.6.0/bin/npm\n\n$ cat ~/.npmrc\nfund=false\nprefix=/home/mslinn/.npm-global\nRun Code Online (Sandbox Code Playgroud)\n$ source ~/.bashrc\nYour user\xe2\x80\x99s .npmrc file (${HOME}/.npmrc)\nhas a `globalconfig` and/or a `prefix` setting, which are incompatible with nvm.\nRun `nvm use --delete-prefix v17.6.0 --silent` to unset it.\nRun Code Online (Sandbox Code Playgroud)\n删除该prefix …
这可以很好地编译,但是在运行时会由于以下原因而爆炸:
线程“主”中的异常java.lang.NoSuchMethodError:scala.collection.immutable.List.filter(Lscala / Function1;)Lscala / collection / immutable / List
import scala.Function1;
import scala.collection.immutable.List;
import scala.collection.immutable.Nil$;
import scala.runtime.AbstractFunction1;
public class FunProc {
List nil = Nil$.MODULE$; // the empty list
List<Integer> list1 = nil.$colon$colon(1); // append 1 to the empty list
List<Integer> list2 = list1.$colon$colon(2); // append 2 to List(1)
List<Integer> list3 = list2.$colon$colon(3).$colon$colon(14).$colon$colon(8); // List(8, 14, 3, 2, 1)
Function1<Integer, Object> filterFn = new AbstractFunction1<Integer, Object>() {
public Boolean apply(Integer value) { return value<10; }
};
List<Integer> list4 = …Run Code Online (Sandbox Code Playgroud) 使用Jerkson,我能够解析包含JSON数组的String,如下所示:
com.codahale.jerkson.Json.parse[Array[Credentials]](contents)
Run Code Online (Sandbox Code Playgroud)
其中contents是包含以下内容的String:
[{"awsAccountName":"mslinn","accessKey":"blahblah","secretKey":"blahblah"}]
Run Code Online (Sandbox Code Playgroud)
......我会得到一系列证书.
(简短转移)我尝试使用新的JSON解析器为Play 2.1和Scala使用不同的数据做类似的事情.对于简单的解析,以下工作正常.案例类(S3File)定义了此工作所需的unapply方法:
case class S3File(accountName: String,
bucketName: String,
endpoint: String = ".s3.amazonaws.com")
implicit val s3FileFormat = Json.format[S3File]
val jsValue = Json.parse(stringContainingJson)
Json.fromJson(jsValue).get
Run Code Online (Sandbox Code Playgroud)
让我们重新考虑一下名为包含JSON的内容的原始字符串.与所有集合一样,对象数组没有unapply方法.这意味着我在上面的转移中展示的技术将无效.我试图为此目的创建一次性案例类:
case class ArrayCreds(payload: Array[Credentials])
implicit val credsFormat = Json.format[ArrayCreds]
val jsValue = Json.parse(contents)
val credArray = Json.fromJson(jsValue).get.payload
Run Code Online (Sandbox Code Playgroud)
......不幸的是,这失败了:
No unapply function found
[error] implicit val credsFormat = Json.format[ArrayCreds]
[error] ^
[error]
/blah.scala:177: diverging implicit expansion for type play.api.libs.json.Reads[T]
[error] starting with method ArrayReads in trait DefaultReads
[error] val credArray = Json.fromJson(jsValue).get
[error] …Run Code Online (Sandbox Code Playgroud) $ cat /etc/apt/sources.list
## Note, this file is written by cloud-init on first boot of an instance
## modifications made here will not survive a re-bundle.
## if you wish to make changes you can:
## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg
## or do the same in user-data
## b.) add sources in /etc/apt/sources.list.d
## c.) make changes to template file /etc/cloud/templates/sources.list.tmpl
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb https://old-releases.ubuntu.com/releases/ focal …Run Code Online (Sandbox Code Playgroud) scala ×5
java ×2
apt ×1
json ×1
npm ×1
nvm ×1
pug ×1
python ×1
python-3.x ×1
ruamel.yaml ×1
slick ×1
specs2 ×1
ubuntu-19.04 ×1