0

Given situation

const n = 1;

function value(data) {
  return data === 1 && data;
}

function compare(data, next) {
  return !!(1 == data === next ? next(data) : value(data));
}

function isOne(a, b) {
  return !isNaN(a) && b()
}

function result(x, fn) {
  return fn ? fn(x) : x(n);
}

We can call result()

result(1, function(data) {return compare(data)}); // true

result(1, function(data) {return compare(value(data))}); // true

result(0, compare.bind(value)); // false

result(compare); // true

result(0, compare); // false


result(1, function(data) {
  return isOne(data, compare.bind(null, data, value))
}); // true

With each call pattern resulting in return value of result being true.

How can we call value function as parameter to compare using Function.prototype.bind, Function.prototype.call, Function.prototype.apply or other approach without explicitly using function() {} pattern or changing the functions value, compare or result?

For example:

// where `value` is called with passed `1` as parameter which
// becomes parameter to `compare` call, which becomes parameter
// to `isOne`, with expected return value of `result` being `true`
result(1, isOne.bind(null, compare.bind(null, value)));

const n = 1;

function value(data) {
  console.log("value", data);
  return data === 1 && data;
}

function compare(data, next) {
  console.log("compare", data, next);
  return !!(1 == data === next ? next(data) : value(data));
}

function result(x, fn) {
  console.log("result", x, fn);
  return fn ? fn(x) : x(n);
}

function isOne(a, b) {
  console.log("isOne", a, b);
  return !isNaN(a) && b()
}

console.log(result(1, function(data) {
  return compare(data, value)
})); // true
console.log(result(0, compare.bind(value))); // false
console.log(result(compare.bind(value))); // true
console.log(result(compare)); // true
console.log(result(1, function(data) {
  return isOne(data, compare.bind(null, data, value))
})); // true
 // how can we pass `1` to `value`
 // expected result: `true`, returns `false`
console.log(result(1, isOne.bind(null, compare.bind(null, value))));
guest271314
  • 1
  • 15
  • 104
  • 177
  • You have some very unusual higher-order functions, and it is making your question hard to understand. Could you please create a simpler, more minimal example of what you're trying to do? – 4castle Nov 11 '16 at 04:51
  • Note, `isOne` is function which also cannot be changed. – guest271314 Nov 11 '16 at 04:51
  • @4castle `result` "callback" function `isOne` expects two parameters, one of which is a function. We want to pass the `value` function, with value `x ` passed to callback as parameter to `compare` which will be called within `isOne` to satisfy function parameter expected. `x` => `isOne(x, compare(value(x)))` using `.bind()` – guest271314 Nov 11 '16 at 04:55
  • @4castle Attempting to return result of second to last entry at stacksnippet composed as last entry at stacksnippet. Or, determine if it is not possible to use the last pattern to return same result as second to last pattern. The values are not important save for the value passed to innermost `value` function passed to `compare` function passed to `isOne` function, which is where value is parameter to `value` function. – guest271314 Nov 11 '16 at 05:07
  • @4castle See http://stackoverflow.com/questions/36232576/how-to-filter-object-using-array-prototype-filter, http://stackoverflow.com/questions/40456491/why-is-there-not-a-built-in-method-in-javascript-to-check-if-an-object-is-a-plai/ – guest271314 Nov 15 '16 at 03:14

0 Answers0