hom(X, Y) in scala
Feb. 22nd, 2009 12:47 pmActually, just YX, where Y and X are sets.
Looks like a little bit too much, but I hope it to be kind of self-explanatory... is it? Is it not?
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?