Design pattern for function operation

Hi guys,

At your knowledge is there any design pattern allows to describe operation like the following.

MyFunctionObject f;
//Init f...
MyFunctionObject g;
//Init g...
MyFunctionObject h = f(g) + g;

//Numeric type declaration, complex, real, float, fixed... etc
NumericType x;
//Perform operation...
... = h(x);

I'm interested in design pattern which permits to model this kind of structure, if there's of course...

Lauke


The concept is called operator overloading:

http://en.cppreference.com/w/cpp/language/operators
I don't think "the concept" is a design pattern...
Of course it will be related to, but i need a design pattern, assuming such pattern exists.

I think it could be something like a composite pattern, but i'm looking into something more specialized for algebric operation.

Lauke
I mean... i was thinking to design a sort of library which exploit the concept of "Object function",
so i can keep some information about the function i'm computing.

> i was thinking to design a sort of library which exploit the concept of "Object function"

A library like Boost Phoenix:
http://www.boost.org/doc/libs/1_57_0/libs/phoenix/doc/html/index.html
I've just glanced at it, it seems interesting...
I'm gonna use it and let you know.

But do you think that what i'm looking for isn't related with OOP so?
This kind of stuff is FP (Functional Programming).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/operator.hpp>
#include <complex>
#include <iostream>

int main()
{
    using namespace boost::phoenix;
    using namespace boost::phoenix::arg_names;
    
    const auto cube = arg1 * arg1 * arg1 ;
    const auto square = arg1 * arg1 ;
    const auto half = arg1 / 2.0 ;
    
    const auto function = 2*cube + 3*square + half ;
    
    int n = 7 ;
    std::cout << function(n) << '\n' ; // 2 * cube(n) + 3 * square(n) + half(n) == 836.5
    
    n = 2 ;
    std::cout << function(n) << '\n' ; // 29
    
    std::cout << function(3.8) << '\n' ; 
}

http://coliru.stacked-crooked.com/a/122042b0b7f37210

The truth is, most of the FP techniques can coexist quite well with the standard object oriented and imperative programming paradigms. When we are using STL algorithms and functors (function objects) for example, we are already doing FP. Phoenix is an evolutionary next step. - Phoenix introduction

The evolutionary next step being lazy evaluation.
Geez, this is amazing!!!

I've used something similar when i attend my Artificial Intelligence course. But looking at that code it sounds really amazing!!!

Probably this stuff is what i need, so i can avoid the design xD.

I'm just puzzled a little about this stuff...
Try to see how it works..

Thank you!!!

(No design pattern i see anyway).
Topic archived. No new replies allowed.