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

  • SEARCH
  • Home
  • 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 8989465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:15:58+00:00 2026-06-15T22:15:58+00:00

I am writing library which allows some function programming operations on ranges. Ranges are

  • 0

I am writing library which allows some function programming operations on ranges. Ranges are generalization of STL containers. My question is what result of folding for empty range should be?

auto r  = range(4);    // lazy numeric range {0,1,2,3}
auto r0 = range(0);    // empty range {}
vector<string> vs  {"a", "bb"};
vector<string> vs0 {};

// this is obvious and implemented part    

cout <<  (r  || add);  // 6,  || - folding op
cout <<  (r0 || add);  // 0
cout <<  (vs || add);  // "abb"
cout <<  (vs0|| add);  // ""

cout <<  (r  || mul);  // 0
cout <<  (r0 || mul);  // 1

cout <<  (r  || max);  // 3

//  What result of these should be?

cout <<  (r0 || div);   // ???
cout <<  (r0 || sub);   // ???
cout <<  (r0 || max);   // -∞ ???
cout <<  (r0 || min);   // +∞ ???
cout <<  (r0 || ???);   // result of arbitrary op? 

EDIT – ANSWER

http://en.wikipedia.org/wiki/Identity_element

  • 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-06-15T22:15:59+00:00Added an answer on June 15, 2026 at 10:15 pm

    I assume your “folders” are instances of some template, with a binary function attached and maybe an initial value.

    Traditionally, fold is defined as recursively calling said binary function on (initial, first), then (old value, next) until you run out of stuff to call it on.

    There is no such initial value such that subtraction and division work the way you might expect them to (such that fold({1,2}) is 1/2).

    Thus “folders” of subtraction and division are either “sums of inverse” and “product of inverse” (ie, fold(r) = 1/fold(r), and fold(r) = -fold(r), which seems pretty boring), or they are fundamentally different things that don’t work on empty containers.

    max and min should clearly generate the highest and lowest value for a given type, or be the folders of the second type that do not make sense on empty containers.

    By “not work”, they could throw an exception, or they could return something like a boost::optional<T> — ie, on an empty list, they do not return anything.

    Your folder type could take a function which finds the initial value for a given type, which should resolve to a traits template class or a free function (similar to std::begin).

    …

    Edit: from the comments below, an improvement to the answer.

    The real trick here is that there is no left-hand identity for subtraction and division But there is a right hand identity!

    Operations that only have a right hand identity should be expressed as right-hand folds, and operations that only have a left hand identity should be expressed as left-hand folds (aka, foldr and foldl)

    Ie, the natural way to express a fold on a list {a,b,c} with identity id of binary operation *op* is:

    ( (id *op* a) *op* b ) *op c
    

    but for operations without a left-hand identity, this doesn’t work.

    However, if you reverse the fold handedness, you get this:

    a *op* (b *op* (c *op* id))
    

    which works so long as you have a right-hand identity.

    This is important for div and sub — div has a right-hand identity of 1, and sub has a right-hand identity of 0, but neither has a left-hand identity. (there is no element e such that e-x = x for all x. There is an element e such that x-e = x for all x, namely 0).

    The same is true of exponentiation (which has a right-hand identity of 1, but no left-hand identity).

    This still doesn’t match what a naive expectation of what fold div should do. It works on lists of length 2, but on lists of length 3 something unintuitive happens. But at least it is mathematically sound. 🙂

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

Sidebar

Related Questions

I'm writing a library for procedural image generation ( Clisk ) which allows users
I am writing a library which uses a few functions from the windows user32.dll
I am writing a library which is a set of classes meant to be
I'm writing a library in PHP 5.3, the bulk of which is a class
I am writing library which wraps a lot of functions and methods from other
I am writing a C library which involves telling other computers on the subnet
I am writing a class library which abstracts data contained in XML files on
Gamemaker is a neat tool which allows for some good RAD. However it has
Background I am writing a class library assembly in C# .NET 3.5 which is
I am writing a program that will call a function from an external library,

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.