tricks

12
Stupid Programming Tricks (Part 1)

Upload: brett-carter

Post on 24-Jun-2015

375 views

Category:

Technology


4 download

DESCRIPTION

Stupid Programming Tricks, a talk I gave at the Portland Python Meetup 4/10/2012

TRANSCRIPT

Page 1: Tricks

Stupid Programming Tricks (Part 1)

Page 2: Tricks

Stupid Programming Tricks

Some programming tricks are so cool yet utterly inane. Let's talk about some stupid things....

Page 3: Tricks
Page 4: Tricks

The Y-Combinator

You may be asking yourself: What the hell is Y-Combinator?

Page 5: Tricks

First some definitions...

● Functions that can be passed as values● The calculus of function composition● Most importantly, functions can take other

functions

Page 6: Tricks

Some terminology

λx.x == lambda x: xfunctional - a function that takes other functions, and returns a functionCombinator - a function that takes functionals (functions that take other functions) and returns a function

Page 7: Tricks

Fixed points

Fixed points of a function are any values for x such that f(x) = x. Consider f(x) = x^2 - it's fixed points are 0, 1. If we can compute the fixed points of a function, why not a functional?

Page 8: Tricks

Fixed points

What would the fixed point of a functional look like? f(g) = g where g is a function. The result would be a function not a value.

Page 9: Tricks

Y-Combinator

The Y combinator simply computes the fixed point of a function such that:Y(F) = F(Y(F)) But what does this mean?

Page 10: Tricks

Magic.

The Y-Combinator can compute the fixed point of a recursive function without using recursion. Y(F) = F(Y(F)) is pretty useless in most languages. Expanding we get:Y = λf.(λx.f (x x)) (λx.f (x x)) Note there's no longer a reference to Y on the RHS.

Page 11: Tricks

Show me the code.

Let's look at a python example....

Page 12: Tricks

Cool.

The Y-Combinator is pretty cool but ultimately pretty impractical.Questions?

[email protected]://gist.github.com/2348175

@zbskii