Introduction to Functional Programming in Swift

*Imperative thinking is thinking in terms of algorithms or steps when solving a problem, or the how of getting from A (the problem) to B (the solution). A perfect example of an imperative approach is a recipe for cupcakes the tells you how much to measure, how to mix and how to bake them.

Declarative thinking analyzes a problem in terms of what the solution is, but not how to get to the solution. Take the concept of a recipe for an example. A declarative recipe would essentially show you a picture or offer a description of cupcakes, rather than tell you how to mix ingredients and how to bake them. 

FP encourages a declarative approach to solving problems via functions.


*Many treaties that discuss FP single out immutable state and lack of side effects as the most important aspects of FP.


*Another characteristic of FP languages is that functions are first-class citizens, meaning that functions can be assigned to values and passed in and out of other functions. There's a higher level yet: higher-order functions. These accept functions as parameters or return other functions.


*Currying essentially means thinking of multi-parameter functions as a sequence of function applications for each parameter, one-by-one.


*

func rideTypeFilter(type: RideType)(fromRides rides: [Ride]) -> [Ride] {

    return rides.filter { $0.types.contains(type) }

}

func createRideTypeFilter(type: RideType) -> [Ride] -> [Ride] {

    returnrideTypeFilter(type)

}

let kidRideFilter =createRideTypeFilter(.Kids)

print(kidRideFilter(parkRides))

Here, rideTypeFilter(:)(:) accepts a RideType as its first parameter and an array of rides as its second.

One of the best uses of currying is to make other functions. By calling your curried function with just one parameter, you make another function that will only require one parameter when called.

Notice that createRideTypeFilter returns a function that maps an array of rides to an array of rides. It does so by calling rideTypeFilter and passing in only the first parameter. Use createRideTypeFilter to create a filter for kid rides, and then apply that filter to your list of park rides.


*Pure Function:

Always produce the same output when given the same input.

Create zero side effects outside of the function. 


*Pure functions are closely related to the concept of referential transparency. An element of a program is referentially transparent if you can replace it with its definition and always produce the same result. It makes for predictable code and allows the compiler to perform optimizations.

Pure functions satisfy this condition. Pre-processor #define constants and marcos are familiar examples of referentially transparent code items.


*By taking a functional, declarative approach, your code can be more concise and clear. As if you need another reason to work with FP, keep in mind that your code easier to test when isolated modular functions that are free from side effects.

Finally, as multi-processing cores become the norm for both CPUs and GPUs, minimizing side effects and issues from concurrency will become increasingly important, and FP will be one of the most important ways to achieve smooth performance.






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值