• Complain

Chris Eidhof - Functional Swift

Here you can read online Chris Eidhof - Functional Swift full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2016, publisher: objc.io, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Chris Eidhof Functional Swift

Functional Swift: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Functional Swift" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Chris Eidhof: author's other books


Who wrote Functional Swift? Find out the surname, the name of the author of the book and a list of all author's works by series.

Functional Swift — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Functional Swift" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Functional Swift
Functional Swift Chris Eidhof Florian Kugler Wouter Swierstra objcio 2016 - photo 1
Functional Swift
Chris Eidhof
Florian Kugler
Wouter Swierstra

objc.io

2016 Kugler, Eggert und Eidhof GbR
Introduction

Why write this book? Theres plenty of documentation on Swift readily available from Apple, and there are many more books on the way. Why does the world need yet another book on yet another programming language?

This book tries to teach you to think functionally. We believe that Swift has the right language features to teach you how to write functional programs. But what makes a program functional? And why bother learning about this in the first place?

Its hard to give a precise definition of functional programming in the same way, its hard to give a precise definition of object-oriented programming, or any other programming paradigm for that matter. Instead, well try to focus on some of the qualities that we believe well-designed functional programs in Swift should exhibit:

  • Modularity: Rather than thinking of a program as a sequence of assignments and method calls, functional programmers emphasize that each program can be repeatedly broken into smaller and smaller pieces, and all these pieces can be assembled using function application to define a complete program. Of course, this decomposition of a large program into smaller pieces only works if we can avoid sharing state between the individual components. This brings us to our next point.

  • A Careful Treatment of Mutable State: Functional programming is sometimes (half-jokingly) referred to as value-oriented programming. Object-oriented programming focuses on the design of classes and objects, each with their own encapsulated state. Functional programming, on the other hand, emphasizes the importance of programming with values, free of mutable state or other side effects. By avoiding mutable state, functional programs can be more easily combined than their imperative or object-oriented counterparts.

  • Types: Finally, a well-designed functional program makes careful use of types. More than anything else, a careful choice of the types of your data and functions will help structure your code. Swift has a powerful type system that, when used effectively, can make your code both safer and more robust.

We feel these are the key insights that Swift programmers may learn from the functional programming community. Throughout this book, well illustrate each of these points with many examples and case studies.

In our experience, learning to think functionally isnt easy. It challenges the way weve been trained to decompose problems. For programmers who are used to writing for loops, recursion can be confusing; the lack of assignment statements and global state is crippling; and closures, generics, higher-order functions, and monads are just plain weird.

Throughout this book, well assume that you have previous programming experience in Objective-C (or some other object-oriented language). We wont cover Swift basics or teach you to set up your first Xcode project, but we will try to refer to existing Apple documentation when appropriate. You should be comfortable reading Swift programs and familiar with common programming concepts, such as classes, methods, and variables. If youve only just started to learn to program, this may not be the right book for you.

In this book, we want to demystify functional programming and dispel some of the prejudices people may have against it. You dont need to have a PhD in mathematics to use these ideas to improve your code! Functional programming isnt the only way to program in Swift. Instead, we believe that learning about functional programming adds an important new tool to your toolbox, which will make you a better developer in any language.

Updates to the Book

As Swift evolves, well continue to make updates and enhancements to this book. Should you encounter any mistakes, or if youd like to send any other kind of feedback our way, please file an issue in our GitHub repository.

Acknowledgements

Wed like to thank the numerous people who helped shape this book. We wanted to explicitly mention some of them:

Natalye Childress is our copy editor. She has provided invaluable feedback, not only making sure the language is correct and consistent, but also making sure things are understandable.

Sarah Lincoln designed the cover and the layout of the book.

Wouter would like to thank Utrecht University for letting him take time to work on this book.

Wed also like to thank the beta readers for their feedback during the writing of this book (listed in alphabetical order):

Adrian Kosmaczewski, Alexander Altman, Andrew Halls, Bang Jun-young, Daniel Eggert, Daniel Steinberg, David Hart, David Owens II, Eugene Dorfman, f-dz-v, Henry Stamerjohann, J Bucaran, Jamie Forrest, Jaromir Siska, Jason Larsen, Jesse Armand, John Gallagher, Kaan Dedeoglu, Kare Morstol, Kiel Gillard, Kristopher Johnson, Matteo Piombo, Nicholas Outram, Ole Begemann, Rob Napier, Ronald Mannak, Sam Isaacson, Ssu Jen Lu, Stephen Horne, TJ, Terry Lewis, Tim Brooks, Vadim Shpakovski.

Chris, Florian, and Wouter

Thinking Functionally

Functions in Swift are first-class values, i.e. functions may be passed as arguments to other functions, and functions may return new functions. This idea may seem strange if youre used to working with simple types, such as integers, booleans, or structs. In this chapter, well try to explain why first-class functions are useful and provide our first example of functional programming in action.

Throughout this chapter, well use both functions and methods. Methods are a special case of functions: they are functions defined on a type.

Example: Battleship

Well introduce first-class functions using a small example: a non-trivial function that you might need to implement if you were writing a Battleship-like game. The problem well look at boils down to determining whether or not a given point is in range, without being too close to friendly ships or to us.

As a first approximation, you might write a very simple function that checks whether or not a point is in range. For the sake of simplicity, well assume that our ship is located at the origin. We can visualize the region we want to describe in Figure :

Figure 1 The points in range of a ship located at the origin First well - photo 2

Figure 1: The points in range of a ship located at the origin

First, well define two types, Distance and Position:

typealias Distance = Double
struct Position {
var x : Double
var y : Double
}

Note that were using Swifts typealias construct, which allows us to introduce a new name for an existing type. We define Distance to be an alias of Double. This will make our API more expressive.

Now we add a method to Position, within(range:), which checks that a point is in the grey area in Figure . Using some basic geometry, we can write this method as follows:

extension Position {
func within ( range : Distance ) -> Bool {
return sqrt ( x * x + y * y ) <= range
}
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Functional Swift»

Look at similar books to Functional Swift. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Functional Swift»

Discussion, reviews of the book Functional Swift and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.