-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This repository is part of a larger project!
Currying is a technique which is named after the mathematician and logician Haskell Curry.
By dividing a function`s multiple parameters into another function(s) could be called currying. An example shall now follow:
/*
This function will be curried
function A(x , y , z)
{
return x * y * z;
}
*/
//The curried version of function "A"
function A(x)
{
return function B(y)
{
return function C(z)
{
return x * y * z;
}
}
}
//Outputs "4", because "1 * 2 * 2 = 4"
console.log(A(1)(2)(2));
//Another way to call
var a = A(1);
var b = a(2);
var c = b(2);
//Outputs "4"
console.log(c);
This should work because of closures fact two. That means here that the paramter of A is known to B and C and the parameter of B is known to C.
Currying could also be useful when some of the parameters of a function are already known and due this they are defined beforehand. If so then it seems using Javascript`s bind method is recommended. Here follows an example:
function Album(genre, title)
{
return genre + ": " + title;
}
var List = ["Title 1", "Title 2", "Title 3"];
//Receiver is "null", because in this example
//there is no need to bind the function to any object
var musicList = List.map(Album.bind(null, "Music"));
/*
Outputs:
["Music: Title1", "Music: Title2", "Music: Title3"]
*/
console.log(musicList);
The parameter genre in Album could be useful when there is more than one list with titles. Every list could be its own genre in that case (e.g. Video, Article, Book).
The user interaction part could look like the content as seen below by starting "index.html" in a web browser and interacting with its features.
-
🅱️ utton "RANDOM(Good Luck!)" generates for every input an own youtube video ID -
The youtube IDs could be given manually if wanted
-
🅱️ utton "GO" uses the youtube ID to show the found results by the system
To use the project just download the files and execute "index.html". Note that all files(folder "wiki_images" excluded) should be placed in the same folder so that the functionality of the code is guaranteed.
This knowledge was gained:
- Effective JavaScript "68 Specific Ways to Harness the Power of JavaScript" by David Herman
Sources:
-
Javascript`s method map by MDN Web docs
-
Will YouTube Ever Run Out Of Video IDs? by Tom Scott
-
The Sad Tragedy of Micro-Optimization Theater written by Jeff Atwood