- 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.
- GitHub Essentials
- Microsoft SQL Server企業級平臺管理實踐
- SQL Server入門經典
- Developing Mobile Games with Moai SDK
- SQL Server 2008數據庫應用技術(第二版)
- Hadoop與大數據挖掘(第2版)
- 大數據架構和算法實現之路:電商系統的技術實戰
- Microsoft Power BI數據可視化與數據分析
- MySQL 8.x從入門到精通(視頻教學版)
- PostgreSQL指南:內幕探索
- TextMate How-to
- 數據庫應用系統技術
- Spring Boot 2.0 Cookbook(Second Edition)
- Learning Ansible
- 大數據計算系統原理、技術與應用