0

I am interested in functional programming, so I decided to try this approach on the scripting environment of my Google Sheets file, and you know that the scripting language is Google Apps Script, which is basically javascript. And it even supports some (if not all) ES6 syntax.

The problem is that I cannot directly run any code, for example:

let a = 4;
Logger.log(a);

I mean, I cannot run it globally, I need to define a function with any name, and then place the code inside that function, and I can run the function, so the function runs the code inside.

So, maybe you ask, "why this behavior makes a problem writing pure functional code?" well, because, as I know, two of the most important factors about pure functions are:

1) We must not use global variables/functions inside a function, instead we must pass as parameters (and then as arguments of course).

2) defining function inside a function is often not very good idea in terms of readability and organization of the code.

So, I want to define more functions (to do some stuff), not just one "main" function, and I could not find any way, any single way to write code (as a whole) without violating at least one of the two statements above.

So, I mean, I can not write anything without making at least one non-pure function.

leodevbro
  • 113
  • 1
  • 9
  • If you ever want to be able to *do* something useful with the code (eg, log a message, change a cell, etc), you won't be able to write in a purely functional way. Side-effects are required at least in *certain points* for a script to be able to accomplish anything. – CertainPerformance Apr 20 '20 at 09:05
  • 2
    *2) defining function inside a function is often not very good idea in terms of readability and organization of the code.* Not at all, this is quite common, and often nearly essential, at least in Javascript. – CertainPerformance Apr 20 '20 at 09:06
  • In FP you can use Monads to model effects. Effects and side-effects are different things. You can find plenty of material in the internet about Monads and Effects. – Vüsal Apr 20 '20 at 09:11
  • 1
    Your 1st assumption is partially incorrect. A function must not depend on globale, mutable variables, but it may depend on global constants and global pure functions. However, often you rather want to pass a function dependency as an argument to obtain a more general higher order function. Your 2nd assumption is merely opinion based. –  Apr 20 '20 at 09:28
  • Your second assumption violates principle of least access. The function doesn't need to be globally accessible, if you use it only inside one function – TheMaster Apr 20 '20 at 11:58

1 Answers1

0

As a user explained in the comments:

Your 1st assumption is partially incorrect. A function must not depend on global, mutable variables, but it may depend on global constants and global pure functions. However, often you rather want to pass a function dependency as an argument to obtain a more general higher order function. Your 2nd assumption is merely opinion based.

So you could, for example, define a main function to run your code as a whole while defining functions inside the main function to achieve functional programming with Apps Script.

Andres Duarte
  • 3,166
  • 1
  • 7
  • 14
  • I think I have found the solution. I can use default parameters for functions, so the functions will grab another function as arguments, and not with global call. For example: ` function dog() { return 4; } function readDog(d = dog) { Logger.log(d()); } ` – leodevbro Apr 25 '20 at 10:10