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

Pages: 1-

Closures are Fucking Awesome!

Name: Anonymous 2012-11-17 2:29

function newfibs()
    fibtable = {[0]=0, 1}
    function fibs (n)
        if fibtable[n] then
            return fibtable[n]
        else
            fibtable[n] = fibs(n -1) + fibs(n - 2)
            return fibtable[n]
        end
    end
    return fibs
end


> fibs=newfibs()
print(fibs(5))
5
print(fibs(20))
6765
print(fibs(50)) -- calculates instantaneously, unlike a naive recursive implementation
12586269025


I wrote this in like 30 seconds. A C equivalent that memoized previously calculated values in the Fibonacci sequence would have taken me several minutes to write. How did I ever get by without closures and tables?

Name: Anonymous 2012-11-17 3:54

>>1

Yeap, they sure are. But here, have some more generality.


-- f: (type_of(f), k) -> v
function memoizer(f)
  local tb = {}
  local mt = {
    __call = function(self,k)
      local memo_v = self[k]
      if memo_v ~= nil then
        return memo_v
      else
        local v = f(self, k)
        self[k] = v
        return v
      end
    end
  }
  setmetatable(tb, mt)
  return tb
end


function fibs(f, n)
  if n == 1 or n == 2 then
    return 1
  else
    return f(n - 1) + f(n - 2)
  end
end


slow_fibs = function(n) return fibs(slow_fibs, n) end

memo_fibs = memoizer(fibs)

print(memo_fibs(30))
print(memo_fibs(30))
print(slow_fibs(30))
print(slow_fibs(30))

Name: Anonymous 2012-11-17 4:29

still not general enough, i cannot simply make an existing function memoized by adding a single line of code (i'd need to rename the base function, add the memoized function, and change the tests). needs more function decorators.

Name: Anonymous 2012-11-17 11:32

end end end end end end
Terrible!

Name: Anonymous 2012-11-17 11:48

Not really.

Dynamic scope > static scope

Name: Anonymous 2012-11-17 12:00

>implying these are better than javascript's closures

Name: Anonymous 2012-11-17 12:04

>>6
Wait, so the Javashit kike comes from /g/? That would explain the pathetic quality of his ``posts''.

Name: Anonymous 2012-11-17 12:05

>>1
fibtable and fibs aren't local

back to /g/, ``please''!

Name: Anonymous 2012-11-17 12:53

You can implement this on C in 30 seconds too.

I'd be more worried about the fiblist than the closure, though.

Name: Anonymous 2012-11-17 13:05

>>3

Before:

function fibs(n)
  if n == 1 or n == 2 then
    return 1
  else
    return fibs(n - 1) + fibs(n - 2)
  end
end


After:

fibs = memoizer(function(fibs, n)
  if n == 1 or n == 2 then
    return 1
  else
    return fibs(n - 1) + fibs(n - 2)
  end
end)

Name: Anonymous 2012-11-17 13:32

How did I ever get by without closures and tables?
I guess by not writing a fib() every day? Oh wait... am I still on /prog/?

Name: Anonymous 2012-11-17 14:55

A C equivalent that memoized previously calculated values in the Fibonacci sequence would have taken me several minutes to write.
Why would you tell us that you are a bad programmer?

Name: Anonymous 2012-11-17 15:30

(%-.-*:)t.
Recursive fibs is wasteful.

Name: Anonymous 2012-11-17 16:41

static int fib_r (int p, int h, int i) {
  switch (i) {
  case 0:  return p;
  case 1:  return h;
  case 2:  return p + h;
  default: return fib_r(h, p+h, i-1);
  }
}

int fib (int n) {
  return fib_r(1, 1, n);
}

Name: Anonymous 2012-11-17 18:59

CLOSURE ANUS

Name: Anonymous 2012-11-17 21:47

fib=let x=1:zipWith(+)(0:x)x in(x!!)

Name: Anonymous 2012-11-17 22:05

>>16
Now with more loeb!

import Control.Applicative

loeb :: Functor f => f (f a -> a) -> f a
loeb x = ($ loeb x) <$> x

fib :: Int -> Integer
fib = (fibs!!)
  where fibs = loeb $ const 0 : const 1 : map (\n xs -> xs !! (n - 2) + xs !! (n - 1)) [2..]

Name: Anonymous 2012-11-17 22:46

what does !! do?

Name: Anonymous 2012-11-17 22:59

>>18
It returns the element at the given index, so ['a','b','c'] !! 1 = 'b'. Also, (fibs !!) is a partial application - it's a function that given x, returns fibs !! x.

Name: Anonymous 2012-11-17 23:57

Thank you!

Name: Anonymous 2012-11-18 6:00

>>19
I love function composition but writing it always feels like I'm about to do something incredibly inefficient.

Name: Anonymous 2012-11-18 9:28

>>17
why are you using applicative?

Name: Anonymous 2012-11-18 10:06

>>22
I imported it for (<$>). Yeah, I realize now that all I needed was Data.Functor, but I've gotten in the habit of importing Applicative whenever I'm using functors.

Name: Anonymous 2012-11-18 12:23

Writing this efficiently without specialized language support is CS101 - Algorithms and Programming I.

Name: Anonymous 2012-11-18 14:32

Terrible!

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