Preface:
in order to have clean and effective code, i want to use external functions in my mapreduce mongo script.
Problem:
given we have following map function (coffeescript syntax):
map: ->
key = foo(@field)
emit(key, value)
Calling an external function 'foo' raise an error
➜ rake mongo:mapreduce
MongoDB shell version: 2.0.5
connecting to: localhost:27017/connect_development
{
"assertion" : "map invoke failed: JS Error: ReferenceError: foo is not defined nofile_b:2",
"assertionCode" : 9014,
"errmsg" : "db assertion failure",
"ok" : 0
}
The same we will return for reduce context call.
Bad smell decision - self-called anonymous function:
map: ->
key = ( (field)->
# some business logic
)(@field)
emit(key, value)
Self-called anonymous function could be very big and not effective to test and may cause leaking memory (not sure on this).
How to resolve this problem?
UPD:
When i said "external function", meant function declared in same file (in same class) with "map/reduce" functions. Of Course, it's invoked on server-side.