Navigation

    What the Daily WTF?

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    1. Home
    2. Weave_Jester
    W
    • Profile
    • Following
    • Followers
    • Topics
    • Posts
    • Best
    • Groups

    Weave_Jester

    @Weave_Jester

    0
    Reputation
    8
    Posts
    28
    Profile views
    0
    Followers
    0
    Following
    Joined Last Online

    Weave_Jester Follow

    Best posts made by Weave_Jester

    This user hasn't posted anything yet.

    Latest posts made by Weave_Jester

    • RE: The most headache-less path to Web Development?

      @asuffield said:

      The people who create languages just aren't all that bright.

      This may be because smart people would not embark on a task so difficult and profitless as creating a language.

      Is this meant to be sarcasm? If so, it's a little too subtle for me :/

      posted in Coding Help
      W
      Weave_Jester
    • RE: Python :: game of life

      @stratos said:

      i'm really looking for comment on how i could have implemented it beter, especially on the part of python specific implementations

      This is a difficult question to answer, because your program is quite long. Instead of writing a number of paragraphs on this, I've written an alternative implementation in Python which makes more use of classes, generators and comprehensions than yours does. It has more or less the same functionality, so far as I can tell:

      neighbour_deltas = [(1, 0), (-1, 0), (1, 1), (-1, 1),
                          (0, 1), (0, -1), (-1, -1), (1, -1)]
      
      class Board:
          cell  = 'X'
          blank = ' '
      
          def __init__(self, (width, height)):
              self.content = [[self.blank for x in xrange(width)] for y in xrange(height)]
              self.width, self.height = width, height
      
          def in_bounds(self, (x, y)):
              return x >= 0 and x < self.width and y >= 0 and y < self.height
      
          def get_neighbours(self, (x, y)):
              neighbours = [(x + dx, y + dy) for dx, dy in neighbour_deltas]
              return filter(self.in_bounds, neighbours)
      
          def is_cell(self, (x, y)):
              return self.content[y][x] == self.cell
      
          def add(self, (x, y)):
              self.content[y][x] = self.cell
      
          def add_many(self, *seq):
              for pos in seq: self.add(pos)
      
          def add_predicate(self, predicate):
              for pos in self.coords():
                  if predicate(pos): self.add(pos)
      
          def remove(self, (x, y)):
              self.content[y][x] = self.blank
      
          def is_alive(self, pos):
              n = len(filter(self.is_cell, self.get_neighbours(pos)))
              return n == 3 or (self.is_cell(pos) and n >= 2 and n <= 3)
      
          def coords(self):
              for x in xrange(self.width):
                  for y in xrange(self.height):
                      yield x, y
      
          def take_turn(self):
              births, deaths = [], []
      
              for pos in self.coords():
                  if self.is_alive(pos):
                      if not self.is_cell(pos):
                          births.append(pos)
                  elif self.is_cell(pos):
                      deaths.append(pos)
      
              for cell in births: self.add(cell)
              for cell in deaths: self.remove(cell)
      
          def __repr__(self):
              return "\n".join(" ".join(cell for cell in row) for row in self.content)
      
      import random, time
      
      def construct_board():
          board = Board((50, 50))
      
          # insert a glider
          board.add_many((11, 11), (11, 12), (11, 13), (12, 11), (13, 12))
      
          # fill the wall
          board.add_predicate(lambda (x, y): x == 1 or y == 1)
      
          # put some random stuff in
          board.add_predicate(lambda _: random.randint(1, 10) == 1)
      
          return board
      
      if __name__ == '__main__':
          board = construct_board()
          print board
          raw_input("press enter")
          for i in xrange(1, 501):
              begin = time.time()
              board.take_turn()
              print time.time() - begin
              print board
              time.sleep(0.2)

      The above code uses nested lists to maintain the cells, but it's not too hard to change this to a hash. Just replace the __init__, add, remove and is_cell functions:

          def __init__(self, (width, height)):
      self.content = {}
      self.width, self.height = width, height

      def is_cell(self, position):
      return self.content[position] == self.cell

      def add(self, position):
      self.content[position] = self.cell

      def remove(self, position):
      self.content[position] = self.blank

      I thought about designing it to take an storage object of some kind, so you could use a list, dict, or whatever structure you wanted to store the data. However time was short, and it sounded a bit too much like feature creep.

      posted in Coding Help
      W
      Weave_Jester
    • RE: Why do people hate Java?

      @ammoQ said:

      I don't know Hasell, but this example seems a bit strange to me. The first parameter to "reduce" is the functor, the second parameter is the list, the third parameter is the start value... or isn't it?

      You're right, of course. Since Haskell already has a function like this (foldl), I used "reduce" as a name instead, which is what the equivalent function is called in Python. Because the start parameter is optional in Python's reduce function, it is also the final parameter. I suppose I used Python's parameter ordering out of (bad) habit.

      Of course, the whole function isn't really that good of an example, because it's not tail recursive (the last function called is f, rather than reduce). A better way to write it would be:

      reduce f s [] = s
      reduce f s x:xs = reduce f (f s x) xs

      Which is essentially the same as the definition for foldl defined in the Haskell prelude. However, despite poor execution, my point remains the same :)
       

      posted in General
      W
      Weave_Jester
    • RE: Why do people hate Java?

      @variableName said:

      Your example illustrates why first-class functions are extremely powerful and why it is unfortunate that Java does not support them, but this comparison would hold for any other functional language.  I agree with most of your assertions and just thought it might be worth pointing out that this is a defficiency of many imperative languages when compared to any functional language and is not limited to the  Java and Haskell example.

      First class functions aren't limited to functional languages. Python supports them. Ruby supports them. Perl supports them. Javascript supports them. Hell, even C# supports them via anonymous delegates, and 3.0 has some nice sugar to make the whole process rather more concise. And it's not like its a limitation of the JVM, as every other JVM-based language I can think of (Scala, Groovy, Nice, JRuby, Jython, Kawa) supports first class functions as well.

      The lack of first class functions in Java is, essentially, a choice by the designers to intentionally limit the functionality of the language.
       

      posted in General
      W
      Weave_Jester
    • RE: Why do people hate Java?

      There appear to be any number of reasons why Java is disliked, and, if I may, I'd like to add one more to the mix.

      Good programming practise involves dividing a software up into discrete pieces, such as functions, objects, modules and so forth. This technique is sometimes known as factoring, and there are many different ways this can be achieved, some better than others. The better a program is factored, the more pieces of code can be reused, and the less effort expended. Intelligently factoring software is the main thing that divides good software from bad.

      Programming languages differ on how well they can factor programs, and how easily this can be achieved. Java is near the bottom of the pile, as it provides relatively few syntactical constructs to facilitate factoring. I'll provide an example; lets say you want to write a method to find the sum of a list, and a method to find the product of a list. In Java:

      int sum(List<int> xs) {
      int total = 0;
      for (int x : xs)
      total += x;
      return total;
      }
      int product(List<int> xs) {
      int total = 1;
      for (int x : xs)
      total *= x;
      return total;
      }</int></int>

      Now, these functions are very similar, and one might think, well, is there a way of factoring out the common code? Unfortunately, you can't do this in Java without generating more code than the original solution:

      interface Functor<T> {
      T invoke(T x, T y);
      }

      int reduce<T>(List<T> xs, Functor<T> f, T start) {
      T total = start;
      for (T x : xs)
      total = f(total, x);
      return total
      }

      int sum(List<int> xs) {
      return reduce(xs, new Functor<int>() {
      invoke(int x, int y) {
      return x + y;
      }
      }, 0);
      }

      int product(List<int> xs) {
      return reduce(xs, new Functor<int>() {
      invoke(int x, int y) {
      return x * y;
      }
      }, 1);
      }</int></int></int></int></T></T></T></T>

      Now lets see what the same functionality would look like in Haskell; first the original, unfactored version:

      sum [] = 0
      sum x:xs = x + (sum xs)

      product [] = 1
      product x:xs = x * (product xs)

      Well, it's already significantly shorter than the Java version, demonstrating Java's verboseness and lack of type checking. But what observe what happens when it is refactored:

      reduce f [] s = s
      reduce f x:xs s = f (reduce f xs s) x

      sum = reduce (+) 0
      product = reduce (*) 1

      Notice that in Haskell, refactoring these functions are possible. Our sum and product functions have been reduced from two lines down to one. This example is trivial, but for more complex systems, the language constructs of Haskell can result in programs factored much more efficiently than would be possible in Java. This is why programming in Java can be a very frustrating experience, because as a good programmer, you want to avoid repeating yourself, but Java's limited syntax often makes the alternatives even more verbose and costly. Java has a large standard library, and a very optimised virtual machine, but as a language, it lacks the power that other programming languages can provide.

      And that's why I dislike programming in Java.

      posted in General
      W
      Weave_Jester