faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 6 people (83%) answered Yes
Recently 4 of 5 people (80%) answered Yes

Entry

Caml-like functions in Python

Jul 5th, 2000 10:03
Nathan Wallace, Hans Nowak, Snippet 310, Python Snippet Support Team


"""
Packages: maths
"""
# caml.py
"""Some Caml-like functions for Python."""
# A derivative function.
def _deriv(f, x, dx):
    return (f(x+dx) - f(x)) / dx
def deriv(f, dx):
    return lambda x, f=f, dx=dx: _deriv(f, x, dx)
# Compose a function from two others.
def compose(f, g):
	return lambda x, f=f, g=g: f(g(x))
if __name__ == "__main__":
    from math import *
    def square(x): return x * x
    cos2 = compose(cos, square)
    print cos2(45)
    cosh = deriv(sin, 1e-6)
    print cosh(pi)