scala查找列表列表的所有组合

时间:2016-03-31 13:57:24

标签: scala

如何用列表替换项目并展平结果?

示例:

将3替换为List(a,b)

输入:Gen.choose

输出:

static member RelativeUrl_1() =
  gen {
    // let! length = Arb.generate<int> |> Gen.suchThat (fun l -> l > 0 && l <= 10)
    let! length = Gen.choose (1, 10)
    let! list = Gen.listOfLength length <| Generators.Alphanumeric()
    return String.Join ("/", list)
  }

输入:List(1, 2, 3, 4)

输出:

List(List(1, 2, a, 4)
    List(1, 2, b, 4))

1 个答案:

答案 0 :(得分:0)

这是:

def replacePermutations[A](l: Seq[A], replace: A, by: Seq[A]) = {
  val byPermutationsWithRepetitions =
    (by map (e => by.indices map (_ => e))) ++ by.permutations

  byPermutationsWithRepetitions map { by =>
    val i = Iterator.from(0)
    l map { e =>
      if (e == replace) by(i.next() % by.size)
      else e
    }
  }
}

干杯

相关问题