我正在编写一个Scala库,以便更轻松地查询分页JSON API.每个API调用都返回一个如下所示的对象:
{
"count": 100,
"current_page": 1,
"total_pages": 2,
"records": [
...
]
}
Run Code Online (Sandbox Code Playgroud)
我想有一个函数返回某种迭代器,如MyIterator [Record].在Scala世界中是否有任何标准方法可以做到这一点,甚至可能在标准库中构建可以帮助我的方法?
我通常使用lift-json进行JSON解析,如果这有用的话.
谢谢.
我想我错过了一些东西:
scala> Some(1) collect ({ case n if n > 0 => n + 1; case _ => 0})
res0: Option[Int] = Some(2)
scala> None collect ({ case n if n > 0 => n + 1; case _ => 0})
<console>:6: error: value > is not a member of Nothing
None collect ({ case n if n > 0 => n + 1; case _ => 0})
^
<console>:6: error: value + is not a member of Nothing
None …Run Code Online (Sandbox Code Playgroud) 我已经提交了雷达,因为我认为这是一个错误,但我想知道是否有人可以在此期间建议解决方案.
在安装了Xcode的计算机上成功运行数周后,测试将不再执行.似乎Xcode无法再找到模拟器或其设备.从Xcode中的Devices菜单(以及所有目录中~/Library/Developer/CoreSimulator/Devices)删除模拟器设备并再次添加它们无济于事.
模拟器不会启动,并且Xcode中不会输出任何错误消息.system.log在Console.app中打开会显示以下消息:
Nov 21 16:21:29 jenkinss-mini.home xcodebuild[51853]: [MT] DVTAssertions: Warning in /SourceCache/IDEFrameworks/IDEFrameworks-6604/IDEFoundation/Testing/OCUnit/IDEOCUnitTestRunner.m:388
Details: No baseline file matching runDestinationRecord: {
localComputer = {
busSpeedInMHz = 100;
cpuCount = 1;
cpuKind = "Intel Core i5";
cpuSpeedInMHz = 2500;
logicalCPUCoresPerPackage = 4;
modelCode = "Macmini6,1";
physicalCPUCoresPerPackage = 2;
platformIdentifier = "com.apple.platform.macosx";
};
targetArchitecture = i386;
targetDevice = {
modelCode = "iPhone5,1";
platformIdentifier = "com.apple.platform.iphonesimulator";
};
}
Object: <Xcode3OCUnitTestRunner: 0x7fa0d6b61550>
Method: -_scheduledTestArgumentsOperationForBuildParameters:launchParameters:runDestination:workspace:testRunIdentifier:testRunIdentifiers:actionResultsBundleWithBaselineOverridesFilePath:error:completionBlock:
Thread: <NSThread: 0x7fa0d0514030>{name = (null), …Run Code Online (Sandbox Code Playgroud) 我正在制作一个Chaos Monkey程序,我希望它能在一段时间后自行清理.我想要一种简单的方法来将清理任务排队,以便将来称为一定时间.我想我可以和演员一起做点什么,还有很多挥手,但这似乎是错误的做法.在Scala标准库中是否有更好的工具来完成此任务?
我真的很喜欢使用上层类型边界来为我的构造提供一些灵活性.但是,我真的不知道它背后的任何原理,因为我发现使用以下代码:
object BoundsTest {
abstract trait Service
class Collection[T <: Service] extends collection.mutable.HashMap[Symbol, collection.mutable.Set[T]] with collection.mutable.MultiMap[Symbol, T]
type Actives[T <: Service] = collection.mutable.HashMap[Symbol, T]
class Library[T <: Service](collection: Collection[T], actives: Actives[T])
private val libraries = new collection.mutable.HashMap[Symbol, Library[Service]]
def setLibrary[T <: Service](name: Symbol, library: Library[T]) {
libraries += name -> library
}
}
Run Code Online (Sandbox Code Playgroud)
我试图让我的类可以使用子类,Service只要它是一致的.但是,这不起作用:
$ scalac test.scala
test.scala:10: error: type mismatch;
found : com.bubblefoundry.BoundsTest.Library[T]
required: com.bubblefoundry.BoundsTest.Library[com.bubblefoundry.BoundsTest.Service]
Note: T <: com.bubblefoundry.BoundsTest.Service, but class Library is invariant in type T. …Run Code Online (Sandbox Code Playgroud) 我发现了一个类似的问题,但它有一个似乎更简单的情况,昂贵的操作始终是相同的.就我而言,我想收集一些我希望并行执行的昂贵API调用的结果.
说我有:
def apiRequest1(q: Query): Option[Result]
def apiRequest2(q: Query): Option[Result]
Run Code Online (Sandbox Code Playgroud)
哪里q是相同的值.
我想要一个List[Result]或类似的(显然List[Option[Result]]很好),我希望两个昂贵的操作并行发生.
当然,一个简单的List构造函数不会并行执行:
List(apiRequest1(q), apiRequest2(q))
Run Code Online (Sandbox Code Playgroud)
并行集合可以帮助吗?或者我应该期待未来等?我可以想到使用并行集合的唯一方法似乎很简单:
List(q, q).par.zipWithIndex.flatMap((q) =>
if (q._2 % 2 == 0) apiRequest1(q._1) else apiRequest2(q._1)
)
Run Code Online (Sandbox Code Playgroud)
实际上,所有事情都是平等的,也许这并不是那么糟糕......
我有以下变量series:
var series: List[FlotSerie] = List(
new FlotSerie() {
override val label = Full("Min")
},
new FlotSerie() {
override val label = Full("Max")
},
new FlotSerie() {
override val label = Full("Avg")
}
)
Run Code Online (Sandbox Code Playgroud)
不幸的是,我使用以下方法获得编译器错误,该方法采用新数据点并series使用List[FlotSeries]基于新数据和旧系列的新数据进行更新.
def updateSeries(sample: Sample): List[FlotSerie] = {
series = series.map(serie =>
serie match {
case item if item.label == Full("Min") => {
new FlotSerie() {
override val label = item.label
override val data = (sample.timestamp.toDouble, sample.min) :: serie.data
}
} …Run Code Online (Sandbox Code Playgroud) 考虑以下:
scala> object Currency extends Enumeration {
| type Currency = Value
| val USD = Value
| val GBP = Value
| val EUR = Value
| val TRY = Value // Turkish lira
| val NGN = Value // Nigerian naira
| }
defined module Currency
scala> import Currency._
import Currency._
scala> val pf: (String) => Option[Currency] = {
| case "$" => Some(USD)
| case "€" => Some(EUR)
| case "£" => Some(GBP)
| case "?" => …Run Code Online (Sandbox Code Playgroud) 我有一个简单的容器特征,如下所示:
trait Handler {
def apply[In, Out](in: In): Out
}
Run Code Online (Sandbox Code Playgroud)
当我尝试实现它时:
new Handler {
def apply[Any, Unit](in: Any) = println(in)
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
[error] found : scala.Unit
[error] required: Unit(in method apply)
[error] def apply[Any, Unit](in: Any) = println(in)
Run Code Online (Sandbox Code Playgroud)
为什么是这样?我可以修复此编译器错误吗?
我认为这是关于协方差,但我对这个话题很弱......
我有一个通用的Event类用于数据库持久性之类的东西,让我们这样说:
class Event(
subject: Long,
verb: String,
directobject: Option[Long],
indirectobject: Option[Long],
timestamp: Long)
{
def getSubject = subject
def getVerb = verb
def getDirectObject = directobject
def getIndirectObject = indirectobject
def getTimestamp = timestamp
}
Run Code Online (Sandbox Code Playgroud)
但是,我有很多不同的事件动词,我想使用模式匹配等这些不同的事件类型,所以我将创建一些相应的case类:
trait EventCC
case class Login(user: Long, timestamp: Long) extends EventCC
case class Follow(
follower: Long,
followee: Long,
timestamp: Long
) extends EventCC
Run Code Online (Sandbox Code Playgroud)
现在,问题是,如何轻松地将通用事件转换为特定的案例类.
这是我第一次尝试:
def event2CC[T <: EventCC](event: Event): T = event.getVerb match {
case "login" => Login(event.getSubject, event.getTimestamp)
case "follow" => Follow( …Run Code Online (Sandbox Code Playgroud) scala ×9
types ×3
api ×1
collect ×1
covariance ×1
flot ×1
ios ×1
iterator ×1
lift ×1
option ×1
pagination ×1
return-type ×1
scheduling ×1
subclass ×1
subtype ×1
type-bounds ×1
xcode ×1