我试图在Kotlin中填充一个字符串,以在控制台输出上实现一些正确的对齐.这些方面的东西:
accountsLoopQuery - "$.contactPoints.contactPoints[?(@.contactAccount.id)]"
brokerPassword - *****
brokerURI - tcp://localhost:61616
brokerUsername - admin
contactPointPriorityProperties - "contactPointPriority.properties"
customerCollection - "customer"
customerHistoryCollection - "customer_history"
defaultSystemOwner - "TUIGROUP"
Run Code Online (Sandbox Code Playgroud)
我最终用这种方式编写代码 - 用Java的String.format作弊:
mutableList.forEach { cp ->
println(String.format("%-45s - %s", cp.name, cp.value))
}
Run Code Online (Sandbox Code Playgroud)
有没有一个正确的方法来与Kotlin库这样做?
考虑这个表达式的用法:
String hi = Optional.ofNullable(sayHi()).orElse("-");
Run Code Online (Sandbox Code Playgroud)
这有效地对应于这个三元表达式:
String hi = sayHi() != null ? sayHi() : "-";
Run Code Online (Sandbox Code Playgroud)
使用Optional.ofNullable方法调用是一种好习惯吗?或者只是额外的详细编码?
我认识到Optional.ofNullable实际上创建了一个变量并避免了sayHi()两次调用该方法.为了避免这个问题,你实际上可以创建一个额外的变量,但这会增加三元选项的详细程度:
String hi = sayHi();
hi = hi != null ? hi : "-";
Run Code Online (Sandbox Code Playgroud)
另一方面,Optional.ofNullable在hi不是null额外Optional对象的情况下创建.所以肯定会有更多的开销.
因此,使用这种类型的构造来替换三元构造函数似乎有一些利弊.
顺便说一下:这是Java 8的实现Optional.ofNullable:
public static <T> Optional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
}
Run Code Online (Sandbox Code Playgroud) 我想知道,作为一个新手Kotlin编码器,是否有一些良好的实践甚至语言结构来声明函数的前置条件.
在Java中,我一直在使用Guava的Preconditions检查实用程序:
https://github.com/google/guava/wiki/PreconditionsExplained
经过一些进一步的调查后,我遇到了require函数:
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/require.html
这通常用于检查函数的前提条件吗?
在 Pytorch 1.0 中似乎有两种使用均匀分布初始化嵌入层的方法。
例如,您有一个嵌入层:
self.in_embed = nn.Embedding(n_vocab, n_embed)
Run Code Online (Sandbox Code Playgroud)
并且您想用均匀分布初始化其权重。您可以完成此操作的第一种方法是:
self.in_embed.weight.data.uniform_(-1, 1)
Run Code Online (Sandbox Code Playgroud)
另一个是:
nn.init.uniform_(self.in_embed.weight, -1.0, 1.0)
Run Code Online (Sandbox Code Playgroud)
我的问题是:第一种和第二种初始化形式有什么区别。这两种方法做同样的事情吗?
我一直在尝试使用 Maven(版本 3.3.9)和 JUnit 5(不是 4)使用以下命令在一个类中运行单个测试失败:
mvn -Dtest=EmitRulesTest#cr_filter_contact_points_for_C4C_output test
Run Code Online (Sandbox Code Playgroud)
此命令执行所有测试。
尝试这个命令实际上会执行类中的所有测试:
mvn test -Dtest=EmitRulesTest
Run Code Online (Sandbox Code Playgroud)
这是我的 JUnit 5 Maven 配置:
<dependencies>
...
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.0.0-RC2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<systemPropertiesFile>${basedir}/src/test/resources/definitions/system.properties</systemPropertiesFile>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0-RC2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0-RC2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
更多参考:使用 Maven 运行单个测试
如何将Java中的范围(使用java.util.stream.LongStream或java.util.stream.IntStream)转换为Java 中的分隔字符串?
我试过了:
String str = LongStream.range(16, 30)
.boxed()
.map(String::valueOf)
.collect(Collectors.joining(","));
System.out.println(str);
Run Code Online (Sandbox Code Playgroud)
这打印:
16,17,18,19,20,21,22,23,24,25,26,27,28,29
Run Code Online (Sandbox Code Playgroud)
同样可以使用IntStream.是否有更方便的范围转换为分隔字符串?
如何使用Java类定义中的伪数据生成JSON示例?(注意:我并不是在问如何从POJO生成JSON。这是在stackoverflow上问过的。)
我想要的是直接从Java类生成一些样本虚拟数据。例如,您有一个像这样的类:
public class Reservation {
@ApiModelProperty(value = "")
private Traveller leadTraveller = null;
@ApiModelProperty(example = "2", value = "")
private Integer sourceSystemID = null;
@ApiModelProperty(value = "")
private String recordLocation = null;
@ApiModelProperty(example = "2010", value = "")
private Integer recordLocatorYear = null;
}
Run Code Online (Sandbox Code Playgroud)
然后您有一个函数,该函数无需创建POJO即可生成具有伪值的JSON字符串,例如:
{
"leadTraveller": {
"firstNames": "firstNames",
"lastName": "lastName",
"email": "email",
"travellerGUID": "travellerGUID",
"travellerRefs": [{
"customerReportingRank": 37,
"value": "value",
"description": "description"
}]
},
"sourceSystemID": 38,
"recordLocation": "recordLocation",
"recordLocatorYear": 9
}
Run Code Online (Sandbox Code Playgroud)
是否有一个库可以默认执行此操作?
我尝试使用具有以下Maven依赖项的Java代码解决此问题:
<dependency>
<groupId>fctg-ngrp</groupId>
<artifactId>model-core</artifactId>
<version>1.0.4-SNAPSHOT-MODEL-CORE</version> …Run Code Online (Sandbox Code Playgroud) 我们一直在使用Apache Commons文本中的Jaro Winkler模糊匹配算法实现,并且在研究代码时发现了潜在的缺陷。
似乎此实现是基于有关Jaro-Winkler的非常容易理解的Wikipedia文章:
https://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance
Jaro Winkler使用公式来计算两个字符串的接近度。输出通常是0.0到1.0的两倍。Jar-Winkler的内部公式使用匹配项,换位符和公共前缀的数量长度作为输入。
在研究Apache Commons Jaro Winkler实现(请参阅https://commons.apache.org/sandbox/commons-text/jacoco/org.apache.commons.text.similarity/JaroWinklerDistance.java.html)时,我们看到了此代码的前缀长度提取:
int prefix = 0;
for (int mi = 0; mi < min.length(); mi++) {
if (first.charAt(mi) == second.charAt(mi)) {
prefix++;
} else {
break;
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码看起来正确,但是以某种方式与Wikipedia文章中有关前缀长度提取的规范不匹配:
l是字符串开头的公共前缀长度,最多四个字符
根据我的理解,前缀匹配大小不得超过4。
Apache Commons Text实现确实可以提高带有长公共前缀的字符串的得分。例如:
"john.fernandez@onepointltd.com" - "john.fernandez@onepointlt.co"
Run Code Online (Sandbox Code Playgroud)
如果由Apache Commons文本实现评估,则将返回1.0,这意味着我们完全匹配。这感觉不对。
我对社区的问题是:Apache实现不应将前缀长度限制为最多4个吗?
我一直在尝试将Java表达式转换为Kotlin,它产生了这个序列:
1,2,4,8,16,32,64
这是Java代码:
for(int i = 1; i < 100; i = i + i) {
System.out.printf("%d,", i);
}
Run Code Online (Sandbox Code Playgroud)
我发现将其翻译成Kotlin的唯一方法是:
var i = 1
while (i < 100) {
print("$i,")
i += i
}
Run Code Online (Sandbox Code Playgroud)
我试图使用步骤表达式,但这似乎不起作用.有没有办法在Kotlin中更优雅地表达这种类型的序列?
我知道你可以使用Kotlin + Java 9获得这样的代码:
Stream.iterate(1, { it <= 100 }) { it!! + it }.forEach { print("$it,") }
Run Code Online (Sandbox Code Playgroud)
但这依赖于Java库,我更喜欢Kotlin本地库.
Freemarker 中似乎没有方便的内置功能来在模板中生成随机 UUID。
我能想到的最好办法是创建一个Freemarker 方法;在我的 Java 8 代码中,我注入了该方法以供以后在模板中使用。例子:
public String generate(Map<String, Object> data, String templateLocation) throws IOException, TemplateException {
try (StringWriter writer = new StringWriter()) {
Template template = configuration.getTemplate(templateLocation);
// UUID generation method injected in this line:
data.put("uuid", (TemplateMethodModelEx) (list) -> UUID.randomUUID());
template.process(data, writer);
return writer.toString();
}
}
Run Code Online (Sandbox Code Playgroud)
在 Freemarker 模板中,我可以使用如下方法:
${uuid()}
Run Code Online (Sandbox Code Playgroud)
在 Freemarker 中是否有更方便的生成 uuid 的解决方案?
这是我的 Maven 依赖项:
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.25-incubating</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)