Tap(K-combinator)的功能签名 [英] Function signature of Tap (K-combinator)

查看:260
本文介绍了Tap(K-combinator)的功能签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过一本书,说明了tap函数(也称为K-Combinator)的函数签名如下:

  tap ::(a  - > *) - > a  - > a 




这个函数接受一个输入对象a和一个函数,





  1. >有人可以帮我解释函数签名中星号(*)的含义吗?

  2. 低于实现正确吗?

  3. 如果全部这三个实现是正确的,哪一个应该用在什么时候?任何示例?



实现1:



  const tap = fn => a => {fn(a);返回一个; }; (it)=> console.log(it))(10); 

tap // 10



执行2:



  const tap = a => fn => {fn(a);返回一个; }; 

点击(10)((it)=> console.log(it)); // 10



实现3:



  const tap =(a,fn)=> {FN(一);返回一个; }; 

点击(10,(it)=> console.log(it)); // 10


解决方案

这看起来很像 Ramda定义。那里的 * 可能是一个错误。 (免责声明:我是Ramda作者之一)它应该可以阅读

  // tap ::(a  - > ; b) - > a  - > a 

类似于您的第一个实现:

  const tap = fn => a => {fn(a);返回一个; }; 

或拉姆达版本:

 {fn(a); return a;}); 

匹配该签名,并且主要用于调试上下文。我使用它来暂时将日志记录引入到功能管道中:


$ b

  // Map Map User 
const users = fetchUsersFromSomewhere();

// :: [评论] - > [编号]
常量userRatingForComments = R.pipe(
R.pluck('username'),// [Comment] - > [String]
R.tap(console.log) ,//用于调试,需要在浏览器中包含`bind` envs
// ^^^^^^^^^^^^^^^^^^
R.map(R.propOf (用户))// // [String] - > [User]
R.pluck('rating')// [User] - > [Number]
);

尽管这并不是K combinator。




1 此代码示例来自我的旧文章在拉达。


I've read in a book that the function signature of tap function (also called K-Combinator) is below:

tap :: (a -> *) -> a -> a

"This function takes an input object a and a function that performs some action on a. It runs the given function with the supplied object and then returns the object."

  1. Can someone help me to explain what is the meaning of star (*) in the function signature?
  2. Are below implementation correct?
  3. If all the three implementation are correct, which one should be used when? Any examples?

Implementation 1:

const tap = fn => a => { fn(a); return a; };

tap((it) => console.log(it))(10); //10

Implementation 2:

const tap = a => fn => { fn(a); return a; }; 

tap(10)((it) => console.log(it)); //10

Implementation 3:

const tap = (a, fn) => {fn(a); return a; };

tap(10, (it) => console.log(it)); //10

解决方案

This looks much like the Ramda definition. The * in there is probably a mistake. (Disclaimer: I'm one of the Ramda authors.) It should probably read

// tap :: (a -> b) -> a -> a

An implementation like your first one:

const tap = fn => a => { fn(a); return a; };

or Ramda's version:

const tap = curry((fn, a) => { fn(a); return a; });

match that signature and are useful IMHO mostly in debugging contexts. I use it to temporarily introduce logging statements into functional pipelines1:

// :: Map String User
const users = fetchUsersFromSomewhere();

// :: [Comment] -> [Number]  
const userRatingForComments = R.pipe(
    R.pluck('username'),     // [Comment] -> [String]
    R.tap(console.log),      // for debugging, need to include `bind` in browser envs
//  ^^^^^^^^^^^^^^^^^^      
    R.map(R.propOf(users)),  // [String] -> [User]
    R.pluck('rating')        // [User] -> [Number]
);

This is really not the K combinator, though.


1 This code sample is from an old article of mine on Ramda.

这篇关于Tap(K-combinator)的功能签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