2

I'm looking to lower my overhead on code like this

foo(bar(baz("hello"))) // function hell

ideally something like this

var fbb = bind(foo, bar, baz)
foo("hello")

Does this exist? Native or library?

I looked through underscore and bind.

3
  • What's wrong with just doing: function fbb(s) { return foo(bar(baz(s))); } Commented Dec 2, 2014 at 9:16
  • MooTools has a nice function chaining, but not very clear to me still what you need/look for. Commented Dec 2, 2014 at 9:18
  • This idea may be a breakthrough in computing. Quick, someone alert the functional programming guys. Commented Dec 2, 2014 at 11:47

3 Answers 3

1

Underscore has the compose function which will do what you want:

var composite = _.compose(foo, bar, baz);

composite('hello');

function foo(a1){
  return 'foo' + a1;
}		

function bar(a2){
  return 'bar' + a2;
}

function baz(a3){
  return 'baz' + a3;
}

alert(foo(bar(baz("hello"))));

var composite = _.compose(foo, bar, baz);

alert( composite('hello') );
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

1
function getCaller(first) {
    var rest = Array.prototype.slice.call(arguments, 1);
    return function (value) {
        return rest.reduce(function (previous, next) {
            return next(previous);
        }, first(value));
    };
}

function foo(string) {
    return string + ' world!';
}

function bar(string) {
    return string + ' Hi';
}

function baz(string) {
    return string + ' Mom!';
}

var caller = getCaller(foo, bar, baz);
console.log(caller('Hello'));
// Prints: Hello world! Hi Mom!

2 Comments

Hey! Thanks so much for this! I was wondering if there's any way to accept multiple arguments for the last function? I know I used strings in the example above but my real-life example looks something more like await(Promise.promisify(fs.copy(newIconPath, appIconPath))) thoughts?
Functions only return one value so that makes your requirement illogical. However, you could make a special case where, if an array is returned, apply the array instead of passing the array as an argument to next, but that prevents you from passing an array as an argument to next, which is a very likely scenario.
0
var bind = function() {
  var fns = Array.prototype.slice.call(arguments).reverse();
  return function(value) {
    for (var key in fns) {
      var fn = fns[key];
      console.log(fn);
      value = fn(value);
    }
    return value;
  }
}

function plusTomato(value) {
  return value + "tomato";
}

function plusPear(value) {
  return value + "pear";
}

var plus = bind(plusTomato, plusPear);

var y = plus("pancake"); //pankaketomatopear
console.log(y);
var x = plusTomato(plusPear("pancake")); //pankaketomatopear
console.log(x);

1 Comment

Why do you call that function bind? It doesn't bind anything. Also, never enumerate arrays using for in

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.