我有以下案例类:
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解决问题.
你有什么想法吗?
我很想用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) 在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?
是否有一种简单的方法可以收集满足谓词的所有结构?
(./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)
我们怎样才能获得所有的价值:名称,包括["镇流器""船体"]?
如何def someA(in trait B)使用trait A与C#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) 在使用模式匹配实现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 ×4
cake-pattern ×1
case-class ×1
clojure ×1
java ×1
manifest ×1
scala-2.9 ×1
shapeless ×1
specter ×1
types ×1