/poc-cpp-functional

A POC showing how one can leverage C++ functional features to: partial application, higher order functions.

Primary LanguageC++

POC C++ partial application

This repository holds a POC code showing how one can use partial applications in C++, along with a higher order function to compute the time it took to run another functions.

It does implement the numpy.linespace function along with some helpers to print to the console the result and the time it took to run our linespace version.

# I'm using g++ 11.4.0
g++ -O2 -o program.exe main.cpp

# Then execute
./program.exe
# Sample result:
# ------------------------------------------------------------

# Time: 462ns
# array([0, 0, 0, 0, 0])

# Partial application with curied lambda functions
# Time: 259ns
# array([0, 0, 0, 0, 0])

# Partial application with std::bind
# Time: 329ns
# array([0, 0, 0, 0, 0])
# ------------------------------------------------------------

# Time: 173ns
# array([0, 2, 4, 6])

# Partial application with curied lambda functions
# Time: 130ns
# array([0, 2, 4, 6])

# Partial application with std::bind
# Time: 136ns
# array([0, 2, 4, 6])
# ------------------------------------------------------------

# Time: 108ns
# array([0, 1.5, 3, 4.5])

# Partial application with curied lambda functions
# Time: 139ns
# array([0, 1.5, 3, 4.5])

# Partial application with std::bind
# Time: 166ns
# array([0, 1.5, 3, 4.5])
# ------------------------------------------------------------

# Time: 245ns
# array([0, 0.444444, 0.888889, 1.33333, 1.77778, 2.22222, 2.66667, 3.11111, 3.55556, 4])

# Partial application with curied lambda functions
# Time: 224ns
# array([0, 0.444444, 0.888889, 1.33333, 1.77778, 2.22222, 2.66667, 3.11111, 3.55556, 4])

# Partial application with std::bind
# Time: 265ns
# array([0, 0.444444, 0.888889, 1.33333, 1.77778, 2.22222, 2.66667, 3.11111, 3.55556, 4])
# ------------------------------------------------------------

# Time: 192ns
# array([0, 0.4, 0.8, 1.2, 1.6, 2, 2.4, 2.8, 3.2, 3.6])

# Partial application with curied lambda functions
# Time: 168ns
# array([0, 0.4, 0.8, 1.2, 1.6, 2, 2.4, 2.8, 3.2, 3.6])

# Partial application with std::bind
# Time: 212ns
# array([0, 0.4, 0.8, 1.2, 1.6, 2, 2.4, 2.8, 3.2, 3.6])
# ------------------------------------------------------------

# Time: 140ns
# array([2, 2.2, 2.4, 2.6, 2.8])

# Partial application with curied lambda functions
# Time: 127ns
# array([2, 2.2, 2.4, 2.6, 2.8])

# Partial application with std::bind
# Time: 175ns
# array([2, 2.2, 2.4, 2.6, 2.8])
# ------------------------------------------------------------

# Time: 131ns
# array([2, 2.25, 2.5, 2.75, 3])

# Partial application with curied lambda functions
# Time: 130ns
# array([2, 2.25, 2.5, 2.75, 3])

# Partial application with std::bind
# Time: 168ns
# array([2, 2.25, 2.5, 2.75, 3])
# ------------------------------------------------------------

# Time: 149ns
# array([3, 2.75, 2.5, 2.25, 2])

# Partial application with curied lambda functions
# Time: 132ns
# array([3, 2.75, 2.5, 2.25, 2])

# Partial application with std::bind
# Time: 166ns
# array([3, 2.75, 2.5, 2.25, 2])

Some useful links:

Using nix

Inspired by link.

nix develop

*It does not work properly with C++ 23. For example, I can’t use the print library header.