Skip to content

Instantly share code, notes, and snippets.

@luijar
Last active June 2, 2024 12:33
Show Gist options
  • Save luijar/ce6b96f13e31cb153093 to your computer and use it in GitHub Desktop.
Save luijar/ce6b96f13e31cb153093 to your computer and use it in GitHub Desktop.
Functional Programming in JavaScript Chapter 01 - run function
/*
* Functional Programming in JavaScript
* Chapter 01
* Magical -run- function in support of Listing 1.1
* Author: Luis Atencio
*/
// -run- with two functions
var run2 = function(f, g) {
return function(x) {
return f(g(x));
};
};
// -run- with three functions
var run3 = function(f, g, h) {
return function(x) {
return f(g(h(x)));
};
};
// Test this magical function
var add1 = function(x) {return x + 1;};
var mult2 = function(x) {return x * 2;};
var square = function(x) {return x * x;};
var negate = function(x) {return -x;};
var double = run2(add1, add1);
console.log(double(2)); //-> 4
var testRun = run3(negate, square, mult2);
console.log(testRun(2)); //->-16
@NoahDavidATL
Copy link

And since run is just an alias for compose, you can also use this:

function compose = ( ...fns ) => fns.reduce( ( f, g ) => ( ...args ) => f( g( ...args ) ) );

Usage: compose(fn3, fn2, fn1);

@mg901
Copy link

mg901 commented Aug 30, 2018

const compose = (...fns) => x => fns.reduceRight((acc, fn) => fn(acc), x);

@vanholler
Copy link

Great =) Thanks so much, from Russia )))) I was bye your book yesterday and i want say what "Functional Programming" is really cool ))))

@anhilde
Copy link

anhilde commented Jan 14, 2019

Hi, it is quite frustrating reading the book with examples that are lacking the code. E.g. how about providing code for this example end to end:

var printMessage = run(addToDom('msg'), h1, echo);
printMessage('Hello World');

I have made an attempt and get a result, but I don't think I went the right way. If the book provides these snippets making use of "magic functions" as you call them, they should be available for lookup based on the examples. The code here is making use of a different set of examples. Particularly how to make the h1 function work with run is something Id like to know. I can only make it work using a different parameter order such as:

run2(echo, h1);

If you use examples you do not explain, the books use is very limited for someone converting from imperative languages to the functional approach because the trap of falling back into old habits catches one out a lot. At least for me this is true and I don't like to continue reading if I have open issues already in the first set of examples. Obviously for people migrating from some other functional language to functional javascript, that may be trivial stuff.

@anhilde
Copy link

anhilde commented Jan 14, 2019

Just for completion of my rant above, here is my solution with reversed arguments:

const compose = (...fns) => x => fns.reduceRight((acc, fn) => fn(acc), x);

var echoDocument = document.writeln.bind(document);

var h1 = function (text) { 
    return "<h1>" + text + "</h1>"; 
}

var printMessage = compose(echoDocument, h1);
printMessage("Hello World");

@shinancao
Copy link

shinancao commented Mar 28, 2019

When I first time saw this, I was also very confused as @anhilde mentioned:

var printMessage = run(addToDom('msg'), h1, echo); printMessage('Hello World');

After trying several times, here is my solution:

function addToDom(elementId) { return function(content) { document.querySelector(#${elementId}).innerHTML = content } }

function h1(message) { return '<h1>' + message + '</h1>' }

function echo(message) { return message }

var run = function(f, g, h) { return function(x) { return f(g(h(x))) } }

var printMessage = run(addToDom('msg'), h1, echo) printMessage('Hello World')

@Gordon9
Copy link

Gordon9 commented Nov 25, 2020

emmm...where is run-func details

@TangoPJ
Copy link

TangoPJ commented Dec 17, 2020

emmm...where is run-func details

Scroll up the mouse wheel 5 times, it's right there, lol

@PachVerb
Copy link

JavaScript functional programming chapter one. Examples, run(console.log, repeat(3),h2, echo). I can't think solutions, maybe, there are good idea?

@PachVerb
Copy link

PachVerb commented Jun 1, 2021

And since run is just an alias for compose, you can also use this:

function compose = ( ...fns ) => fns.reduce( ( f, g ) => ( ...args ) => f( g( ...args ) ) );

Usage: compose(fn3, fn2, fn1);

this run can't not do anying?

@PachVerb
Copy link

PachVerb commented Jun 1, 2021

And since run is just an alias for compose, you can also use this:

function compose = ( ...fns ) => fns.reduce( ( f, g ) => ( ...args ) => f( g( ...args ) ) );

Usage: compose(fn3, fn2, fn1);

maybe this

const compose =  ( ...fns ) => fns.reduce( ( f, g ) => ( ...args ) => f( g( ...args ) ) );

@Linhieng
Copy link

In Listing 1.4, the curry is not given, So I found this code below

const curry = function(f) {
    const var_num = f.length
    return function(...args) {
      if (args.length >= var_num) {
        return f.apply(null, args)
      }
      return function(...new_args) {
        return f.apply(null, args.concat(new_args))
      }
    }
  }

Hope I can help someone who is a beginner like me, good luck!

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