val a = (1, 2)
val b = (3, 4, 5)
I would like to create a single list of lists:
( (1,3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5) )
Is there a simple/elegant way to do this in scala?
Bill
 /**
   * a view of an n*m-combination of all elements, probably Ordered[_]
like this: t1a -> t2a, t1a -> t2b, t1a -> t2c, t1b -> t2a, t1b -> t2b
and so on
   */
  def *[T2](other: Traversable[T2]): Traversable[(T, T2)] = {
    t.view.flatMap(e => other.map(e -> _))
That's not a zip, but rather a cartesian product. You can create it using a for comprehension:
scala> for (x <- List(1,2); y <- List(3,4,5)) yield (x,y)    
res1: List[(Int, Int)] = List((1,3), (1,4), (1,5), (2,3), (2,4), (2,5))
Nate
import scalaz._
a <|*|> b
If Haskell helps:
> let (<|*|>) = liftA2 (,) [1, 2] <|*|> [3, 4, 5]
[(1,3),(1,4),(1,5),(2,3),(2,4),(2,5)]
- -- 
Tony Morris
http://tmorris.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAk1JutkACgkQmnpgrYe6r63DVwCgzepOy2v9eq1jKwoQvsFW17DL
YpMAoKLsz/cnmkWpMGA6mVqTfT9mKlC2
=Lr5s
-----END PGP SIGNATURE-----
Wow, what a wealth of options, thanks everyone.
I've applied this to my problem, and have now run into the problem
where I want to apply a function to each member of the resulting list.
 I would like to write the function to take distinct parameters,
rather than a list:
    def foo(protocol: String, emailAddress: String) {
        println("Proto " + protocol + ": " + emailAddress)
    }
    def main(av: Array[String]) {
        val protocols = List("NPV", "XML", "SOAP", "GROD")
        val emailAddresses = List("ema...@pp.com", "ema...@pp.com",
                 "ema...@pp.com", "ema...@pp.com", "ema...@pp.com",
                 "ema...@pp.com", "ema...@pp.com", "ema...@pp.com",
                 "ema...@pp.com")
        val args = protocols.map(x => emailAddresses.map((x, _)))
        args.foreach(foo)
But, the compiler does not like this.  Is there any way to map things
to a function in this way?
Sorry for the newbie ignorance.
Bill
Bill
import scala.io.Source
object gl {
def main(args : Array[String]) {
if(args.length != 1)
println("Usage : scala gl <FileName>")
else
for(ligne <- Source.fromFile(args(0)).getLines)
println(ligne.stripLineEnd)
}
}
>        val args = protocols.map(x => emailAddresses.map((x, _)))
>        args.foreach(foo)
>
> But, the compiler does not like this.  Is there any way to map things
> to a function in this way?
These should be
    val args = protocols.flatMap(x => emailAddresses.map((x, _))
    args.foreach(x => foo(x._1, x._2))
If I were you, I would go for
for (x <- protocols; y <- emailAddresses) yield foo(x, y)
- Mahmood
I agree. Here is my final code example for our Java team:
object It {
    def foo(protocol: String, emailAddress: String) {
        println("Proto " + protocol + ": " + emailAddress)
    }
    def main(av: Array[String]) {
        val protocols = List("NPV", "XML", "SOAP", "GROD")
        val emailAddresses = List("ema...@pp.com", "ema...@pp.com",
                 "ema...@pp.com", "ema...@pp.com", "ema...@pp.com",
                 "ema...@pp.com", "ema...@pp.com", "ema...@pp.com",
                 "ema...@pp.com")
        for (x <- protocols; y <- emailAddresses) yield foo(x, y)
    }
}
Which I think will be relatively easy for our Java developers to grok.
Bill
/davidB
Hello,
How should I configure Scala to accept non-Ascii characters?
For instance I get an error message when I place comments in my soucre file containing accented characters (like é, è, à...)
And when reading a text file like here:
import scala.io.Source
object gl {
def main(args : Array[String]) {
if(args.length != 1)
println("Usage : scala gl <FileName>")
else
for(ligne <- Source.fromFile(args(0)).getLines)
println(ligne.stripLineEnd)
}
}
does'nt work if the text file contains diacritics...
Thx for the help
I think I've made a mistake and gotten away from what I originally was
trying for here.  I need to separate out the data generation part from
the part that calls the 'foo' method, so my code should really be
something of the sort:
object It {
    def foo(protocol: String, emailAddress: String) {
        println("Proto " + protocol + ": " + emailAddress)
    }
def gen() = {
        val protocols = List("NPV", "XML", "SOAP", "GROD")
        val emailAddresses = List("ema...@pp.com", "ema...@pp.com",
                 "ema...@pp.com", "ema...@pp.com", "ema...@pp.com",
                 "ema...@pp.com", "ema...@pp.com", "ema...@pp.com",
                 "ema...@pp.com")
        for (x <- protocols; y <- emailAddresses) yield (x, y)
    }
    def main(av: Array[String]) = {
        val x = gen()
        x.foreach(???)
    }
}
Here, I'd like to keep the signature of foo() as it is, easily
comprehensible.  Changing it to:
> I think I've made a mistake and gotten away from what I originally was
> trying for here.  I need to separate out the data generation part from
> the part that calls the 'foo' method, so my code should really be
> something of the sort:
>
> object It {
> [...]
>    def main(av: Array[String]) = {
>        val x = gen()
>        x.foreach(???)
>    }
> }
>
> Here, I'd like to keep the signature of foo() as it is, easily
> comprehensible.
As one of the answers indicated, `(x, y)` type is a Tuple2[X, Y]
instance which can be accessed using the ._1 and ._2 accessors, so you
can do:
    val x = gen()
    x.foreach(i => foo(i._1, i._2))
You can also still use the for comprehension, which is IMHO a bit more readable:
    val x = gen()
    for ((prot, email) <- x) { foo(prot, email) }
- Mahmood
Thanks to everyone else also for helping out.
Bill
f => a => b => for(aa <- a; bb <- b) yield f(aa)(bb)
also:
a => b => for(aa <- a; bb <- b) yield (aa, bb)
(which would call the former)
Bill Lear schrieb:
> I have two lists:
> 
> val a = (1, 2)
> val b = (3, 4, 5)
> 
> I would like to create a single list of lists:
I have a solution for an arbitrary number of lists of arbitrary numbers
of elements.
def xproduct (xx: List [List[_]]) : List [List[_]] =
  xx match {
    case aa :: bb :: Nil =>
      aa.map (a => bb.map (b => List (a, b))).flatten		
    case aa :: bb :: cc =>
      xproduct (bb :: cc).map (li => aa.map (a => a :: li)).flatten
    case _ => xx
}
which works for a List of two List of Something too.
- --
Tsch���--->...Stefan
- ---------------------------
Don't visit my homepage at:
http://home.arcor-online.net/hirnstrom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iEYEARECAAYFAk1J+D4ACgkQQeATqGpDnRoNEwCglZ8uQsZ5mC2VoEnxqvymePqt
x3cAoIdXX/53wFvRzyJmt6nwDIIsmDLr
=ZRO3
-----END PGP SIGNATURE-----
On 02/02/2011 22:31, alain silovy wrote:
> How should I configure Scala to accept non-Ascii characters?
I think you should use UTF-8 in your source, it is the default.
For runtime, you can use the -Dfile.encoding setting.
[EDIT] Sending again, shortcutting Gmane, but it seems to have been updated. Am I the only 
one to use NNTP to read these lists?
-- 
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --