So, I'm tasked with organizing the code and building a library with all the common code among our products.

One thing that seems to happen all the time and I wanted to abstract is posted below in pseudocode, and I don't know how to call it (different products have different domain specific implementations and names for it)

list function idk_what_to_name_it ( list list_of_callbacks, value common_parameter ): 
  list list_of_results = new list
  for_each(callback in list_of_callbacks)
  end for_each
  return list_of_results
end function

Would you call this specific construct a

list ListOfCallbacks.Map( value value_to_map) 

method or would it better be

value Value.apply(list list_of_callbacks)

I'm really curious about this kind of thing. Is there a standard guide for this stuff?

  • 2
    The correct approach is to write a map function (your example is almost a map function) and then to use a higher order function to map over your list of callbacks. – Jimmy Hoffa Oct 25 '12 at 23:00
  • our language thankfully allows us to get rid of the map function itself (not that it's a bad idea, it's just that it is closer to what I've seen this people do over and over). I think I'm going with fmap as cited in a link below; it's shorter than mapFrom (my previous choice) and sort of mnemotecnic at that – Carlos Vergara Oct 25 '12 at 23:06
  • Hi guys, finding new vocabulary here, can you point me to a (canonical) link where these "function patterns" are listed and described? Something akin to the Gang of Four's OO patterns? – Marjan Venema Oct 26 '12 at 9:36
  • Don't know what might be a canonical resource, but wikipedia entries for map, fold and filter are quite complete, with huge tables on how they are called in different languages. You'll see that vocabulary is kind of a weak point here ;) (en.wikipedia.org/wiki/Category:Higher-order_functions) – scrwtp Oct 26 '12 at 14:07

This is a map. In haskell it would be written as

map ($ param) listOfFunctions

this would take each function and execute it with the single param, returning a list of the results.

I don't recognize the language you used but I think something like this makes sense

list function map ( callback functionToMap, list list_of_callbacks ): 
  list list_of_results = new list
  for_each(callback in list_of_callbacks)
  end for_each
  return list_of_results
end function

Then you would use it as:

value function execute_with_parameter( callback function_to_execute ):
  return function_to_execute(whatever_parameter_you_want)
end function

map(execute_with_parameter, list_of_callbacks)

Mapping over a list of items means using a normal function to map over it, mapping over a list of functions means using a higher order function to map over it, but it's a map all the same.

  • map ($ param) listOfFunctions – user39685 Oct 26 '12 at 1:53
  • It is a map, but that doesn't answer the question of what to call it. – Peter Taylor Oct 26 '12 at 12:10
  • 2
    @PeterTaylor It actually answers a broader question of how to implement it, which is why it's valuable to recognize patterns by their names. If you look at my implementation you'll see a good way to solve this exact problem is to implement a standard map function, and then to call it with a higher order function, rather than making a function like the one posted which is a non-general function with only one purpose. The map function will serve many purposes, including the specific one posted. – Jimmy Hoffa Oct 26 '12 at 14:19
  • But the question he asked wasn't how to implement it. I'm not saying that anything you wrote is wrong: just that it's off-topic. – Peter Taylor Oct 26 '12 at 15:02
  • Actually, that's overstating it. It's only half-way to an answer - it hints vaguely at what the answer might be. – Peter Taylor Oct 26 '12 at 19:54

I don't know if that is the standard name (or if that is what you would really like to call it), but in Clojure, function that does this is called juxt (short of juxtaposition)

Usage: (juxt f)
       (juxt f g)
       (juxt f g h)
       (juxt f g h & fs)
Takes a set of functions and returns a fn that is the juxtaposition
of those fns.  The returned fn takes a variable number of args, and
returns a vector containing the result of applying each fn to the
args (left-to-right).
((juxt a b c) x) => [(a x) (b x) (c x)]

Source: http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/juxt

