你做
import shapeless._ ; import poly._
object fun extends (List ~>> (List, Int)) {
override def apply[T](list: List[T]): (List, Int) = list -> list.size
}
println((List(1,2,3) :: List("a", "b", "c") :: HNil).map(fun))
将每个子列表映射成一对。但是,如果HList元素更复杂,例如tutples会怎样?自然的尝试
object fun extends ((String -> List) ~>> (List, Int)) {
override def apply[T](list: (String -> List[T])): (List, Int) = list -> list.size
被编译器拒绝。你是做什么?你在哪里可以了解到这一点?
答案 0 :(得分:2)
React.createClass({
getInitialState: function() {
return {lists: [{
key1: 'value',
key2: 123,
key3: {
key30: 'value',
key31: {
key310: 123,
key311: 'hello'
}
}
}]}
},
generateFirst: function(parent, data, tempContainer) {
var li
var liHead
var wrap
var key
for (key in data) {
if (this.getType(data[key]) === 'object') {
// li head
liHead = React.DOM.li({
className: 'token-head'
}, key + ':' + data[key])
// line
li = React.DOM.li({
className: 'iop'
}, React.DOM.ul({
className: 'iok'
}, this.generateFirst(data[key])))
// wrap
wrap = React.DOM.ul({
className: 'wrap-ul'
}, li)
tempContainer.push(liHead)
tempContainer.push(wrap)
} else {
li = React.DOM.li({
className: 'iol'
}, key + ':' + data[key])
tempContainer.push(li)
}
}
},
renderTokenContent: function(data) {
var tempContainer = []
this.generateFirst(data, tempContainer)
return (<ul>{tempContainer}</ul>)
},
render: function() {
var self = this
return (<ul className="scroll-helper">
{ this.state.lists.map(function(data) {
return (<li >
{ self.renderTokenContent(data) }
</li>)
}) }
</ul>)
}
})
然后:
import shapeless._
import poly._
object fun2 extends Poly1 {
implicit def caseTuple[T] =
at[(String, List[T])](x => (x._2.tail, x._1.toInt))
}
或者如果你想用原始的println((("56", List(1,2,3)) :: ("78", List(4,2,3)) :: HNil).map(fun))
来做它仍然可能,但看起来很难看我的口味:
~>
P.S。另请注意,您的代码示例无法编译。您在第一部分中错过了object fun3 extends (({type L[T] = (String, List[T])})#L ~> ({type L[T] = (List[T], Int)})#L) {
override def apply[T](f: (String, List[T])): (List[T], Int) = f match {
case (str, list) => (list.tail, str.toInt)
}
}
println((("56", List(1,2,3)) :: ("78", List(4,2,3)) :: HNil).map(fun3))
,在最后一部分中需要使用[T]
而~>
在scala中是错误的类型。 String -> List
是一个类型构造函数,而不是类型,你不能这样使用它