0

I've decided to teach myself functional programming.

I've submitted all of my assignments whilst maintaining the rules of functional programming, and my instructor is totally fine with it. To grab inputs, I've been doing something like this:

var getWidth = function(){
  return prompt("What is the width?");
};

This works fine but it could be something simpler, like:

var getWidth = prompt("What is the Width?");

The problem with this, is when I call the function getWidth(), it simply runs the function once and assigns it to the getWidth variable. But that's not what I want, I want it to run prompt("What is the Width?") every time getWidth() is called. I've tried to search for it, but I'm not really entirely sure how to phrase it. Google's useful, if you know how to use it.

Ucenna
  • 177
  • 1
  • 7
  • 3
    That's the difference between *defining* a function and *calling* a function. – Barmar Sep 29 '16 at 00:33
  • Checkout this answer. This might be of some help. [Can you alter a Javascript function after declaring it?](http://stackoverflow.com/questions/2136522/can-you-alter-a-javascript-function-after-declaring-it) – Random Integer Sep 29 '16 at 00:36
  • Well, in functional programming with pure functions there would not be much of a difference, but **`prompt` is not pure**. Therefore it matters how often you call it or not. – Bergi Sep 29 '16 at 01:09
  • @Bergi Didn't know that, I'll have to look at the individual functions more in depth. At the moment I'm just learning, so I'll forgive myself for now. – Ucenna Sep 29 '16 at 01:46

4 Answers4

1

Well you could use bind()

var hm = window.prompt.bind(window, "simpler?");
console.log(hm());
epascarello
  • 204,599
  • 20
  • 195
  • 236
1

it could be something simpler, like:

You cannot "simplify" a function

var a = function onePlusTwo() {
  return 1 + 2;
};

by writing

var a = 1 + 2;

The two do entirely different things.

The first defines a function (meaning, does not execute it), and assigns the function to the variable a. The function can then be called with a(), which will evaluate to 3, since that is the return value of the function:

alert(a() * 2)  // 6

The second (var a = 1 + 2;) merely executes the code which you had in the body of the function. Executing it assigns the result of 1 + 2 to the variable a. This code is "re-usable" only by copying and pasting it elsewhere in your program.

So if you want to define functionality and re-use it, you have no choice but to define it as a function. That's what functions are for!

If you want to take a function and make another function you can use to call that function later, with or without some arguments, then as mentioned in another answer, you can use bind, as in var a = prompt.bind(window, "What is the width?");, and then call it with a(). But this is essentially the same as writing the function out in the way you did in the first example, and is a little bit less readable.

By the way, none of this is "functional programming". Functional programming does not mean just using functions; all programs do that. Functional programming refers to a style of programming involving manipulating, combining, dynamically creating, and passing around functions.

  • Okey dokey. Thanks. Now that I think about it, just making getWidth a string and passing it to prompt would work fine for my purposes. I am aware that it's not function, that was mostly just background. I've done lot's of manipulating with functions, though dynamic creating is new to me. I'll have to look into it. – Ucenna Sep 29 '16 at 03:46
0

You cannot simplify your function this way. By the first method:

var getWidth = function(){
  return prompt("What is the width?");
}; 

You are returning an executable function, and assigned the function reference to a variable getWidth. However in the second one:

var getWidth = prompt("What is the Width?");

You are returning the result of the function window.prompt.

Sparkmorry
  • 119
  • 9
0

What the OP might be looking for, is the lambda functional shorthand. It's a really simple bit of code, that let's you right functions much shorter. It's usually, used for anonymous functions, but it could easily be re-purposed here. For example:

function getWidth(){
   return prompt("What is the width?");
}

Would be reduced to:

var getWidth() = () => prompt("What is the width?");

Note that the value of prompt is returned automatically, so you don't need to add return to your code.

Parameters could also be specified if desired, like this:

var getWidth = (x) => prompt(x);

And if you use arguments this way, then the () are optional. This would work too:

var getWidth() = x => prompt(x);
Ucenna
  • 177
  • 1
  • 7