11
V2EX  ›  Scala

Scala 怎么做如下变换:(A,B,C) => (A,(B,C)) (B,(A,C)) (C,(A,B))

  •  
  •   11 · Nov 2, 2014 · 7695 views
    This topic created in 4234 days ago, the information mentioned may be changed or developed.

    (A,B,C) => (A,(B,C)) 这一步好说,直接取 head tail 就可以了。但是取每一个元素都做这个操作怎么搞?

    1 replies    2014-11-24 14:36:19 +08:00
    nil3
        1
    nil3  
       Nov 24, 2014
    scala> val a = Set(1, 2, 3)
    a: scala.collection.immutable.Set[Int] = Set(1, 2, 3)

    scala> a map (x => Set(x, a diff Set(x)))
    res: scala.collection.immutable.Set[scala.collection.immutable.Set[Any]] = Set(Set(1, Set(2, 3)), Set(2, Set(1, 3)), Set(3, Set(1, 2)))

    或者

    scala> for (x <- a) yield Set(x, a diff Set(x))
    res: scala.collection.immutable.Set[scala.collection.immutable.Set[Any]] = Set(Set(1, Set(2, 3)), Set(2, Set(1, 3)), Set(3, Set(1, 2)))
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1501 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 16:51 · PVG 00:51 · LAX 09:51 · JFK 12:51
    ♥ Do have faith in what you're doing.