溫故而知新:柯里化 與 bind() 的認知

?

什么是柯里化?
科里化是把一個多參數函數轉化為一個嵌套的一元函數的過程。(簡單的說就是將函數的參數,變為多次入參)

const?curry?=?(fn,?...args)?=>?fn.length?<=?args.length???fn(...args)?:?curry.bind(null,?fn,?...args);
//?想要看懂上面這個函數,重點在于全面理解bind的用法。
//?思路倒是很簡單,就是在參數未搜集完善之前,通過bind來實現參數搜集,
//?當搜集完成時,就可以執行原函數了。
const?add?=?(x,?y)?=>?x?+?y;
const?curryadd?=?curry(add);
curryadd(4)(4);?//?8

?

關于bind的認知
bind 和 call / apply 很相似,都可以改變 this 的指向,也都可以傳遞參數。但還是有一些區別:

?1)bind不會立即執行函數,而是返回一個新函數。譬如在 React 中我們經常用 bind 將函數的 this 指向組件本身:

export default class ClickOutside extends Component {constructor(props) {super(props)this.getContainer = this.getContainer.bind(this)}getContainer(ref) {this.container = ref}
}

?

2)除了 this 以外的參數,會把原函數的參數位給占領(擾亂王?鳩占鵲巢?小三上位?),也就是預設值綁定(賦值):

// demo1: 演示預設綁定 x 和 y 的參數
const add = (x, y, z) => x + y + z;
add.bind(null, 1, 2)(3) // => 6 , 等價于 add(1, 2, 3)// demo2:  演示多次bind
const add = (x, y) => x + y;
const myadd = add.bind(null, 1).bind(null, 2)
myadd() // => 3 , 等價于 add(1, 2)// demo3: 和...args這種數組解構結合使用時可別懵了 O(∩_∩)O哈哈~
const add = (...args) => console.log(args, args.length);
const myadd = add.bind(null, 1).bind(null, 2).bind(null, 3).bind(null, 4).bind(null, 5)
myadd() // =>  [1, 2, 3, 4, 5] 5

這種特性實際上和 偏應用 很相似,區別僅僅在于偏應用不需要關注 this 的綁定。

偏應用的目的只有一個,那就是通過預設值減少函數的參數位,達到簡化函數、惰性函數、可重用函數等目的。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389279.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389279.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389279.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

OPENVAS運行

https://www.jianshu.com/p/382546aaaab5轉載于:https://www.cnblogs.com/diyunpeng/p/9258163.html

Memory-Associated Differential Learning論文及代碼解讀

Memory-Associated Differential Learning論文及代碼解讀 論文來源&#xff1a; 論文PDF&#xff1a; Memory-Associated Differential Learning論文 論文代碼&#xff1a; Memory-Associated Differential Learning代碼 論文解讀&#xff1a; 1.Abstract Conventional…

大數據技術 學習之旅_如何開始您的數據科學之旅?

大數據技術 學習之旅Machine Learning seems to be fascinating to a lot of beginners but they often get lost into the pool of information available across different resources. This is true that we have a lot of different algorithms and steps to learn but star…

純API函數實現串口讀寫。

以最后決定用純API函數實現串口讀寫。 先從網上搜索相關代碼&#xff08;關鍵字&#xff1a;C# API 串口&#xff09;&#xff0c;發現網上相關的資料大約來源于一個版本&#xff0c;那就是所謂的msdn提供的樣例代碼&#xff08;msdn的具體出處&#xff0c;我沒有考證&#xff…

數據可視化工具_數據可視化

數據可視化工具Visualizations are a great way to show the story that data wants to tell. However, not all visualizations are built the same. My rule of thumb is stick to simple, easy to understand, and well labeled graphs. Line graphs, bar charts, and histo…

Android Studio調試時遇見Install Repository and sync project的問題

我們可以看到&#xff0c;報的錯是“Failed to resolve: com.android.support:appcompat-v7:16.”&#xff0c;也就是我們在build.gradle中最后一段中的compile項內容。 AS自動生成的“com.android.support:appcompat-v7:16.”實際上是根據我們的最低版本16來選擇16.x.x及以上編…

Apache Ignite 學習筆記(二): Ignite Java Thin Client

前一篇文章&#xff0c;我們介紹了如何安裝部署Ignite集群&#xff0c;并且嘗試了用REST和SQL客戶端連接集群進行了緩存和數據庫的操作。現在我們就來寫點代碼&#xff0c;用Ignite的Java thin client來連接集群。 在開始介紹具體代碼之前&#xff0c;讓我們先簡單的了解一下Ig…

VGAE(Variational graph auto-encoders)論文及代碼解讀

