我想将.conf文件直接转换为json,以便将其传递给前端.有没有办法在scala/play中这样做?我正在采取的路径似乎非常麻烦:
val conf: Configuration = play.api.Configuration.apply(ConfigFactory.parseFile(new File("app/assets/strings.conf")))
conf.entrySet.seq.map(t => t._1 -> t._2.unwrapped())
// which gives me a Seq[(String, AnyRef)] which cannot be converted with Json, so the path from here is even uglier
Run Code Online (Sandbox Code Playgroud)
我很想回到JSON,但HOCON语法非常适合我们的用例.HOCON是basicly JSON用更少的括号和引号-所以转换应该是很简单的.我还是找不到一个简单的方法来玩play/scala这样的事情.
Eclipse中Java的任何有用的第三方编码模板是什么?例如,对于Collections的一些会很好.有人推荐吗?
通过编码模板,我的意思是:
public static void main(String[] args) {
${cursor}
}
Run Code Online (Sandbox Code Playgroud)
和
${array_type}[] ${result:newName(array)} = new ${array_type}[${array}.length + 1];
System.arraycopy(${array}, 0, ${result}, 0, ${array}.length);
${result}[${array}.length]= ${var};
Run Code Online (Sandbox Code Playgroud)
等等
假设您有一个客户端和一个想要共享/同步相同模型/对象的服务器.模型指向彼此,并且您希望它们在客户端和服务器之间发送/序列化后始终指向同一对象.我目前的解决方案大致如下:
class Person {
static Map<Integer,Person> allPeople;
int myDogId;
static Person getPerson(int key){
return allPeople.get(key);
}
Dog getMyDog() {
return Dog.getDog(myDogId);
}
}
class Dog {
static Map<Integer,Dog> allDogs;
int myOwnersId;
static Dog getDog(int key) {
return allDogs.get(key);
}
Person getMyOwner() {
return Person.getPerson(myOwnersId);
}
}
Run Code Online (Sandbox Code Playgroud)
但我对这个解决方案不太满意,字段是整数和东西.这也应该是一个非常普遍的问题.所以我在这里寻找的是这个问题的名称,模式,通用解决方案或库/框架.
java serialization pointers design-patterns architectural-patterns

我已经尝试过Package Control: Remove Pacakage再安装它.本CoffeeScript.tmLanguage 应该是正常的.有没有其他人经历过这个?怎么修好?
textmate textmatebundles coffeescript sublimetext sublimetext2
在playframework中缓存未来结果的正确方法是什么.例如:
val userGravatar: Future[JsValue] = RemoteGravatarService.get(user.id, user.email)
object RemoveGravatarService {
def get(userId: String, email: String): Future[JsValue] = {
Cache.getOrElse("gravatar-for-$userId", 1.hour) {
WS.url("gravatar.com/email=$email").get().asJson
}
}
}
Run Code Online (Sandbox Code Playgroud)
我们不想问(这个虚构的)"的Gravatar"每一次,因为它不会改变该频率.但我们需要在本地使用som userGravatar信息.
在这里我们缓存未来本身,但实际上我们只想缓存未来的结果.有没有方便/正确的方法来玩这个?
case class Person(name: String, age: Int, qualified: Boolean = false)
val people: List[Person] = ....
val updated: List[Person] = people.map(person =>
if (person.age >= 25)
person.copy(qualified=true)
else
person // unmodified
))
// Setting every person above 25 y.o. as qualified
Run Code Online (Sandbox Code Playgroud)
有没有一个组合器/更高阶函数的方法来做到这一点?喜欢:
people.updateWhere(_.age >= 25, _.copy(qualified=true))
Run Code Online (Sandbox Code Playgroud)
这就像一个有条件的map.大多数元素都是未修改的,但满足条件的元素会被修改/"映射".
combinations functional-programming scala higher-order-functions
我们的项目将C#和F#与ASP.NET Core混合在一起。ASP.NET内核默认使用Newtonsoft来在任何地方进行序列化/反序列化,而且庞大的文档生成(nswag)也依赖于类似于CLI(Mutable)的DTO来生成正确的类型。
我们的DTO在F#中定义如下:
[<CLIMutable>]
type Query =
{ Status: string
Offset: int
Limit: int }
Run Code Online (Sandbox Code Playgroud)
这使得Newtonsoft进行序列化和nswag生成swagger doc变得很简单。
但..!我在使用null / option“ mismatch”时非常困难。C#/ Newtonsoft / NSwag坚持使用null,并且不会理解F#option。所以在我的F#中,我必须检查null并将它们转换为option;从域转换回DTO时,还将我的选项转换回null。
但是F#坚持认为,在什至不允许我检查的情况下,不可能出现null:编译器给我:“类型{...}没有适当的'null'”。但事实是,运行时,它IS空,因为这是Newtonsoft是如何工作的。Newtonsoft是在 .NET标准看来,所以我不认为用别的东西是可行的不幸。
即使F#认为不可能为null,我如何检查null?
采取如下的简单类:
class Person(name: String, age: Int) {}
Run Code Online (Sandbox Code Playgroud)
现在,当我实例化这个类时,我通常希望它的用户能够使用name和age.喜欢:
val ronald = new Person("Ronald", 22)
val ronaldsName = ronald.name // <-
Run Code Online (Sandbox Code Playgroud)
当然,这不起作用,除非我定义一个被调用的gettername,或者创建Person一个case类.后者是一个简单的解决方案,但我觉得我在滥用案例类?
前者有点不方便,因为我不能简单地说:
class Person(name: String, age: Int) {
def name = name
}
Run Code Online (Sandbox Code Playgroud)
所以,那么我必须name将类的构造函数中的第一个重命名为其他东西,比如personNameor _name或n.但这在我看来有点令人困惑,而且不那么优雅.这是相同的概念/变量/值,所以它应该具有完全相同的名称,对吗?
那么......这里最好或最正确的做法是什么?添加它是如此诱人case.
在foo这里创建函数时,我引用了fromTheFuture尚未声明的变量.这实际上按预期工作,但为什么?它被认为是危险的还是不好的做法?
var foo = function(x) {
return fromTheFuture * x;
};
var fromTheFuture = 5;
console.log(foo(10));
Run Code Online (Sandbox Code Playgroud)
我可以看到这非常方便,如果你有几个函数想要以循环方式相互使用 - 而不必var在方法的开头声明它们.
scala ×4
java ×2
.net ×1
caching ×1
case-class ×1
coffeescript ×1
combinations ×1
eclipse ×1
f# ×1
hocon ×1
javascript ×1
json.net ×1
null ×1
optional ×1
performance ×1
pointers ×1
sublimetext ×1
sublimetext2 ×1
templates ×1
textmate ×1