Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

post bits of code you like

Name: Anonymous 2005-08-02 1:52

i shall start:

open(F,"find -type f|");
while (<F>) {
 $_ = substr($_,2,-1);
 print "$_\n" if $v;
 push @{$dups{substr(`md5sum -b "$_"`,0,31)}}, $_;
}
close(F);

for every file in a directory it checksums the file, then adds the checksum as a key to a hash, the value being an array reference. the filename is then added to the array.

so basically the arrays have a list of files with the same checksum. i use this to find duplicates files.

Name: Anonymous 2005-11-08 22:34

For god's sake, put some hard returns in your code!  It doesn't have to be on one line!  And C does not suck!

Name: Anonymous 2005-11-09 6:32

>>41
That's a single expression :)

Name: Anonymous 2005-11-09 20:01

>>39
That's what I call witty programming. If Python source is as structured as it seems, and everything sounds to be so clean, yet it's still powerful enough to do that, then you've given me another reason to learn Python.

And the differente with Perl would be that, while you can be obfuscated in both, Perl's syntax is crappy even when you do not intend to obfuscate your code, and most Perl code I saw was a nightmare.

Name: Anonymous 2005-11-10 20:54

>>43
you are just looking for any reason to say python is grate and perl makes you irate. gb2/python.org

Name: Anonymous 2005-11-11 2:45 (sage)

>>44 speaks truth, even if he can't spell.

Name: Anonymous 2005-11-12 15:59

I feel the need to point out that unless the destination is a directory on 4chan there should not be a / anywhere

gb2python.org

fixed thus

Name: Anonymous 2005-11-14 8:05 (sage)

>>46
gb2/pedant

Name: Anonymous 2005-11-18 9:56

(define (fib n)
  ((lambda (f n)
     (f f n))
   (lambda (f n)
     (if (< n 2)
       1
       (+ (f f (- n 1)) (f f (- n 2)))))
   n))

Name: Anonymous 2005-11-18 10:32

>>48
Ugh. Lisp syntax. Or abscense of. Or whatever it is, it just sucks. I would bother to learn Lisp if it had a sane syntax. (Silly me thinking (< n 2) is highly illogical.)

Name: Anonymous 2005-11-18 10:36

Not impressed.

fib = 1 : 1 : zipWith (+) fib (tail fib)

Name: Anonymous 2005-11-18 11:50

>>48
f has 1 argument when defining and two when applying? what's going on here?

Name: Anonymous 2005-11-18 12:28

I'm no lisper, but here's my take.

We're defining a function called fib, which takes an argument n.

The body of fib is made up of the application of an anonymous function, which takes two arguments, a function f and a number n. This anonymous function applies the function f it was passed to f and n.

The last argument to the anonymous function is n, the same n that fib was passed. The first argument is another anonymous function, which it too takes two arguments, a function f and a number n. If n < 2 then it returns 1, otherwise it applies the function f it was passed to f and n-1, and adds that to the application of f and n-2.

One wonders if this doesn't have exponential time complexity. One also questions the sanity of lispers.

Name: Anonymous 2005-11-18 13:17

a function taking itself as an argument to recurse. so he goes to all that trouble to not name a function...

Name: Anonymous 2005-11-18 15:42

One wonders if this doesn't have exponential time complexity.
It does. I hate it when people define fib recursively.

One also questions the sanity of lispers.
(no u)

Name: Anonymous 2005-11-18 15:49

You see the Haskell code for fib in 50? It's recursive but still has linear complexity. Infinite lists recursively defining themselves are a beautiful thing.

Name: Anonymous 2005-11-18 16:13

>>55
There's a difference between defining something tail-recursively (which is equivalent to iteration, but sexier) and recursing down two branches of a tree with no good reason.

If you wanna see the scheme code that does the job properly instead of that mess Anonymous posted above I'll be happy to post it.

Name: Anonymous 2005-11-18 16:23

I do, though I suspect it'll be nothing more than the use of an accumulator variable.

Name: Anonymous 2005-11-18 16:37

