小编jwi*_*ndy的帖子

如何在Scala中表示对case classe的部分更新?

我有以下案例类:

case class PropositionContent(title:String,content:String)
Run Code Online (Sandbox Code Playgroud)

我想代表它作为数据的部分修改.

一种方法是创建案例类:

case class PartialPropositionContent(title:Option[String],content:Option[String)
Run Code Online (Sandbox Code Playgroud)

然后是一些方法:

object PropositionContent {

   def append( pc  : PropositionContent
             , ppc : PartialPropositionContent) =
   PropositionContent ( ppc.title.getOrElse(pc.title)
                      , ppc.content.getOrElse(pc.content) )

   def append( ppc  : PartialPropositionContent
             , ppc2 : PartialPropositionContent ):  PartialPropositionContent = {...}

}
Run Code Online (Sandbox Code Playgroud)

但这有点像锅炉架!

我认为一个case class PropositionContent[M[_]](title:M[String],content:M[String])不会真正解决的问题,我不知道如何使用Shapeless解决问题.

你有什么想法吗?

scala case-class shapeless

12
推荐指数
2
解决办法
732
查看次数

在Java中,有没有办法指定参数实现两个接口

我很想用jGraphT做这样的代码

/*
  interface DirectedGraph<V,E> { ...}
  interface WeightedGraph<V,E> { ...}
*/

public class SteinerTreeCalc  {

    public SteinerTreeCalc( < ??? implements DirectedGraph<V,E>, WeightedGraph<V,E> > graph )  {
     ......
    }


}
Run Code Online (Sandbox Code Playgroud)

我想创建一个构造函数,要求实现两个接口的对象.

更新:

在我的目标中,已经为Vertex和Edges(V和E)选择了类,但非常感谢那些想出的人:

public class SteinerTreeCalc <V, E, T extends DirectedGraph<V, E> & WeightedGraph<V, E>>  
{ 
   ....
}
Run Code Online (Sandbox Code Playgroud)

java

11
推荐指数
3
解决办法
1612
查看次数

通用按类型在scala中收集

在Scala 2.9.1中

def collectFirstOfT[T](la: List[_])(implicit m:Manifest[T]) : Option[T] = {
  la.collect{case x if m.erasure.isAssignableFrom(x.getClass) => x}.
    headOption.asInstanceOf[Option[T]]}

class A
class B
Run Code Online (Sandbox Code Playgroud)

为什么这个表达:

val oB:Option[B] = collectFirstOf(List(new A,new B)) 
Run Code Online (Sandbox Code Playgroud)

编译,但收集一些(A),但

val oB =collectFirstOf[B](List(new A,new B))
Run Code Online (Sandbox Code Playgroud)

工作良好.

如何从Option [T]中推断T?

scala manifest scala-2.9

6
推荐指数
1
解决办法
195
查看次数

使用specter进行递归映射查询

是否有一种简单的方法可以收集满足谓词的所有结构?

(./pull '[com.rpl/specter "1.0.0"])

(use 'com.rpl.specter)

(def data {:items [{:name "Washing machine"
                    :subparts [{:name "Ballast" :weight 1}
                               {:name "Hull"    :weight 2}]}]})



(reduce + (select [(walker :weight) :weight] data))
;=> 3

(select [(walker :name) :name] data)
;=> ["Washing machine"]
Run Code Online (Sandbox Code Playgroud)

我们怎样才能获得所有的价值:名称,包括["镇流器""船体"]?

clojure specter

6
推荐指数
1
解决办法
555
查看次数

蛋糕模式和类型

如何def someA(in trait B)使用trait AC#MyTypein中相同B?(然后A#MyType =:= B#MyType)

trait C {
  type MyType
}


trait A {
  self: C =>
  def doSomething(s: MyType) { println(s.toString)}
}

trait B {
  self: C =>  

  def someA: A
  def myType: MyType

  def action = someA.doSomething(myType)
}

// Mix part

case class Ahoy(value: String)

trait ConcreteC extends C {  
  type MyType = Ahoy
}


class PieceOfCake extends B with ConcreteC {
  val someA = …
Run Code Online (Sandbox Code Playgroud)

types scala cake-pattern

5
推荐指数
1
解决办法
278
查看次数

如何在Scala中使用模式匹配来避免def定义的语法开销?

在使用模式匹配实现def时如何避免包装args?

例子 :

def myDef(a: A, b:B, c: C): D = (a,c,d) match {
  case ('qsdqsd, _ , _ ) => ???
  case _ => ???
}
Run Code Online (Sandbox Code Playgroud)

scala pattern-matching

4
推荐指数
1
解决办法
174
查看次数