[英]Scala List filter by date difference
I've got a problem and I don't really know how to do it in a proper Scala way. 我遇到了一个问题,我真的不知道如何以适当的Scala方式进行操作。
I've got a list of objects, holding a date. 我有一个对象列表,上面有个日期。 I want to do something like this:
我想做这样的事情:
I want to make a selection using an acceptable time value, like 2 hours, between 2 successors in the list. 我想使用可接受的时间值(例如2小时)在列表中的2个后继者之间进行选择。 The purpose is to keeps user trend comparing to a point (if he shows up 2 times here, or 1 or 15 !).
目的是使用户趋势与某个点相比较(如果他出现2次,或者1或15!)。
The algorithm I imagined: 我想象的算法:
How to do it, with some filters or collects? 如何做,使用一些过滤器或收集器? Oh and if the algorithm doesn't sounds good for you, I'm open to criticism!
哦,如果该算法听起来对您不利,那么我可以公开批评!
Edit: I'm not asking for the solution, but just the right functions to lookup! 编辑:我不是要解决方案,而是要查找正确的函数!
Say I have a list of integers, and I want to step through them and only keep the ones which are more than 1 greater than the previous. 说我有一个整数列表,我想逐步遍历它们,只保留比前一个大1的整数。 I would use foldLeft to step through them, building a list of only the items which are acceptable:
我将使用foldLeft逐步浏览它们,构建仅可接受项目的列表:
val nums = List(1,2,4,5,7)
nums.foldLeft(List[Int]()){
case (List(), b) => List(b)
case (list, b) if b - list.head > 1 => list :+ b
case (list, b) => list
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.