scala accessing list objects , evaluating them
i have list
val example = list(itemdesc(6164,6165,6166,6195,the values correct), itemdesc(14879,14879,14879,14894,the values ok), itemdesc(19682,19690,19682,19694,the values good))
from example list, want access object 'itemdesc'. , count , odd count.
for example, taking first object in list
itemdesc(6164,6165,6165,6195,the values correct)
i want find out,
evenplacescount = 6164+6166
oddplacescount = 6165+6195
i have code evaluate if plain list(6164,6165,6166,6195,the values correct).
but how compute if have list objects 'example' above ?
something this?
case class itemdesc(a: int, b: int, c: int, d: int, desc: string) case class pair(even: int, odd: int) val example = list( itemdesc(6164, 6165, 6166, 6195, "the values correct"), itemdesc(14879, 14879, 14879, 14894, "the values ok"), itemdesc(19682, 19690, 19682, 19694, "the values good") ) def evenoddcount(item: itemdesc): pair = pair(item.a + item.c, item.b + item.d) def addevenoddcounts(first: pair, second: pair): pair = pair((first.even + second.even), (first.odd + second.odd)) val evenoddpair = evenoddcount(itemdesc(6164, 6165, 6166, 6195, "the values correct")) // pair(12330,12360) val allpairs = example.map(evenoddcount) // list(pair(12330,12360), pair(29758,29773), pair(39364,39384)) val totalpair = example.map(evenoddcount).reduce(addevenoddcounts) // pair(81452,81517)
Comments
Post a Comment