Skip to content

Instantly share code, notes, and snippets.

@polarity
Last active July 10, 2024 21:18
Show Gist options
  • Save polarity/9742935 to your computer and use it in GitHub Desktop.
Save polarity/9742935 to your computer and use it in GitHub Desktop.
# Functional Programming Javascript: using Partials

Functional Programming Javascript: using Partials

Partials are basically functions that return functions with some already predefined arguments and need some arguments to be completed. Let's say you have a function with several arguments to be set, but you don't want to set the majority of arguments over and over again because you need it more than once.

# my useless function to write something to the dom
function writeSomethingToDom(element, content){
	element.append(content);
}

# use the function repeatedly  
writeSomethingToDom( $("p"), "Hallo" );
writeSomethingToDom( $("p"), "Welt" );
writeSomethingToDom( $("p"), "Hello" );

So it would be better to define the "element" argument and use the function just with the changed "content" argument. So we're gonna need a "partial" where the element is set and the function awaits just one argument to be completed. We're going to create a Partial where our function is returned with some arguments already set.

# my useless function to write something to the dom
function writeSomethingToDom(element, content){
	element.append(content);
}

# create a new partial
# retuns our writeSomethingToDom() function 
# but with the "element" argument already set.
var writeToParagraph = createPartial( writeSomethingToDom, $("p") );

# use the function repeatedly  
writeToParagraph( "Hallo" );
writeToParagraph( "Welt" );
writeToParagraph( "Hello" );

That's a functional approach to use a function repeatedly, where a subset of the arguments are the same. You can use and fulfill as many arguments as you want in the first and second step. The partial awaits always the rest of the arguments to be given. All you need is a helper function like this:

function createPartial(func /*, 0..n args */) {
	var args = Array.prototype.slice.call(arguments, 1);
	return function() {
		var allArguments = args.concat(Array.prototype.slice.call(arguments));
		return func.apply(this, allArguments);
	};
}

thx to this stackoverflow thread

I came across this paradigm because I like to use chained promises in "jQuery" / Javascript. There are so readable in my opinon, but sometimes you need to pass some arguments and that's only possible if you use ugly anonymous functions. Look at this example:

loadSomeAjaxStuff("api.php")
.then(compileJson)
.then(writeContentToDom);

If you want to give the last function some arguments with it, you can do something ugly like

loadSomeAjaxStuff("api.php")
.then(compileJson)
.then(function(){ writeContentToDom($("p")) });

or more functional like

var writeToParagraph =   createPartial( writeSomethingToDom, $("p") );
loadSomeAjaxStuff("api.php")
.then(compileJson)
.then(writeToParagraph);

or use a short name for "createPartial()" like "do()" and use it inline:

var do = createPartial;
loadSomeAjaxStuff("api.php")
.then(compileJson)
.then( do(writeSomethingToDom,$("p")) );

Also to be mentioned, partial isn't a curry. It is a similar concept, but not the same.

@naorzr
Copy link

naorzr commented Apr 26, 2018

Great explanation

@midir99
Copy link

midir99 commented Jul 10, 2020

Great gist!
I've also seen people doing partials like this:

let sayHello = (name) => () => { console.log("Hello, " + name) };
setTimeout(sayHello('George'), 3000);

@c4nzin
Copy link

c4nzin commented Jan 20, 2023

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment