English 中文(简体)
在Schala的“脱胎”清单
原标题:"Deinterlacing" a list in Scala

我有一份显示来自音频接口的原始样本的盒子清单。 根据使用案例和H/W,每个样本的长度从1到4个不等,“上游”的渠道总数可能多少是任意的。 每一样本的渠道和借方数量在操作时间都知道。

我举了一个例子,说明我的含义。 溪流有4个渠道,每个样本有2个。

List(A1、A2、B1、B2、C1、C2、D1、D2、A3、A4、B3、B4、C3、C4、D3、D4)

A1是第一个星号:A第一组样本,A2是同一样本的第二星,等等。

我需要做的是将每一频道的样本输入自己的名单,例如:

List (List (A1, A2, A3, A4), List (B1, B2, B3, B4), List (C1, C2, C3, C4), List (D1, D2, D3, D4)

我会怎样做这件事呢? 我刚刚在几个小时前开始学习,我提出的唯一的非强制性解决办法显然不乐观:

def uninterleave(samples: Array[Byte], numChannels: Int, bytesPerSample: Int) = {
val dropAmount = numChannels * bytesPerSample
  def extractChannel(n: Int) = {
    def extrInner(in: Seq[Byte], acc: Seq[Byte]): Seq[Byte] = {
      if(in == List()) acc
      else extrInner(in.drop(dropAmount), in.take(bytesPerSample) ++ acc)
    }
    extrInner(samples.drop(n * bytesPerSample), Nil)
  }

  for(i <- 0 until numChannels) yield extractChannel(i)
}
最佳回答

我会这样做。

samples.grouped(bytesPerSample).grouped(numChannels).toList
  .transpose.map(_.flatten)

不过,我不会为它的业绩vo。 我会避免名单,但不幸的是,<编码>组别的<>>代码/代码”生成。

Maybe

samples.grouped(bytesPerSample).map(_.toArray)
  .grouped(numChannels).map(_.toArray)
  .toArray
  .transpose
  .map(flatten)

然而,名单很多。

问题回答

didierd answer 只是说是完美的,但是,我想有一份can改进了。 他对所有名单编制感到关切,转口也是一种相当繁重的工作。 如果你能够同时处理所有数据,数据可能很好。

然而,Im正在使用<代码>Stream,并且利用微薄的手法避免转换。

首先,分组是一样的,只有Im把 st变成了溪流:

def getChannels[T](input: Iterator[T], elementsPerSample: Int, numOfChannels: Int) =
  input.toStream.grouped(elementsPerSample).toStream.grouped(numOfChannels).toStream

其次,请你履行one的职责。 渠道:

def streamN[T](s: Stream[Stream[Stream[T]]])(channel: Int) = s flatMap (_(channel))

有了这些,我们就能够把这样的各流编成法典:

// Sample input
val input = List( A1,  A2,  B1,  B2,  C1,  C2,  D1,  D2,  A3,  A4,  B3,  B4,  C3,  C4,  D3,  D4)

// Save streams to val, to avoid recomputing the groups
val streams = getChannels(input.iterator, elementsPerSample = 2, numOfChannels = 4)

// Decode each one
def demuxer = streamN(streams) _
val aa = demuxer(0)
val bb = demuxer(1)
val cc = demuxer(2)
val dd = demuxer(3)

这将使每个频道的单列流返回,无<>/em>,并掌握整个流。 如果你需要实时处理这些投入,这或许是有益的。 这里有一些投入来源,以检验它为具体内容提供的投入在多大程度上:

def source(elementsPerSample: Int, numOfChannels: Int) = Iterator.from(0).map { x =>
  "" + ( A  + x / elementsPerSample % numOfChannels).toChar +
  (x % elementsPerSample 
   + (x / (numOfChannels * elementsPerSample)) * elementsPerSample 
   + 1)
}.map { x => println("Saw "+x); x }

那么,你就能够尝试这样做:

val streams = getChannels(source(2, 4), elementsPerSample = 2, numOfChannels = 4)
def demuxer = streamN(streams) _
val cc = demuxer(2)
println(cc take 20 toList)
val bb = demuxer(1)
println(bb take 30 toList)




相关问题
Finding a class within list

I have a class (Node) which has a property of SubNodes which is a List of the Node class I have a list of Nodes (of which each Node may or may not have a list of SubNodes within itself) I need to be ...

How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

Is List<> better than DataSet for UI Layer in ASP.Net?

I want to get data from my data access layer into my business layer, then prepare it for use in my UI. So i wonder: is it better to read my data by DataReader and use it to fill a List<BLClasses&...

What is the benefit to using List<T> over IEnumerable<T>?

or the other way around? I use generic lists all the time. But I hear occasionally about IEnumerables, too, and I honestly have no clue (today) what they are for and why I should use them. So, at ...

灵活性:在滚动之前显示错误的清单

我有一份清单,在你滚动之前没有显示任何物品,然后这些物品就显示。 是否有任何人知道如何解决这一问题? 我尝试了叫人名单。

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...