How do I populate a list of objects with new values

Apologies: I'm well noob

I have an items class

class item(ind:Int,freq:Int,gap:Int){}

I have an ordered list of ints

val listVar = a.toList

where a is an array

I want a list of items called metrics where

ind is the (unique) integer freq is the number of times that ind appears in list gap is the minimum gap between ind and the number in the list before it so far I have:

def metrics = for {
  n <- 0 until 255 
  listVar filter (x == n) count > 0 
  yield  new item(n, (listVar filter == n).count,0)

It's crap and I know it - any clues?


Well, some of it is easy:

val freqMap = listVar groupBy identity mapValues (_.size)

This gives you ind and freq. To get gap I'd use a fold:

val gapMap = listVar.sliding(2).foldLeft(Map[Int, Int]()) { 
  case (map, List(prev, ind)) =>
    map + (ind -> (map.getOrElse(ind, Int.MaxValue) min ind - prev))

Now you just need to unify them: k => new item(k, freqMap(k), gapMap.getOrElse(k, 0)) )

Need Your Help

Objects within Exports Object

javascript oop node.js module

So I've been looking at this post: What is the purpose of NodeJS module.exports and how do you use it? and have been trying to make sense of it within the scope of JavaScript objects.

Linq-to-Objects Where clause with 2 predicates


Is it better to have 2 Where clauses or 1 Where clause with &amp;&amp; operator or does it not matter?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.