我有一个嵌套的case
类结构List
为简单起见,请使用以下示例 -
case class Address(street: String, city: String, state: String, zipCode: Int)
case class Person(firstName: String, lastName: String, addresses: List[Address])
case class Department(people: List[Person])
Run Code Online (Sandbox Code Playgroud)
说有List[Department]
; 现在,如果我想List[Department]
通过过滤Address
每个Person
没有特定zipCode
值的新东西来创建一个新的; 传统上我们可以做到以下
def filterPersonsByAddress(dlist: List[Department]): List[Department] = {
dlist.map { d =>
val people = d.people.map { p =>
p.copy(addresses = p.addresses.filterNot(_.zipCode == 38978))}
d.copy(people=people)
}
}
Run Code Online (Sandbox Code Playgroud)
这种方法不具备性能,因为取决于嵌套级别,它可以是Big O(n ^ 2)或Big O(n ^ 3);
我试图通过Monocle学习镜头.到目前为止,我学到的是当你必须"修改"一个深层嵌套的case
类结构但尚未找到一种方法根据条件"切断"嵌套结构的某些部分并返回一个新结构时,镜头很有用.这可能是通过Monocle吗?此外,我不确定Lenses是否能够帮助实现更好的Big O时间?
我有以下两个含义.
implicit val readObjectIdFormat = new Reads[ObjectId] {
def reads(jv: JsValue): JsResult[ObjectId] = {
JsSuccess(new ObjectId(jv.as[String]))
}
}
implicit val visitorFormat = (
(__ \ "_id").formatOpt[ObjectId] and
(__ \ "visitorId").format[String] and
(__ \ "referralUrl").formatOpt[String] and
(__ \ "ipAddress").formatOpt[String] and
(__ \ "promotionId").format[String])(Visitor)
Run Code Online (Sandbox Code Playgroud)
虽然readObjectIdFormat是在编译时定义的,但它继续抱怨"(__ \"_ id").formatOpt [ObjectId]"line
找不到类型为org.bson.types.ObjectId的Json格式化程序.尝试为此类型实现隐式格式.
版本:播放2.1-RC2,Scala 2.10
知道为什么它不能识别readObjectIdFormat吗?
以下是一个玩具示例,以展示现实生活遗留方法的形状怪异和问题的要点.
正如您所看到的anotherFunc
,在将personList
expands类型映射到\/[Throwable,List[\/[Throwable,String]]]
不是返回类型但是map
ing的效果之后personList
.再次下面的内容anotherFunc
是为了演示目的(实际上有更多有意义的事情发生而不是Option("fakeString")
或任何一个.
要求是map
personList
,如果它right
然后对List[Person]
返回的每个元素right
(从析取返回的一侧)做一些事情.
如何简化/展平返回类型anotherFunc
(返回\/[Throwable,List[String]]
).可能正在使用其他组合器?
case class Person(name :String)
def personList : \/[Throwable,List[Person]] ={
\/.fromTryCatch{
List(Person("John"))
}
}
def anotherFunc : \/[Throwable,List[\/[Throwable,String]]]= {
personList.map{ pl =>
pl.map{p =>
for{
s <- Option("fakeString").\/>(new Throwable("not found"))
} yield s
}
}
}
Run Code Online (Sandbox Code Playgroud) 如果我有以下方法
def getMyList :\/[Throwable,List[\/[Throwable,Int]]] ={
....
}
Run Code Online (Sandbox Code Playgroud)
如何扁平化的类型getMyList
来\/[Throwable,List[Int]]
如果我想在调用.get
任何Option
值时生成编译时错误,如何执行此操作?
没有编写任何自定义宏但是猜测它是时候了吗?有什么指针吗?
我们有使用贷款模式的测试装置.利用此模式创建运行测试所需的"种子数据".当测试依赖于数据时例如以下
"save definition" should {
"create a new record" in withSubject { implicit subject =>
withDataSource { implicit datasource =>
withFormType { implicit formtype =>
val definitn = DefinitionModel(-1, datasource.id, formtype.id, subject.role.id, Some(properties))
}
}
}
Run Code Online (Sandbox Code Playgroud)
其中withSubject
,withDataSource
,withFormType
是测试夹具返回subject
,dataSource
,formType
从数据库数据分别.withDataSource
夹具需要subject
隐含.建筑DefinitionModel
要求datasource.id
和formtype.id
.所以根据测试的数据要求调用这样的数据构建器夹具会产生很多嵌套的夹具情况.有没有更好的方法来"组合"/构建这样的灯具?
Scala中var和val的内部实现是什么?有兴趣知道他们的实现的细节 - 是什么使var变为"var"如何实现可变结构vs val(更像是最终的)结构,这使得它是不可变的.
试图通过Miles Sabin 使用这个技巧来创建一个只接受一组预定义参数的函数types
.
val bool = Boolean
val timestamp = new Timestamp(date.getTime())
val str = "My String"
Run Code Online (Sandbox Code Playgroud)
然后跟随应该在编译时通过
takeValue(bool)
takeValue(timestamp)
takeValue(str)
Run Code Online (Sandbox Code Playgroud)
但
这里takeValue
应该失败takeValue(someIntValue)
,如果implicit
用于type
Int
不defined.And这种故障会在编译的时候.
trait MyConv[K] { type V; def convert: AnyRef => V }
def iMakeConv[V0](con: AnyRef => V0) = new MyConv[con.type] {
override type V = V0
val convert = con
}
def takeValue(value:AnyRef)(implicit conv :MyConv[value.type]) : \/[Throwable,conv.V] = \/.fromTryCatch(conv.convert(value))
Run Code Online (Sandbox Code Playgroud)
然后
implicit val strAny = iMakeConv((x:Any) …
Run Code Online (Sandbox Code Playgroud) 我sequenceU
在scalaz中使用disjunctions时用来转换内部类型.
例如
val res = List[\/[Errs,MyType]]
干
res.sequenceU
会给 \/[Errs,List[MyType]]
现在,如果我有一个val res2 = List[(\/[Errs,MyType], DefModel)]
- List
含有分离的元组; 什么是正确的转换方式
res2
至 \/[Errs,List[ (Mype,DefModel)]
为了这篇文章的目的,代码片段被轻视,抽样.
case class Person(firstName:String,lstName:String)
Run Code Online (Sandbox Code Playgroud)
此人类已在代码库中的所有位置使用.现在,后来的需求发生了变化,并决定phoneNumber
在人案例类中添加
例如
case class Person(firstName:String,lstName:String,phoneNumber:String)
Run Code Online (Sandbox Code Playgroud)
再次,帖子中的例子极为简化.实际上,有更多有趣的事情正在发生.请注意,这phoneNumber
不是Option
必填字段.通常,人们会更新所有使用Person
类的代码,以满足新领域的需求lastName
.当你有100个引用它时,这是相当繁琐的.
HList
从get get vs case class 创建更灵活的无形帮助?
在使用Scala的Play Framework中,如何将String和Seq [String]值的Map转换为JSON格式?以下代码为我生成错误:
val objMap = Map("id" -> "id", "tags" -> Seq("tag1", "tag2"))
Json.toJson(objMap)
Run Code Online (Sandbox Code Playgroud)
结果错误:
No Json serializer found for type scala.collection.immutable.Map[String,Object]. Try to implement an implicit Writes or Format for this type.
Run Code Online (Sandbox Code Playgroud) analyzer_keyword
创建索引时添加了新的分析器- 请参阅下面的create index
curl -XPUT 'http://mycluster/dsi2' -d '{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 1,
"analysis": {
"analyzer": {
"analyzer_keyword": {
"tokenizer": "keyword",
"filter": "lowercase"
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
运行_settings
端点确认以下内容
http GET http://mycluster/dsi2/_settings
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 267
Content-Type: application/json; charset=UTF-8
{
"dsi2": {
"settings": {
"index": {
"analysis": {
"analyzer": {
"analyzer_keyword": {
"filter": "lowercase",
"tokenizer": "keyword"
}
}
},
"creation_date": "1484088347598",
"number_of_replicas": "1",
"number_of_shards": "1",
"uuid": "Lu98fn6gRiOe3Q1y8fU6tQ",
"version": …
Run Code Online (Sandbox Code Playgroud)