13

Does using a function declared outside the scope of the function it is being used in violate a Functional principle like immutability? Or is that referring specifically to data like arrays, strings, etc.

For example:

var data ["cat", "dog", "bird"];

function doThing (val) { 
     return val + ", go away!" 
}

function alterData (data) {
    return data.map(doThing);
}

alterData(data);

Would the above code be acceptable? or would the "doThing" function need to be passed into the alterData function as an argument?

The reason I am confused is because in Functional Programming examples I often see functions native to the language being used without being first passed to the function. However, the examples are never complicated enough to show how one would work with a library of functions.

Regards

2
  • I've removed the "immutability" tag because it's absolutely irrelevant. Oct 30, 2015 at 16:27
  • You're right, not a good tag.
    – Michael
    Oct 30, 2015 at 16:31

3 Answers 3

3

Functional programming is no different from procedural in that regard—you write definitions that you can reuse anywhere that they are in scope. You control what's in scope where with a variety of mechanisms, for example with module definitions, module export lists and module imports. So for example (in Haskell):

module My.Module
  -- List of definitions exported from this module 
  ( doThing
  , alterData
  ) where

-- Any definitions exported from `My.Other.Module` will be in scope 
-- in this one
import My.Other.Module

-- Can't name this `data` because it's a reserved word in Haskell
yourData :: [String]
yourData = ["cat", "dog", "bird"]

doThing :: String -> String
doThing val = val ++ ", go away!" 

alterData :: [String] -> [String]
alterData strings = map doThings strings
1

TL;DR

It's fine to rely on scoping in FP code.


Immutability means that something represented by a name can't change its value. I wouldn't call it a "principle" of functional programming, though.

Anyway, this is not related to scoping at all. Passing things as arguments makes sense if you want to parametrize a function over another function - essentially making it a Higher-Order function. A good example of such is fold (also known as reduce) - but map is also one.

In your case alterData function isn't adding much value, though. mapping something over something is so common, that it's typically better to provide only the one-element function, as it's fundamentally more reusable.

If you've passed doThing to alterData, you'd make that function essentially useless; why would I use it, if I could simply use map? However, packing the operation together with the mapping can sometimes be an useful abstraction.

0

It is fine have doThing the way it is. You need to do this :

var data = ["cat", "dog", "bird"];

var doThing = function (val) { 
     return val + ", go away!" 
}

function alterData (data) {
    return data.map(doThing);
}

alterData(data);
1
  • 1
    Assuming alterData function is a pure function it is relying on doThings function to have predictable behavior. What if doThings function change its functionality or get removed? Then the behavior of alterData changes which means it is not pure. So in FP we should not rely on scoped functions and we should pass anything that function uses as its arguments. Jan 28, 2019 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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