GithubHelp home page GithubHelp logo

erezsh / lambdax Goto Github PK

View Code? Open in Web Editor NEW
36.0 3.0 6.0 8 KB

Partial evaluation for Python with syntactic sugar

Home Page: http://erezsh.wordpress.com/2008/11/01/fun-while-avoiding-lambda/

Python 100.00%

lambdax's Introduction

X is a composition class. It's a lambda replacement which is shorter, pickle-able, and cooler.

X has two main features:

  1. It acts as an identity function ( so X(object) == object, etc. )
  2. When performing operations on it, it returns a new class that acts as a corresponding function.

So, evaluating (X+2) will result in an instance of X, that whenever called with an argument, will return that argument plus 2:

>>> map( X+2, [1, 2, 3] )
[3, 4, 5]

>>> pow = X**X
>>> pow(2,8)
256

>>> filter( X>0, [5, -3, 2, -1, 0, 13] )
[5, 2, 13]

>>> l = ["oh", "brave", "new", "world"]
>>> sorted(l,key=X[-1])
['world', 'brave', 'oh', 'new']

These operations can be chained and composed:

>>> map(2**(X+1), range(10))
[2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]

>>> sqr = X ** 2
>>> sqr_sum = sqr + sqr
>>> sqr_sum(3, 4)
25

>>> sec_per_min = 60 * X
>>> sec_per_hour = 60 * sec_per_min
>>> sec_per_day = 24 * sec_per_hour
>>> sec_per_day(1)
86400

>>> head, tail = X[0], X[1:]
>>> (head(tail))([1,2,3])
2

Another feature is SameX, which is experimental (like everything here ;)

>>> map(X**SameX, range(10))
[1, 1, 4, 27, 256, 3125, 46656, 823543, 16777216, 387420489]

>>> (X+SameX+SameX)('ha')
'hahaha'

>>> (X + ' ' + SameX[::-1])('hello!')
'hello! !olleh'

Some operations aren't as smooth:

>>> map( X.upper._(), "use underscore for calling a function".split() )
['USE', 'UNDERSCORE', 'FOR', 'CALLING', 'A', 'FUNCTION']
>>> filter( X.in_(range(10)), range(4,20,2))
[4, 6, 8]

License

MIT

lambdax's People

Contributors

erezsh avatar fawers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.