Skip to content Skip to sidebar Skip to footer

Function That Will Execute Function Depending On Value. Functional Programming

I have two functions and they are executed depending of if statement. E.g.: if(value) { doA() } else { doB() } How to write function or object that will take the result an

Solution 1:

continuation passing style

here's an approach using continuation passing style. you'll notice the implementation of main is not far off from your original encoding – once you finish wrapping your head around this, if you haven't already learned about monads, you now know the best one (cont) ^_^

// cont :: a -> (a -> b) -> bconstcont = x =>
  k => k (x)
  
// when :: (a -> boolean, a -> b, a -> b) -> a -> (a -> b) -> bconstwhen = (f,l,r) => x =>
  f (x) ? cont (l (x)) : cont (r (x))

// isOdd :: number -> boolean  constisOdd = x =>
  x & 1 === 1// doA :: number -> number    constdoA = x =>
  x + 1// doB :: number -> numberconstdoB = x =>
  x * x

// main :: number -> voidconstmain = x =>
  cont (x) (when (isOdd, doA, doB)) (console.log)
  
main (3) // IO: 4,  doA (3) === 3 + 1
main (4) // IO: 16, doB (4) === 4 * 4

data constructors

here's another approach using simple data constructors Left and Right to represent a Fork sum type – this results in a sort of data-directed style where the control of main is determined by the input type

// type Fork a = Left a | Right a// Left a :: { fork :: (a -> b, _) -> b }constLeft = x =>
  ({ type: Left, fork: (f,_) => f (x) })

// Right a :: { fork :: (_, a -> b) -> b }  constRight = x =>
  ({ type: Right, fork: (_,f) => f (x) })

// doA :: number -> number    constdoA = x =>
  x + 1// doB :: number -> numberconstdoB = x =>
  x * x

// main :: Fork a -> aconstmain = f =>
  // fork applies the left function (doA) to a Left// fork applies the right function (doB) to a Right
  f.fork (doA, doB)
  
console.log (main (Left (3)))  // => 4, doA (3) === 3 + 1console.log (main (Right (3))) // => 9, doB (3) === 3 * 3

Solution 2:

You could write something like this, for example:

functionexists(value) {
    returnfunction (func) {
        if (value !== undefined) {
            returnfunc(value);
        }
        returnnull;
    }
}

functionnothing(value) {
    returnfunction (func) {
        if (value === undefined) {
            returnfunc();
        }
        returnnull;
    }
}

functiondoA(value) {
    console.log('doing A', value);
}

functiondoB() {
    console.log('doing B');
}

const foo = 'fool';
const bar = undefined;

exists(foo)(doA);
nothing(bar)(doB);

The exists function gets a value and returns another function. The function that is returned gets yet another function as argument, which is executed if the value passed is defined.

I've used “old school” anonymous functions in the example above to make it easier to understand. With ES6 arrow functions, you can write the exists and nothing functions more consisely, like this:

functionexists(value) {
    returnfunc => value !== undefined ? func(value) : null;
}

functionnothing(value) {
    returnfunc => value === undefined ? func(value) : null;
}

The “functional programming fun” really starts when you realize you can refactor these two functions by putting the common code in another function, that is then used to create the two functions, like this:

functionexecuteWithCondition(predicate) {
    returnvalue =>func =>predicate(value) ? func(value) : null;
}

const exists = executeWithCondition(value => value !== undefined);
const nothing = executeWithCondition(value => value === undefined);

This technique is called currying.

Usage of these functions is still the same:

exists(foo)(doA);
nothing(bar)(doB);

Here's the complete runnable code example:

functionexecuteWithCondition(predicate) {
    returnvalue =>func =>predicate(value) ? func(value) : null;
}

const exists = executeWithCondition(value => value !== undefined);
const nothing = executeWithCondition(value => value === undefined);

functiondoA(value) {
    console.log('doing A', value);
}

functiondoB() {
    console.log('doing B');
}

const foo = 'fool';
const bar = undefined;

exists(foo)(doA);
nothing(bar)(doB);

Solution 3:

One approach would be to define the properties of an object with values set to functions

const o = {
  exists: function(value) {
    return value ? this.doA() : this.doB()
  },
  doA: function(value) {
    console.log("A")
  },
  doB: function(value) {
    console.log("B")
  }
}

o.exists(void0);

Post a Comment for "Function That Will Execute Function Depending On Value. Functional Programming"