一&#xff0c;論文來源 論文pdf Variational graph auto-encoders 論文代碼 github代碼 二&#xff0c;論文解讀 理論部分參考&#xff1a; Variational Graph Auto-Encoders&#xff08;VGAE&#xff09;理論參考和源碼解析 VGAE&#xff08;Variational graph auto-en…

IIS7設置

IIS 7.0和IIS 6.0相比改變很大誰都知道&#xff0c;而且在IIS 7.0中用VS2005來調試Web項目也不是什么新鮮的話題&#xff0c;但是我還是第一次運用這個東東&#xff0c;所以在此記下我的一些過程&#xff0c;希望能給更多的后來者帶了一點參考。其實我寫這篇文章時也參考了其他…

tableau大屏bi_Excel,Tableau,Power BI ...您應該使用什么?

tableau大屏biAfter publishing my previous article on data visualization with Power BI, I received quite a few questions about the abilities of Power BI as opposed to those of Tableau or Excel. Data, when used correctly, can turn into digital gold. So what …

python 可視化工具_最佳的python可視化工具

python 可視化工具Disclaimer: I work for Datapane免責聲明&#xff1a;我為Datapane工作 動機 (Motivation) There are amazing articles on data visualization on Medium every day. Although this comes at the cost of information overload, it shouldn’t prevent you …

網絡編程 socket介紹

Socket介紹 Socket是應用層與TCP/IP協議族通信的中間軟件抽象層&#xff0c;它是一組接口。在設計模式中&#xff0c;Socket其實就是一個門面模式&#xff0c;它把復雜的TCP/IP協議族隱藏在Socket接口后面&#xff0c;對用戶來說&#xff0c;一組簡單的接口就是全部。 Socket通…

猿課python 第三天

字典 字典是python中唯一的映射類型,字典對象是可變的&#xff0c;但是字典的鍵是不可變對象&#xff0c;字典中可以使用不同的鍵值字典功能> dict.clear()          -->清空字典 dict.keys()          -->獲取所有key dict.values()      …

在C#中使用代理的方式觸發事件

事件&#xff08;event&#xff09;是一個非常重要的概念&#xff0c;我們的程序時刻都在觸發和接收著各種事件&#xff1a;鼠標點擊事件&#xff0c;鍵盤事件&#xff0c;以及處理操作系統的各種事件。所謂事件就是由某個對象發出的消息。比如用戶按下了某個按鈕&#xff0c;某…

BP神經網絡反向傳播手動推導

BP神經網絡過程&#xff1a; 基本思想 BP算法是一個迭代算法&#xff0c;它的基本思想如下&#xff1a; 將訓練集數據輸入到神經網絡的輸入層&#xff0c;經過隱藏層&#xff0c;最后達到輸出層并輸出結果&#xff0c;這就是前向傳播過程。由于神經網絡的輸出結果與實際結果…

使用python和pandas進行同類群組分析

背景故事 (Backstory) I stumbled upon an interesting task while doing a data exercise for a company. It was about cohort analysis based on user activity data, I got really interested so thought of writing this post.在為公司進行數據練習時&#xff0c;我偶然發…

3.Contructor(構造器)模式—精讀《JavaScript 設計模式》Addy Osmani著

同系列友情鏈接: 1.設計模式之初體驗—精讀《JavaScript 設計模式》Addy Osmani著 2.設計模式的分類—精讀《JavaScript 設計模式》Addy Osmani著 Construct&#xff08;構造器&#xff09;模式 在經典的面向對象編程語言中&#xff0c;Construtor是一種在內存已分配給該對象的…

BZOJ 3653: 談笑風生(離線, 長鏈剖分, 后綴和)

題意 給你一顆有 \(n\) 個點并且以 \(1\) 為根的樹。共有 \(q\) 次詢問&#xff0c;每次詢問兩個參數 \(p, k\) 。詢問有多少對點 \((p, a, b)\) 滿足 \(p,a,b\) 為三個不同的點&#xff0c;\(p, a\) 都為 \(b\) 的祖先&#xff0c;且 \(p\) 到 \(a\) 的距離不能超過 \(k\) 。 …

搜索引擎優化學習原理_如何使用數據科學原理來改善您的搜索引擎優化工作

搜索引擎優化學習原理Search Engine Optimisation (SEO) is the discipline of using knowledge gained around how search engines work to build websites and publish content that can be found on search engines by the right people at the right time.搜索引擎優化(SEO…

Siamese網絡(孿生神經網絡)詳解

SiameseFCSiamese網絡&#xff08;孿生神經網絡&#xff09;本文參考文章&#xff1a;Siamese背景Siamese網絡解決的問題要解決什么問題&#xff1f;用了什么方法解決&#xff1f;應用的場景&#xff1a;Siamese的創新Siamese的理論Siamese的損失函數——Contrastive Loss損失函…