4

I see this pattern everywhere in my code, and in libraries, yet there appears to be no name or abstraction of it that I can find anywhere.

Example (pseudocode)

T foo( T x, void f(T&) )
{
    T y = x;
    f( y );
    return y;
}

Basically: Take a value, and a function that transforms that value. Make of a copy of the value, transform it, and return it.

Real-life examples (C++)

T operator+(const T& x, const T& y)
{
    T z = x; // Make a copy
    operator+=(z, y); // Modify in place
    return z;
}

Vector3 Vector3::normalized() const
{
    Vector3 x = *this; // Make a copy
    x.normalize(); // Modify in place
    return x;
}

T sorted(T const& x)
{
    T y = x; // Make a copy (yeah, yeah, could have passed by value)
    sort( y ); // Modify in place
    return y;
}

Basically, you have an in place function (with side-effects) and make an out-of-place function (without side-effects) out of it.

Is there a name for this pattern? Do you know of any libraries or languages that use it? Obviously functional languages won't use it because they don't have referentially opaque functions to begin with.

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
  • This sure sounds a lot like the K combinator – SingleNegationElimination Nov 16 '10 at 08:37
  • It's similar, but not the same. The key point here is that `f` is an *in-situ* algorithm, i.e. `f(x)` *mutates* `x` -- it doesn't return anything. – Peter Alexander Nov 16 '10 at 09:15
  • 5
    Most likely there is no name for this since theoretical functional programming does not have side effects. In other words, this transform FP unfriendly functions into REAL functions (note: REAL functions don't eat quiche) – slebetman Nov 16 '10 at 10:17
  • Where are the higher order functions you are talking about? – leppie Nov 16 '10 at 12:23
  • @slebetman: Things only have names in they are in FP? :) – Peter Alexander Nov 16 '10 at 12:47
  • @leppie: foo takes a function as an argument, so it is a higher order function by definition. – Peter Alexander Nov 16 '10 at 12:48
  • @Peter Alexander: Ah ok, but none of the real world examples does. – leppie Nov 16 '10 at 13:06
  • @Peter: FP is closely linked to lambda calculus (in fact there is a paper somewhere out there that basically says that lisp is an instance of lambda calculus) and lambda calculus is where all the function names come from. – slebetman Nov 16 '10 at 13:54
  • @slebetman: *All* function names come form lambda calculus? So what your saying is that patterns can only have names if they are in lambda calculus? If you're not saying that then I don't see your point. This is a pattern in non-functional languages, so I don't see why its absence from FP means that it isn't allowed a name. – Peter Alexander Nov 19 '10 at 10:14
  • @Peter: I said function name, not design pattern name. I personally haven't met any function, not design pattern, being given a name outside of the math and FP community. Design patterns on the other hand are primarily named by the OOP community. – slebetman Nov 19 '10 at 14:18
  • And to be clear, the 'all' in my previous answer was more an exaggeration than literal. Though, as I mentioned, haven't met any function being given a formal name outside of the math or FP community. Besides, the OP asked about the name of a higher order function, not design pattern. – slebetman Nov 19 '10 at 14:24
  • There most probably isn't a name for it, because it isn't a higher order function (it has side effects - it is not a function). In OOP there is a metamessage term for messages carrying other messages, but copying is not very natural in OOP and I don't know about any metamessage patterns anyway. If we forget about mutation, we get some sort of the apply function. – Gabriel Ščerbák Jun 25 '11 at 03:04

1 Answers1

1

It's actually what in mathematics and FP is called a composition, because you could express it as mystery_function(x, fun) = fun(copy(x)) instead.

In Design Patterns linguo, it's a wrapper, that wraps the function call with a copy. So I would rather naturally call it a copy wrapper. But I never saw it classified anywhere.

Nowhere man
  • 5,007
  • 3
  • 28
  • 44