Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7179271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:06:25+00:00 2026-05-28T17:06:25+00:00

I have numerous reusable functions, all with the same signature (they take a record

  • 0

I have numerous reusable functions, all with the same signature (they take a record and return a float). I often need to combine functions into a new function.

Let’s say I want to create a function that takes a record, applies f to it, and if the result is negative converts it to zero. I have two ways of doing that: composition and function modification. What are the pros and cons of each approach?

Composition:

def non_negative(value):
    return max(0, value)

g = compose(non_negative, f)

# from functional module by Collin Winter
def compose(func_1, func_2, unpack=False):
    """
    compose(func_1, func_2, unpack=False) -> function

    The function returned by compose is a composition of func_1 and func_2.
    That is, compose(func_1, func_2)(5) == func_1(func_2(5))
    """
    if not callable(func_1):
        raise TypeError("First argument to compose must be callable")
    if not callable(func_2):
        raise TypeError("Second argument to compose must be callable")

    if unpack:
        def composition(*args, **kwargs):
            return func_1(*func_2(*args, **kwargs))
    else:
        def composition(*args, **kwargs):
            return func_1(func_2(*args, **kwargs))
    return composition

Modification:

def non_negative(func):
    def new_func(record):
        return max(0, func(record))
    return new_func

g = non_negative(f)    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:06:27+00:00Added an answer on May 28, 2026 at 5:06 pm

    Assuming compose is available in a library, then I would prefer that style for this example.

    The main reason is that it separates out the concerns of clamping a value to non-negative values and feeding the result of one function to another. In the “modification” style, if you ever find yourself wanting to run non_negative on a value rather than on the result of a function, you’ll end up with contortions like non_negative(lambda x: x)(value). And you need to write a separate function for every thing function you might want to compose, every single one of which contains the composition logic mixed in with the code for that function.

    In this example, the burden is trivial whichever way you do it. But generally, if it’s this easy to make small independent pieces that you then glue together to make your composite code, that’s the good way to do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have numerous functions (unknown at design time) that each take a specific number
I have seen numerous ways to highlight the current tab, but they all have
I have numerous projects that all contain the exact same boilerplate code inside of
We have numerous python classes that do not seem to need __init__ , initialising
I have numerous buttons on my page with the same class names. However these
I have numerous text files of data in the form of float numbers. I'm
I have numerous XML files that are organized into different directories. I need to
I have numerous events that are all day events, and all start at the
I have numerous activites in my Android app., and most should contain the same,
I have numerous Corba servers and some other java apps (not web). I wish

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.