- Swift Functional Programming(Second Edition)
- Dr. Fatih Nayebi
- 271字
- 2021-07-02 23:54:32
Pure functions
Pure functions are functions that do not possess any side effects; in other words, they do not change or alter any data or state outside of themselves. Additionally, they do not access any data or state except their provided parameters. Pure functions are like mathematical functions that are pure by nature.
Pure functions return a value that is only determined by its parameter values. Pure functions are easy to test as they rely only on their parameters and do not change or access any data or state outside of themselves. Pure functions are suitable for concurrency as they do not access and change global data or states.
The following list presents examples of pure and not pure functions:
- Printing a String literal to a console is not pure as it modifies an external state.
- Reading a file is not pure as it depends on the external state at different times.
- The length of a String is pure as it does not rely on a state. It only takes a String as input and returns the length as output.
- Getting the current date is not pure as it returns different values when called on different dates.
- Getting a random number is not pure as it returns different values each time it is called.
Using pure functions may sound very restrictive and impossible to utilize in real-world scenarios. For instance, file and database development in Swift cannot be perfectly pure but there are lots of other functionalities that can be developed using pure functions. We will see the benefits of pure functions in more detail and examples in upcoming chapters.
- 輕松學(xué)大數(shù)據(jù)挖掘:算法、場景與數(shù)據(jù)產(chǎn)品
- Oracle RAC 11g實戰(zhàn)指南
- Python數(shù)據(jù)分析:基于Plotly的動態(tài)可視化繪圖
- 大數(shù)據(jù):從概念到運營
- Ceph源碼分析
- 基于Apache CXF構(gòu)建SOA應(yīng)用
- 大數(shù)據(jù)治理與安全:從理論到開源實踐
- R語言數(shù)據(jù)挖掘
- Splunk智能運維實戰(zhàn)
- Solaris操作系統(tǒng)原理實驗教程
- MySQL DBA修煉之道
- 企業(yè)主數(shù)據(jù)管理實務(wù)
- 云工作時代:科技進(jìn)化必將帶來的新工作方式
- MySQL核心技術(shù)手冊
- SQL Server 數(shù)據(jù)庫教程(2008版)