This page is no longer maintained — Please continue to the home page at www.scala-lang.org

sharing a line of code...

6 replies
vpatryshev
Joined: 2009-02-16,
User offline. Last seen 1 year 24 weeks ago.
def powerset[X](xs: Set[X]) = (Set(Set.empty[X]) /: xs) ((xss, x) => xss ++ xss.map(_ + x)) //;)

--
Thanks,
-Vlad
ounos
Joined: 2008-12-29,
User offline. Last seen 3 years 44 weeks ago.
Re: sharing a line of code...

O/H Vlad Patryshev έγραψε:
> def powerset[X](xs: Set[X]) = (Set(Set.empty[X]) /: xs) ((xss, x) => xss ++ xss.map(_ + x)) //;)
>

vpatryshev
Joined: 2009-02-16,
User offline. Last seen 1 year 24 weeks ago.
Re: sharing a line of code...
See, my code is rather a joke; of course, performancewise, one must have some kind of Iterable-based (that is, enumerable) lazy set,
And I would suggest not to mix Seq and Set - two very different entities.

2009/2/22 Dimitris Andreou <jim.andreou@gmail.com>
O/H Vlad Patryshev έγραψε:
def powerset[X](xs: Set[X]) = (Set(Set.empty[X]) /: xs) ((xss, x) => xss ++ xss.map(_ + x)) //;)
 
J Robert Ray
Joined: 2008-12-18,
User offline. Last seen 3 years 16 weeks ago.
Re: sharing a line of code...

2009/2/21 Vlad Patryshev :
> def powerset[X](xs: Set[X]) = (Set(Set.empty[X]) /: xs) ((xss, x) => xss ++
> xss.map(_ + x)) //;)

It may have been a joke, but it helped me realize how I can express
some code I have using a fold.

Up to now I have always thought of a fold as returning a single value.
But I wanted to combine elements of a list only if they meet some
condition, producing a new list.

scala> (List[Char]('A','A','A','B','A','A') :\ List[Char]())((x, xs)
=> xs match { case y :: ys if x == y => xs ; case _ => x :: xs })
res34: List[Char] = List(A, B, A)

So, thanks!

vpatryshev
Joined: 2009-02-16,
User offline. Last seen 1 year 24 weeks ago.
Re: sharing a line of code...
Thanks for appreciating it. Yes, it was just a principal idea, not an efficient one. Actually, I have also found a fold solution for set exponents, Y^X; will post it in another mail.

2009/2/23 J Robert Ray <jrobertray@gmail.com>
2009/2/21 Vlad Patryshev <vpatryshev@gmail.com>:
> def powerset[X](xs: Set[X]) = (Set(Set.empty[X]) /: xs) ((xss, x) => xss ++
> xss.map(_ + x)) //;)

It may have been a joke, but it helped me realize how I can express
some code I have using a fold.

Up to now I have always thought of a fold as returning a single value.
But I wanted to combine elements of a list only if they meet some
condition, producing a new list.

scala> (List[Char]('A','A','A','B','A','A') :\ List[Char]())((x, xs)
=> xs match { case y :: ys if x == y => xs ; case _ => x :: xs })
res34: List[Char] = List(A, B, A)

So, thanks!



--
Thanks,
-Vlad
vpatryshev
Joined: 2009-02-16,
User offline. Last seen 1 year 24 weeks ago.
Re: sharing a line of code...
Yes, and the kind of code you write is very typical for what people write using map-reduce at Google.

2009/2/23 J Robert Ray <jrobertray@gmail.com>
2009/2/21 Vlad Patryshev <vpatryshev@gmail.com>:
> def powerset[X](xs: Set[X]) = (Set(Set.empty[X]) /: xs) ((xss, x) => xss ++
> xss.map(_ + x)) //;)

It may have been a joke, but it helped me realize how I can express
some code I have using a fold.

Up to now I have always thought of a fold as returning a single value.
But I wanted to combine elements of a list only if they meet some
condition, producing a new list.

scala> (List[Char]('A','A','A','B','A','A') :\ List[Char]())((x, xs)
=> xs match { case y :: ys if x == y => xs ; case _ => x :: xs })
res34: List[Char] = List(A, B, A)

So, thanks!



--
Thanks,
-Vlad
manojo
Joined: 2008-12-22,
User offline. Last seen 3 years 3 weeks ago.
Re: sharing a line of code...
www.scala-kurz.org displays other (almost) one-liners. There are a few good ones, such as the quicksort.

Manohar

On Mon, Feb 23, 2009 at 10:53 PM, Vlad Patryshev <vpatryshev@gmail.com> wrote:
Yes, and the kind of code you write is very typical for what people write using map-reduce at Google.

2009/2/23 J Robert Ray <jrobertray@gmail.com>
2009/2/21 Vlad Patryshev <vpatryshev@gmail.com>:
> def powerset[X](xs: Set[X]) = (Set(Set.empty[X]) /: xs) ((xss, x) => xss ++
> xss.map(_ + x)) //;)

It may have been a joke, but it helped me realize how I can express
some code I have using a fold.

Up to now I have always thought of a fold as returning a single value.
But I wanted to combine elements of a list only if they meet some
condition, producing a new list.

scala> (List[Char]('A','A','A','B','A','A') :\ List[Char]())((x, xs)
=> xs match { case y :: ys if x == y => xs ; case _ => x :: xs })
res34: List[Char] = List(A, B, A)

So, thanks!



--
Thanks,
-Vlad

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland