- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
sharing a line of code...
Sun, 2009-02-22, 02:53
def powerset[X](xs: Set[X]) = (Set(Set.empty[X]) /: xs) ((xss, x) => xss ++ xss.map(_ + x)) //;)
--
Thanks,
-Vlad
Sun, 2009-02-22, 19:37
#2
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>
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)) //;)
Tue, 2009-02-24, 06:17
#3
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!
Tue, 2009-02-24, 07:57
#4
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>
--
Thanks,
-Vlad
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
Tue, 2009-02-24, 08:07
#5
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>
--
Thanks,
-Vlad
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
Tue, 2009-02-24, 10:17
#6
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:
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
O/H Vlad Patryshev έγραψε:
> def powerset[X](xs: Set[X]) = (Set(Set.empty[X]) /: xs) ((xss, x) => xss ++ xss.map(_ + x)) //;)
>