I have a nested structure in scala and I want to filter this structure but not sure how I can do it: the main structure is
我在scala中有一个嵌套结构,我想过滤这个结构但不知道我该怎么做:主结构是
currentCustStory = new CustStory(custEvent.custId, custEvent.eventType, custEvent.eventTime.toLong, currentEventStory)
currentEventStory = new mutable.MutableList[StoryEvent]
storyEvent
has 4 attributes.
storyEvent有4个属性。
I want to filter some elements from currenteventstory
(a list that belongs to currentcustStory
) and create a new currentcustStory
with elements of Story event that pass the filter.
我想从currenteventstory(属于currentcustStory的列表)中过滤掉一些元素,并创建一个新的currentcustStory,其中包含通过过滤器的Story事件元素。
Does anyone have any idea how I can do this? p.s: I am very newbie in Scala
有谁知道我怎么能这样做? p.s:我是斯卡拉的新手
1 个解决方案
#1
It can be done like this:
它可以这样做:
val newCurrentEventStory = currentCustStory.currentEventStory.filter(func(x) => Boolean)
val newCustStory = new CustStory(custEvent.custId, custEvent.eventType, custEvent.eventTime.toLong, newCurrentEventStory)
You have to figure out what func(x) should be, but it is a function taking one StoryEvent argument and returning true or false. Technically its type is:
你必须弄清楚func(x)应该是什么,但它是一个函数,它接受一个StoryEvent参数并返回true或false。从技术上讲,它的类型是:
func(storyEvent: StoryEvent):Boolean
Based on the latest information you provided, I think it would be best to do the filtering with a for comphresion because it makes the code easier to understand, debug and modify. Here is an implementation assuming the first element of currentCustStory.currentEventStory passes if its distance < 60:
根据您提供的最新信息,我认为最好使用for comphresion进行过滤,因为它使代码更易于理解,调试和修改。这是一个实现,假设currentCustStory.currentEventStory的第一个元素在其距离<60时通过:
val newCurrentEventStory = new MutableList[StoryEvent]()
val ces = currentCustStory.currentEventStory
var p = false // records if previous element passed
// filter currentCustStory.currentEventStory and build newCurrentEventStory
for (i <- 0 to (ces.length - 1)) {
if (i == 0) {
if (ces(0).distance < 60) {
p = true
newCurrentEventStory += ces(0)
} else p = false
}
if (p == true) {
if (ces(i).distance < 60 || ces(i - 1).TimeFromRoot < 90) {
p = true
newCurrentEventStory += ces(i)
} else p = false
}
}
val newCustStory = new CustStory(custEvent.custId, custEvent.eventType, custEvent.eventTime.toLong, newCurrentEventStory)
#1
It can be done like this:
它可以这样做:
val newCurrentEventStory = currentCustStory.currentEventStory.filter(func(x) => Boolean)
val newCustStory = new CustStory(custEvent.custId, custEvent.eventType, custEvent.eventTime.toLong, newCurrentEventStory)
You have to figure out what func(x) should be, but it is a function taking one StoryEvent argument and returning true or false. Technically its type is:
你必须弄清楚func(x)应该是什么,但它是一个函数,它接受一个StoryEvent参数并返回true或false。从技术上讲,它的类型是:
func(storyEvent: StoryEvent):Boolean
Based on the latest information you provided, I think it would be best to do the filtering with a for comphresion because it makes the code easier to understand, debug and modify. Here is an implementation assuming the first element of currentCustStory.currentEventStory passes if its distance < 60:
根据您提供的最新信息,我认为最好使用for comphresion进行过滤,因为它使代码更易于理解,调试和修改。这是一个实现,假设currentCustStory.currentEventStory的第一个元素在其距离<60时通过:
val newCurrentEventStory = new MutableList[StoryEvent]()
val ces = currentCustStory.currentEventStory
var p = false // records if previous element passed
// filter currentCustStory.currentEventStory and build newCurrentEventStory
for (i <- 0 to (ces.length - 1)) {
if (i == 0) {
if (ces(0).distance < 60) {
p = true
newCurrentEventStory += ces(0)
} else p = false
}
if (p == true) {
if (ces(i).distance < 60 || ces(i - 1).TimeFromRoot < 90) {
p = true
newCurrentEventStory += ces(i)
} else p = false
}
}
val newCustStory = new CustStory(custEvent.custId, custEvent.eventType, custEvent.eventTime.toLong, newCurrentEventStory)