如何定义生成器以生成给定fields
的{{1}}的随机数据。以下是脚手架代码。我需要一个表达式的帮助来替换Block
。此表达式应生成???
,但应使用已定义的Seq[Field]
函数生成Field
。
genField
ADT
def blockGen(b: Block): Gen[Block] = for {
id <- b.blockId //Use the same blockId as b
fields <- ??? //Get the type from each field and call genField
} yield Block(id, fields)
发电机
trait Data {}
trait Field extends Data {
val name: String
val value: String
}
case class StringField(name: String, value: String) extends Field
case class NumberField(name: String, value: String) extends Field
case class Block(blockId: Field, fields: Seq[Field]) extends Data
示例块:myBlock
def fieldGen(fieldType: Field): Gen[Field] = {
for {
f <-
fieldType match {
case _: NumberField => numGen
case _: StringField => strGen
}
} yield f
}
val strGen: Gen[StringField] = for {
name <- Gen.identifier
value <- Gen.alphaStr
} yield StringField(name, value)
val numGen: Gen[NumberField] = for {
name <- Gen.identifier
value <- Gen.numStr
} yield NumberField(name, value)
答案 0 :(得分:4)
我相信您正在寻找的是private let cellId = "cellId"
class InfoViewShow: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
private let cellHeight:CGFloat = 80
var collectionView: UICollectionView?
let layout = UICollectionViewFlowLayout()
override init() {
super.init()
setupCollectionview()
collectionView?.registerClass(InfoCell.self, forCellWithReuseIdentifier: cellId)
}
private func setupCollectionview() {
if let view = UIApplication.sharedApplication().keyWindow {
//let y = (view.frame.width * 10 / 16) + 34 + 26
collectionView = UICollectionView(frame: .zero , collectionViewLayout: layout)
collectionView?.backgroundColor = UIColor.redColor()
collectionView?.delegate = self
collectionView?.dataSource = self
collectionView?.frame = CGRectMake(0, 0, view.frame.width, view.frame.height) // y to y
}
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath)
cell.backgroundColor = UIColor.blackColor()
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(collectionView.frame.width, cellHeight)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(5, 5, 5, 5); //top,left,bottom,right
}
}
。
此外,Gen.sequence
并非需要来自生成器,因为您所做的只是直接使用特定值。
id
顺便说一下,def blockGen(b: Block): Gen[Block] =
for {
fields <- Gen.sequence[Seq[Field], Field](b.fields.map(fieldGen))
} yield Block(b.blockId, fields)
与Future.sequence
和sequence
in Haskell非常相似。