[英]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但是,我不确定这是哪个组合器来自这样的列表...... 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?最初有人告诉我这是一个 Blackbird 组合器,但我不认为是这样? 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
黑鸟的定义如下...
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
ie it returns a function that takes an a
and returns a d
.即它返回一个接受
a
并返回一个d
的函数。 a
is passed into two functions which creates b
and c
. a
被传递给创建b
和c
两个函数。 They are then passed into a function that returns d
.然后将它们传递到返回
d
的函数中。
After the above comment以上评论后
I found it!我找到了! From @evolutionxbox link I found the
Starling_
combinator...从@evolutionxbox 链接中,我找到了
Starling_
组合器...
starling_ :: (b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d
Which is exactly what I had written above :D这正是我上面写的:D
Thanks to the helpful link from @evolutionxbox I was able to find the Starling_
combinator which is defined as...感谢来自@evolutionxbox 的有用链接,我能够找到定义为...的
Starling_
组合子
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 -> b
是我的sum
函数,它接受数组并返回一个数字。a -> c
is my length
function that takes the array and return a number. a -> c
是我的length
函数,它接受数组并返回一个数字。b -> c -> d
is my div
function that takes two numbers and divides them. b -> c -> d
是我的div
函数,它接受两个数字并将它们相除。 This is the first time I feel like I've understood combinators properly now.这是我第一次觉得我现在已经正确理解了组合器。 :)
:)
Thanks for the help :)谢谢您的帮助 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.