Your example is a special case of juxt with exactly one parameter (and type related stuff you mentioned, due to the implementation details).

  • and juxt is (defn juxt ([x y] (map #(% y) x))) I don't know clojure but you get the idea (unless my attempt was really bad), point being it's a map – Jimmy Hoffa Oct 25 '12 at 23:08
  • Yes, it's definitely a map, just not the most obvious application. In fact, before I discovered juxt I did exactly that. – Goran Jovic Oct 26 '12 at 8:36

It doesn't follow the classic meanings for either 'map' or 'apply'. Map calls the same function on each element from a list, and returns a list of the results. 'Apply' calls a function passing the elements of a list as function arguments. Your function is closer to 'map', but since it passes the same argument to a list of functions, instead of repeatedly calling a single function, I would give it a different name.

  • In a way the main ambiguity I find is that this can be seen as either _mapping_(think linking them with a line) a single value to list of callbacks and just equally as applying a series of operations to a given value. What'd you suggest is a better way of looking at it? – Carlos Vergara Oct 25 '12 at 22:17
  • @CarlosVergara: It seems there is no common name for this action: stackoverflow.com/questions/3401604/… – kevin cline Oct 25 '12 at 22:33
  • 1
    It is a map, it's a list of functions instead of items but it's still a map, you just have to use a higher order function to map over the list if you want to execute the functions in the list. – Jimmy Hoffa Oct 25 '12 at 22:47

You could call it an n-dimensional pointwise Cartesian product of functions. However, that doesn't make for a short name, and might require explanation...


To my mind, the word "map" implies some kind of association (e.g. a 'dictionary'), although there doesn't appear to be any association or pairing of data happening in your algorithm.

The result of your function is a generated list of values, so I would suggest that it's important that the name of your function puts a certain amount of emphasis on the end result and/or any side-effects the algorithm may have on state (It doesn't appear to have side-effects so more emphasis on the result/output).

e.g. The C++ standard library is equipped with some vaguely similar algorithms which contain the word generate - their purpose is to generate multiple data elements based on the input arguments.

  • Objection ;) The functions are the association. – Philip Oct 25 '12 at 22:40

There is no real standard for this, in fact not even a common convention. You may want to browse through APIs of programming languages and toolkits that do a lot of list juggling: Ruby, Python, Linq, Matlab, Maple, C++ STL, ... Take a look and you'll see that each program/lib has its own conventions.

In fact the mentioned languages/libs even have contradicting naming conventions:


doubled = [1,2,3].map{|x| 2*x}


var doubled = new int[] { 1, 2, 3}.Select(s => 2 * x);

(In Ruby select filters items from a list.)

Maybe you want to call your function applyMaps? This is what Maple does:

map(apply, [sin, cos, tan], Pi)


[0, -1, 0]
  • I liked that construct in that the first parameter seems to imply it's going to do [sin.apply(Pi), cos.apply(Pi), tan.apply(Pi)] or something along those lines. I think I'm going for ListOfCallbacks.mapTo(value) – Carlos Vergara Oct 25 '12 at 22:26
  • Yeah, Maple has an "interesting" Syntax... Makes sense, that's short and easily comprehensible. – Philip Oct 25 '12 at 22:30
  • (Although mapFrom would make semantically more sense IMHO. From the value to the result.) – Philip Oct 25 '12 at 22:35
  • It's widely recognized that select is just the LINQ chosen name for what is more commonly called map. The other two languages you mentioned both use the term map as well. Map is pretty common across a large swath of languages, not sure why LINQ decided to go a different route, maybe just to look more like SQL for developers. – Jimmy Hoffa Oct 25 '12 at 22:58
  • 1
    @JimmyHoffa: strictly speaking, the name select for what is commonly called map was chosen by the SQL committee. LINQ just follows SQL conventions where appropriate. – Jörg W Mittag Oct 25 '12 at 23:08

I'd call it fire_callbacks, call_callbacks, apply_callbacks, or something like that.

I've usually had such a method as part of a signal or observable object.

Not the answer you're looking for? Browse other questions tagged or ask your own question.