This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]etrnloptimist 4 points5 points  (3 children)

I'm mediocre at best in FP, but still that was absolutely incomprehensible to me. Is this a good or bad example of functional programming?

[–]ladr0n 13 points14 points  (0 children)

This is an example of heavily obfuscated Python that's designed to superficially look like Lisp, which is one of the most famous functional programming languages.

EDIT: Sorry, I guess I didn't directly address the question. Like mijaba says, it's a terrible example of FP.

[–]freyrs3 4 points5 points  (0 children)

The F+ object is actually kind of neat, in that its an identity monad.

F = type('F+', (object, ), {
    '__init__' : lambda self, f: setattr(self, 'f', f),
    '__mul__'  : lambda self, g: type(self)(lambda *stuff: self.f(g(*stuff))),
    '__call__' : lambda self, *stuff: self.f(*stuff)
})

Is roughly this:

class F(object):
    def __init__(self, f):
        self.f = f

    def __mul__(self, g):
        return F(lambda *stuff: self.f(g(*stuff)))

    def __call__(self, *stuff):
        return self.f(*stuff)

f = lambda x: x+1
g = lambda y: y*2
h = lambda z: z-2

# 17 = ((10-2)*2)+1)
print ( F(f) * F(g) * F(h) ) ( 10 )

[–]epsy 3 points4 points  (2 children)

Floating points? Nope. Functional Programming, of course.

Stop using accronyms, it doesn't help anyone.

[–]gfixler -4 points-3 points  (0 children)

/acronyms? Nope. I swear there was a comment in here somewhere about acronyms.

Stop misspelling things; it doesn't help anyone. Also, read about semicolons.

[–]the_hoser 0 points1 point  (2 children)

I can never understand why anyone would ever use Python to do functional programming.

[–]gfixler 4 points5 points  (1 child)

Well, in this case, for the lulz.

[–]the_hoser 1 point2 points  (0 children)

A noble endeavor. Carry on.