News for hackers. Stuff that matters.
Re:I don't like Python. Does that make me a bad person? by zepolen on Saturday June 26, 2010 @08:12AM (#1463565)
You don't really appreciate Python until you try and program in something like Java again.

The following code is readable and I have required a variation of it in a program before:

    sorted([ord(c) for c in
        set('letters in this sentence') & set('and this one')], reverse=True)
Doing it in Java would be a chore now.
Re:I don't like Python. Does that make me a bad person? by mcav on Saturday June 26, 2010 @10:31AM (#1463720)
Here's a translation of that to Clojure, since I'm learning that now:

    (use 'clojure.set)

    (reverse (sort (map int
                        (intersection (set "letters in this sentence")
                                      (set "and this one")))))
Edit: Take two...

    (use 'clojure.set)

    (->> (intersection (set "letters in this sentence")
                       (set "and this one"))
         (map int) sort reverse)
(experienced lispers, feel free to make this more idiomatic)
Re:I don't like Python. Does that make me a bad person? by mattrepl on Saturday June 26, 2010 @12:02PM (#1463831)
Here's one using a list comprehension to make it more closely resemble the Python example.

    (require ['clojure.set :as 's])

    (-> (for [c (s/intersection (set "letters in this sentence")
                                (set "and this one"))] (int c))
        sort
        reverse)
Re:I don't like Python. Does that make me a bad person? by sreque on Saturday June 26, 2010 @03:46PM (#1464166)
Not sure why we are posting alternative language implementations, but here is the Scala version :). You could say that this was an exercise for me in learning 2.8, which I am unfamiliar with. A for comprehension isn't needed because Scala already treats Strings as a sequence of characters.

(Set("letters in this sentence":_* ) & Set("and this one":_* )).toSeq.sorted.reverse

Or you, can take advantage of Scala's rich choice of collections.

import scala.collection.immutable.TreeSet

import scala.math.Ordering._

TreeSet("letters in this sentence":_* )(Char.reverse) & Set("and this one":_* ) toSeq

The machinery Scala has in place to make the second example work is quite impressive, and I was pleased to see that it worked. Even though the intersection method returns a brand new set, using implicit arguments it correctly creates a set of the correct type (TreeSet) with the correct ordering function (Char.reverse), without any duplication of code in the standard library (like overriding '&' in every sub-class).

Re:I don't like Python. Does that make me a bad person? by wycats on Saturday June 26, 2010 @09:21PM (#1464658)
An easier Ruby solution would be:

("letters in this sentence" + "and this one").chars.sort.reverse

Re:I don't like Python. Does that make me a bad person? by sreque on Saturday June 26, 2010 @10:10PM (#1464703)
Except you aren't using sets, so you get duplicate characters.

I can just as easily write ("letters in this sentence" + "and this one").toSeq.sorted.reverse in scala. You could tag the end of your ruby statement with .uniq and you would be fine. Alternatively, you could wrap the above scala statement in TreeSet(). These solutions process things in a significantly different way than was done by the OP, though.

Re:I don't like Python. Does that make me a bad person? by wcarss on Sunday June 27, 2010 @07:34PM (#1466523)
("letters in this sentence".split(//) & "and this one".split(//)).sort.reverse.join

I'm just learning, but I think this performs it correctly with sets.

Re:I don't like Python. Does that make me a bad person? by AndyKelley on Saturday June 26, 2010 @07:51PM (#1464530)
I find the python code much more readable.
Re:I don't like Python. Does that make me a bad person? by lelele on Monday June 28, 2010 @07:35AM (#1467588)
IMO, both suck at readability, since you have to read inner expressions first. An example with a "pipe" would be clearer.
Re:I don't like Python. Does that make me a bad person? by Lewisham on Saturday June 26, 2010 @02:22PM (#1464042)
I'm not sure any decent Java developers would encourage starting a new project in Java anymore. This is exactly the sort of thing that Scala is just as good at.

Scala isn't as succinct, of course, but it gets much closer!

Re:I don't like Python. Does that make me a bad person? by sreque on Saturday June 26, 2010 @07:23PM (#1464496)
If you look at my Scala examples above, they are both more succinct than their Clojure and Python counterparts.
Re:I don't like Python. Does that make me a bad person? by xtho on Saturday June 26, 2010 @02:07PM (#1464009)
Doesn't python have sorted sets? Oh, what a pity. Does python only have one single implementation of sets? Too bad. Is the runtime behaviour of that implementation documented? Let's take a look ... I'll be back. This could take a while.
Re:I don't like Python. Does that make me a bad person? by pjscott on Saturday June 26, 2010 @03:08PM (#1464105)
Python has sets as a built-in datatype. If someone needs a different implementation, there's nothing stopping them from writing one.
Re:I don't like Python. Does that make me a bad person? by sreque on Saturday June 26, 2010 @04:43PM (#1464258)
A couple of points.

1. I love python.

2. The OP was being a troll.

3. People need to be more honest about themselves and their favorite pet languages. I can write a sorted set in assembler too, if I wanted. However, there's been several times that I've wanted a sorted set data type while coding in Python but found that none was easily available. I hate the, "you can write your own!" counterpoint, because it is essentially meaningless unless you can prove that writing your own is as trivial as importing someone else's implementation.

Re:I don't like Python. Does that make me a bad person? by pjscott on Saturday June 26, 2010 @06:19PM (#1464391)
I didn't mean to imply that lacking a sorted set datatype wasn't a pain in the ass; I was pointing out that the lack of multiple set implementations is a tradeoff that we make for all built-in datatypes that could support other operations but don't. (Personally, I would love to have the ability to pick a random element from a set. That would be great.)

By the way, in case anybody was wondering, here is a (relatively inefficient) way of making a sorted set in python:

http://code.activestate.com/recipes/576694/

Re:I don't like Python. Does that make me a bad person? by silvertab on Saturday June 26, 2010 @08:15PM (#1464561)
Couldn't you just do the following?

  from random import choice
  choice(list(myset))
(I'm guessing you'd want to do it without having to convert it to a list first?)
Re:I don't like Python. Does that make me a bad person? by pjscott on Sunday June 27, 2010 @04:32AM (#1465215)
That's what I ended up doing, but this was happening with a very large set, right in an inner loop. Luckily this was running in batch mode, so I could go get lunch or something instead of taking the time to make it faster.