Search code examples
javascriptcombinators

What is this combinator?


I used a combinator to calculate the average of a list of numbers...

const myCombinator = f => g => h => x => f(g(x))(h(x));

I could then use it like...

const div = a => b => a/b;
const sum = a => a.reduce((a, n) => a + n, 0);
const length = a => a.length();

const average = myCombinator(div)(sum)(length);

console.log(average([1, 2, 3, 4, 5, 6, 7, 8, 9]));

However, I'm not sure which combinator this is from a list like... http://www.angelfire.com/tx4/cus/combinator/birds.html

I was told initially that it's a Blackbird combinator but I don't think that is the case? Is that correct?

Is it a "named" combinator? If so, do you know which one it is?

From the comments

Blackbird is defined like... blackbird :: (c -> d) -> (a -> b -> c) -> a -> b -> d

However, I think my function is doing like...

myCombinator :: (b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d

i.e. it returns a function that takes an a and returns a d. a is passed into two functions which creates b and c. They are then passed into a function that returns d.

After the above comment

I found it! From @evolutionxbox link I found the Starling_ combinator...

starling_ :: (b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d

Which is exactly what I had written above :D


Solution

  • Thanks to the helpful link from @evolutionxbox I was able to find the Starling_ combinator which is defined as...

    starling_ :: (b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d

    In my case...

    • a -> b is my sum function that takes the array and returns a number.
    • a -> c is my length function that takes the array and return a number.
    • b -> c -> d is my div function that takes two numbers and divides them.

    This is the first time I feel like I've understood combinators properly now. :)

    Thanks for the help :)