Feb. 22nd, 2009

juan_gandhi: (Default)
Actually, just YX, where Y and X are sets.
  def maps[X, Y](xs: Set[X], ys: Set[Y]) = {
    (Set(Map.empty[X,Y])/: xs) { // initially the set is empty; will append mappings for each x
      (set, x) => { // given a set of maps and an x, build a new set, where maps are defined on x
        val pairs = { for (y <- ys; map <- set) yield (map, y) } // will append x->y to each map for each y for given x
        (Set(Map.empty[X,Y]) /: pairs) {
          (newSetOfMaps, p) => {
            val map: Map[X, Y] = p._1
            val y: Y = p._2
            newSetOfMaps + (map + (x->y)) 
          }
        }
      }  
    }
  }


Looks like a little bit too much, but I hope it to be kind of self-explanatory... is it? Is it not?
juan_gandhi: (Default)
Any explanation for scala implementation of iterable concatenation?


def ++ [B >: A](that: Iterable[B]): Collection[B] = {
  val buf = new ArrayBuffer[B]
  this copyToBuffer buf
  that copyToBuffer buf
  buf
}
 


And what's the reason? Why not lazy? Why return Collection? Why not something like the following?
def ++ [B >: A](that: Iterable[B]): Iterable[B] = new Iterable[B] {
  override def elements = this.elements ++ that.elements
}

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

August 2025

S M T W T F S
      12
3456789
10 11 12 13141516
171819 20212223
2425 26272829 30
31      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 1st, 2025 01:17 am
Powered by Dreamwidth Studios