Headline

simple functional programming in Language:Python

Motivation

This illustrates simple functional programming in Python. Using this style mostly reduces the code length and is more expressive although Language:Python doesnt offer full support for functional programming.

Illustration

The company model is implemented by named tuples. One can instantiate the data the same way as using classes.

Company = namedtuple('Company', ['name', 'departments'])
Employee = namedtuple('Employee', ['name', 'salary'])
Department = namedtuple('Department', ['name', 'manager', 'subunits'])

For the implementation of cut new objects have to be created because named tuples are immutable.

def cut(company):

    def cutSubunit(s):

        def cutEmployee(employee):
            return Employee(employee.name, employee.salary/2.0)

        def cutDepartment(department):
            return Department(department.name, cutEmployee(department.manager), map(cutSubunit, department.subunits))

        return cutEmployee(s) if isinstance(s, Employee) else cutDepartment(s)

    return Company(company.name, map(cutSubunit, company.departments))   

Usage

Download the source and run python operations.py


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.