Demo entry 1790215

functional vs. imperative python

   

Submitted by ratijas on Jun 02, 2015 at 08:12
Language: Python. Code size: 3.8 kB.

if (lambda sort = lambda v: (
    lambda vec = list(v):
    lambda len = len(vec):
    lambda for_ = lambda r, f: map(f, r),
    swap = lambda i, j: map(vec.__setitem__, (i, j), (vec[j], vec[i])):
    lambda _ =
    for_(range(len), lambda i:
        for_(range(i, len), lambda j:
            vec[i] < vec[j] or swap(i, j))):
        list(vec))()()()():
        __import__("sys").stdout.write("%s\n" % sort([3,5,2,6,8]))
        )() or True:
    print "You can do more in one expression"


# this is explanations for HOW THIS HELL WORKS?! question.
# firstly, we can rewrite the code above, pretty indenting it, and
# wrapping with additional parentheses:
if (lambda sort=(
        lambda v: (
            lambda vec=list(v): (
                lambda len=len(vec):
                   lambda
                        for_=
                            (lambda r, f: map(f, r)),
                        swap=
                            (lambda i, j:
                                map(vec.__setitem__,
                                    (i, j),
                                    (vec[j], vec[i])))
                    :
                       lambda _=(
                            for_(range(len),
                                lambda i:
                                    for_(range(i, len),
                                         lambda j:
                                            (vec[i] < vec[j] or swap(i, j))
                                         )
                            )):
                                list(vec)
                        )
                    )()()()()
                ):
        __import__("sys").stdout.write("%s\n" % sort([3,5,2,6,8]))
    )() or True:
    print "You can do more in one expression"

# well, in fact those assign operations ("=") are nothing else but
# default values for lambdas.
# it definetly reminds some sort of "let" expressions (for example,
# in clojure), but with additional brought-from-hell syntax of
# additional colons (":"), invocations somewhere long way down from
# definition, and language restriction that forbid you grouping
# arguments that has defaults with pair of brackets (i.e.
# lambda (x="hello", y=42): "nice try"
# gotta bring you SyntaxError).

# now, let's rewrite that in a way without nested lambdas, so that
# all variables will be defined plainly on first level of
# indentation inside function:

def sort(v, length=len):
    vec = list(v)
    len = length(vec)  # len = len(vec) raises an UnboundLocalError
    for_=\
        (lambda r, f: map(f, r))
    swap=\
        (lambda i, j:
            map(vec.__setitem__,
                (i, j),
                (vec[j], vec[i])))

# list is changed in proccess.  no return value needed.
    for_(range(len),
             lambda i: (
                for_(range(i, len),
                     lambda j: (
                        vec[i] < vec[j] or swap(i, j)
                     )
                )
            )
        )
    return list(vec)

# futher simplifications follows:
# replacing "reversed" for_ with normal map
    map(lambda i: (
         map(lambda j: (
                vec[i] < vec[j] or swap(i, j)
             ),
             range(i, len)
         )),
         range(len))

# traditional for-loop
    for i in range(len):
        for j in range(i, len):
            if not (vec[i] < vec[j]):
                swap(i, j)

# actually, it's also okey to replace `swap` with cross-assignment:
# vec[i], vec[j] = vec[j], vec[i]
# as so, make our sort function consists completely of statements instead
# of in-line expressions.

# don't forget the magic word (=
    return list(vec)


print "simplified version:"
__import__("sys").stdout.write("%s\n" % sort([3,5,2,6,8]))

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).