Functional Programming in Swift by Chris Eidhof & Florian Kugler & Wouter Swierstra

Functional Programming in Swift by Chris Eidhof & Florian Kugler & Wouter Swierstra

Author:Chris Eidhof & Florian Kugler & Wouter Swierstra [Eidhof, Chris]
Language: eng
Format: epub
Published: 2015-04-20T13:28:43+00:00


The decompose function checks whether or not an array is empty. If it is empty, it returns nil; if the array is not empty, it returns a tuple containing both the first element of the array and the tail or remainder of the array, with the first element removed. We can recursively traverse an array by repeatedly calling decompose until it returns nil and the array is empty.

For example, we can use the decompose function to sum the elements of an array recursively, without using a for loop or reduce:

func sum(xs: [Int]) -> Int { if let (head, tail) = xs.decompose { return (head + sum(tail)) } else { return 0 } }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.