Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Partial application is syntactic sugar over lambdas. For example, say you have a print_with_color function:

    print_with_color(color.RED, level.INFO, "some message")
But it's to long to type, each time you want to print a message, so you define some wrappers:

    info=lambda m: print_with_color(color.GRAY, level.INFO, m)
    error=lambda m: print_with_color(color.RED, level.ERROR, m)
    warning=lambda m: print_with_color(color.YELLOW, level.WARNING, m)
    info('hi im an info')
Which works, but it is perhaps nicer to use partial:

    info=partial(print_with_color, color.GRAY, level.INFO)
    error=partial(print_with_color, color.RED, level.ERROR)
    warning=partial(print_with_color, color.YELLOW, level.WARNING)
    info('hi im an info')
You could also use it on objects:

    std=partial(stdlog.log, level.INFO)
    std('hi')
    err=partial(errlog.log, level.ERROR)
    err('bad')
Partial application saves you more in languages with syntactic support for them. There api producers design their functions with partial application in mind, unlike python where you often wish for an rpartial function because you want to partially apply from "the other side."


Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: