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 7550517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:14:02+00:00 2026-05-30T10:14:02+00:00

Are there open source C++ libraries that are similar or equivalent to the excellent

  • 0

Are there open source C++ libraries that are similar or equivalent to the excellent Functional Java library?

Specific features would include:

  • map, fold/reduce, filter, etc on iterables or the like
  • option type
  • immutable data-structure implementations

(asked out of curiosity, having been away from C++ for some years)

Yes, some of these features have traditionally been thought to require garbage collection. But with modern C++ features and libraries, has anyone started passing managed pointers through the functional transformations or something?

UPDATE
To be clear, I’m wondering the there’s something similar to Functional Java, so that the following might be typical syntax:

// assumptions:
//   * my_list is a standard library iterable of ints
//   * f is a function of int that returns a std::string
//   * p is a predicate of std::string returning bool
//   * head_opt returns an option type
stream(my_list).map(f).filter(p).head_opt.get_or_else("None")

This is the idiom that Functional Java offers, and believe me it’s really easy to get accustomed to it…

  • 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-30T10:14:03+00:00Added an answer on May 30, 2026 at 10:14 am

    As @jalf said, map and fold are already in the standard, hidden behind different names:

    • map -> std::transform, found in header <algorithm>
    • fold -> std::accumulate, found in header <numeric>

    Many more functional stuff can be found in Boost.Range, which is a pretty awesome library. Especially the range adaptors give a real functional feeling, as they create views over other ranges. With C++11, possible predicates are also easily created on-the-fly through lambdas.

    Boost.Optional might be your “option type”, depending on what exactly you mean with that.

    Immutability in C++ can be achieved by simply declaring your object const. You can avoid copies using by-reference argument passing. Truth be told, this is of course no real equivalent to true functional immutability, since immutable containers in functional languages can be copied however your want and usually just share the internal representation. After all, copy-on-write is awesome if you never write.

    On your managed pointers, I got no idea what you mean by them. In C++, you usually don’t need pointers or dynamically allocated objects at all. Just create them “on the stack”: Foo obj;.

    If you mean shared ownership, there’s std::shared_ptr. There even is a nice range adaptor if you store such a pointer in a container:

    #include <boost/range/adaptor/indirected.hpp>
    #include <boost/range/algorithm/generate.hpp>
    #include <boost/range/algorithm/copy.hpp>
    #include <vector>
    #include <memory>
    #include <algorithm>
    #include <iterator>
    #include <iostream>
    
    int main(){
      std::vector<std::shared_ptr<int>> v(5);
      int i = 0;
      boost::generate(v, [&i]{ return std::make_shared<int>(i++); });
      boost::copy(v | boost::adaptors::indirected,
          std::ostream_iterator<int>(std::cout));
    }
    

    Your specific example

    my_list.map(f).filter(p).head_opt.get_or_else("not found")

    might be implemented like this (note that std::vector is the default container in C++):

    // Warning, C++11 only!
    // Boost.Range doesn't like lambdas without this:
    #define BOOST_RESULT_OF_USE_DECLTYPE
    
    #include <vector>
    #include <string>
    #include <iterator>
    #include <iostream>
    #include <boost/optional.hpp>
    #include <boost/range/adaptor/filtered.hpp>
    #include <boost/range/adaptor/transformed.hpp>
    #include <boost/range/algorithm/generate.hpp> // only needed for filling the vector
    #include <boost/range/algorithm/copy.hpp> // only needed for printing
    
    // we need a little helper for the optional stuff
    struct head_opt_gen{} head_opt; // just a tag type
    
    template<class Range>
    auto operator|(Range const& r, head_opt_gen)
      -> boost::optional<decltype(r.front())>
    {
      if(r.empty())
        return boost::none;
      return r.front();
    }
    
    int main(){
      using namespace boost::adaptors;
      std::vector<int> v(5);
      int i = 0;
      boost::generate(v, [&]()->int{ ++i; return i*i; });
      // first, without the optional stuff
      boost::copy(v | transformed([](int x){ return std::to_string(x); })
                    | filtered([](std::string const& s){ return s.size() > 1; }),
          std::ostream_iterator<std::string>(std::cout, "\n"));
      std::cout << "=====================\n";
      // now with
      std::cout << boost::get_optional_value_or(
          v | transformed([](int x){ return std::to_string(x); })
            | filtered([](std::string const& s){ return s.size() > 2; }) // note: > 2
            | head_opt, "none");
    }
    

    Compiled with Clang 3.1 Trunk, this results in the following output:

    16
    25
    =====================
    none
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm interested if there are libraries in java, that render voxel -based sceneries? Open-Source
Are there any open-source libraries that all programmers should know about? I'm thinking something
Are there any open source libraries (any language, python/PHP preferred) that will tokenize/parse an
Are there open source libraries for Java to make implementation of drag and drop
There are few open source projects/APIs/libraries that we use in our project ( Spring,
Possible Duplicate: Good STL-like library for C Are there any open source C libraries
I found this open-source library that I want to use in my Java application.
Are there any open source libraries that I can use?
Are there any portable open source libraries that support sample-based synthesis and encapsulate producing
Possible Duplicate: Good STL-like library for C Are there any open source C libraries

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.