Not an accumulator variable, that would require assignment which is a bad thing in FP.

  (define (loop i fib_i fib_i-1)
    (if (= i x) fib_i
        (loop (+ i 1) (+ fib_i fib_i-1) fib_i)))
  (if (< x 1) 1
      (loop 1 1 1)))

It's obvious that Haskell wins at fib (assuming your function was defined correctly, I can't say I know how to read Haskell.)

Name: Anonymous 2005-11-18 16:41

Ah nuts, I fail at copy/pasting. Let's try that again, this time with the first line included:
(define (fib x)
  (define (loop i fib_i fib_i-1)
    (if (= i x) fib_i
        (loop (+ i 1) (+ fib_i fib_i-1) fib_i)))
  (if (< x 1) 1
      (loop 1 1 1)))

It's also fairly obvious how to convert this into a form that returns the whole series rather than just the value at a given point in the series.

Name: Anonymous 2005-11-18 21:18

>>53
It's not too different from obfuscating C or Perl code, only the former is a "Good Thing" and later is frowned upon.

Name: Anonymous 2005-11-29 2:00

everytime i see lisp i feel like i'm high

Name: Anonymous 2005-11-30 17:22

:(){ :|:& };:

Name: Anonymous 2005-11-30 17:54

>>62
That's Perl for a multitasking OS if I'm not mistaken

Name: Anonymous 2005-11-30 20:18

>>63
bash

Name: Anonymous 2005-12-01 6:27

>>63
Execute it, you'll like it.

kekeke

Name: Anonymous 2005-12-01 9:49

I don't know looks familiar but under the headline "kernel buffer overflow" or something like that

Name: Anonymous 2005-12-01 10:59

>>62
Fascinating... I first tried it through Perl because I thought it might be some bug with the parser but it wasn't, then I moved onto Bash and it started spawning hundreds and hundreds of processes... Care to explain what it does? (I don't know Bash script.)

It wasn't a problem though, because I run Windows. I don't have a completely retarded kill program, I just pskill bash and everything solved, oh and the system didn't seem to care much.

Name: Anonymous 2005-12-01 15:36

I moved onto Bash and it started spawning hundreds and hundreds of processes... Care to explain what it does?
The answer is in the question: it spawns hundreds and hundreds of processes.

It's a forkbomb, designed to punish those who haven't implemented the basic protection of having a limit on the number of processes that can be running at the same time. If you don't have a limit, your computer becomes unresponsive within seconds forcing you to reboot.

Name: Anonymous 2005-12-02 6:28

Really? I won't try it under Linux, but my Windows machine did not become unresponsive enough to prevent me from killing the thing; I tried it three times because it rocked, and last time I saw it had spawned some 8000 processes. I just went to Start+Run, killed them, and resumed normal operation immediately. It did me warn me about not having enough virtual memory for all that shit (I have a very small swap file, it expands as I need and I rarely need so that's why), but the new processes didn't really take RAM because they do nothing; I think the whole thing was some 50 MB extra.

Name: Anonymous 2005-12-04 1:05

seriously can someone explain how >>62 works bash scripting is insane

Name: Anonymous 2005-12-05 13:03

>>70
:(){ :|:& };:

:(){ ... } -- defines a function called :

Name: Anonymous 2005-12-05 13:05

>>70
:(){ :|:& };:

:(){ ... } -- defines a function called :

:|:& -- executes : and pipes it to itself. & makes the process go into the background.

;

: -- executes


translation of sorts  f(){ f|f& };f

Name: mizukami 2005-12-06 1:30

i don't exactly _like_ this code, but it compiles, which is a miracle enough in itself...yes, this is java.

I apologize in advance if this is a repost.


package A;

class A
{
   A A(A A)
   {
      A:
        for (;;)
        {
             if (A.A(A) == A) break A;
        }
        return A;
   }
}

Name: Anonymous 2005-12-06 11:08

>>73
I have seen this before somewhere, but I'm not sure where.

Name: Anonymous 2005-12-06 13:51

>>74
it's everywhere. Big news everyone: Java has more than one namespace!

Name: Vicks007 !V7GsiU/wa. 2005-12-15 19:19

