Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/64j0/poc-cpp-functional

A POC showing how one can leverage C++ functional features to: partial application, higher order functions.
https://github.com/64j0/poc-cpp-functional

cpp functional-programming

Last synced: 2 days ago
JSON representation

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

Awesome Lists containing this project

README

        

#+TITLE: POC C++ partial application
#+DATE: [2024-08-20 ter]

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 [[https://numpy.org/doc/stable/reference/generated/numpy.linspace.html][numpy.linespace]] function along with some helpers to print
to the console the result and the time it took to run our linespace version.

#+BEGIN_SRC bash
# 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])
#+END_SRC

Some useful links:

- https://habr.com/en/articles/436488/
- https://en.cppreference.com/w/cpp/utility/functional/bind
- https://learn.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp?view=msvc-170#capture-clause

** Using nix

Inspired by [[https://nixcademy.com/posts/cpp-with-nix-in-2023-part-1-shell/][link]].

#+BEGIN_SRC bash
nix develop
#+END_SRC

*It does not work properly with C++ 23. For example, I can't use the [[https://en.cppreference.com/w/cpp/header/print][print]]
library header.