Ruby is the most beautiful language I have ever had the privilege to use.  Below is a wonderful little unbalanced binary tree, complete with an iterator for its traversal.  This makes a call such as <span class="code">for aChild in thisRoot ... end</span> possible.  Here you are.

<div class="code">
class Node
  attr_accessor :left, :right, :data
  def traverse
    @left.traverse {|node| yield node } unless @left.nil?
    yield self
    @right.traverse {|node| yield node } unless @right.nil?
  end
end
</div>

As for Fibonacci, you can do some nice versions of that.  Consider this:
<div class="code">
def fibone(n)
  a, b = 0, 1
  (n-1).times do
    yield a if block_given?
    a, b = b, a+b
  end
  return b
end
</div>
In addition to returning the Fibonacci number of your choice, this is also an iterator over the Fibonacci numbers.  You could do something like <span class="code">fib(100) {|n| puts n}</span> to print out all of the fibonacci numbers from 1 to 100.

And if you want to calculate the Fibonacci numbers quickly without having to worry about machine precision, you'd be hard pressed to beat the following gem in terms of time complexity:

<div class="code">
$fibcache = Hash.new
def fib(n)
  $fibcache[0], $fibcache[1] = 0, 1
  return fib_re(n)
end

def fib_re(n)
  if !($fibcache[n].nil?)
    return $fibcache[n]
  elsif (n.modulo(2) == 0)
    fnn = fib_re((n/2.0).to_i)
    fnm = fib_re(((n/2.0)-1).to_i)
    ans = (((2 * fnm) + fnn)*fnn).to_i
  else
    fnn = fib_re((n/2.0).ceil)
    fnm = fib_re((n/2.0).floor)
    ans =  ((fnn**2)+ (fnm**2)).to_i
  end
  $fibcache[n]=ans
  return ans
end
</div>
This uses a method outlined by Djikstra for the calculation of Fibonacci number that takes advantage of the fact that the nth Fibonacci number can be calculated using fib(n/2) and fib((n/2)-1) (assuming n is even, else you need fib((n+1)/2) and fib((n-1)/2)).  This algorithm is O(log(n)) time and memory. 

In conclusion, Ruby rules.

Name: Anonymous 2005-12-15 19:35

I've seen a lot of C programmers who didn't realize you can define data inline. Not that it's a good programming practise.

// PRINT D
char c = [3]"ABCDEFG"

// PRINT 4
typedef struct { int sandnigger; int cracker } blame;
printf("%d\n", (blame)[1]{3, 4} );

Name: Anonymous 2005-12-15 19:36

Reposting code from above post with proper tags.  Apparently, I fail at XHTML in Shiichan.

<code>for aChild in thisRoot ... end
</code>

<code>
class Node
  attr_accessor :left, :right, :data
  def traverse
    @left.traverse {|node| yield node } unless @left.nil?
    yield self
    @right.traverse {|node| yield node } unless @right.nil?
  end
end
</code>


<code>
def fibone(n)
  a, b = 0, 1
  (n-1).times do
    yield a if block_given?
    a, b = b, a+b
  end
  return b
end
</code>

<code>fib(100) {|n| puts n}
</code>

<code>
$fibcache = Hash.new
def fib(n)
  $fibcache[0], $fibcache[1] = 0, 1
  return fib_re(n)
end

def fib_re(n)
  if !($fibcache[n].nil?)
    return $fibcache[n]
  elsif (n.modulo(2) == 0)
    fnn = fib_re((n/2.0).to_i)
    fnm = fib_re(((n/2.0)-1).to_i)
    ans = (((2 * fnm) + fnn)*fnn).to_i
  else
    fnn = fib_re((n/2.0).ceil)
    fnm = fib_re((n/2.0).floor)
    ans =  ((fnn**2)+ (fnm**2)).to_i
  end
  $fibcache[n]=ans
  return ans
end
</code>

Name: Anonymous 2005-12-15 19:37

>>78
I give up.  Still, the Ruby works...

Name: Anonymous 2005-12-15 19:57

That's because it uses BBCode...

(I'll gladly add XHTML if I find a good sanitizer library that won't let people post <xmp> or something)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List