Swift, functional programming, and does it matter? Alexis

Similar documents
Functional Programming

SWIFT! init(title: String) { self.title = title } // required initializer w/ named parameter

A Practical Optional Type System for Clojure. Ambrose Bonnaire-Sergeant

Software System Design and Implementation

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Functional Programming Patterns And Their Role Instructions

THE PRAGMATIC INTRO TO REACT. Clayton Anderson thebhwgroup.com WEB AND MOBILE APP DEVELOPMENT AUSTIN, TX

The Hack programming language:

Functional Programming Lecture 13: FP in the Real World

Xcode and Swift CS 4720 Mobile Application Development

Learning Scala: Practical Functional Programming For The JVM By Jason Swartz

CS457/557 Functional Languages

CS 11 Haskell track: lecture 1

SD314 Outils pour le Big Data

Haskell in the Software Industry

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

JavaScript Fundamentals_

Swift 5, ABI Stability and

Type Inference. Prof. Clarkson Fall Today s music: Cool, Calm, and Collected by The Rolling Stones

Seminar on Languages for Scientific Computing Aachen, 6 Feb Navid Abbaszadeh.

CMSC 330: Organization of Programming Languages. Functional Programming with OCaml

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Introduction to Nate Foster Spring 2018

Concepts of programming languages

First Programming Language in CS Education The Arguments for Scala

CSCI-GA Scripting Languages

Lecture 8: Summary of Haskell course + Type Level Programming

Xcode 6 and ios 8 What s New for Software Developers

Rust for high level applications. Lise Henry

Lecture 1: Overview

Haskell in the corporate environment. Jeff Polakow October 17, 2008

Transitioning from C# to Scala Using Apache Thrift. Twitter Finagle

CSC324 Principles of Programming Languages

Introduction to Functional Programming and Haskell. Aden Seaman

Functional Programming Principles in Scala. Martin Odersky

ClojureScript. as a compilation target to JS. Michiel Vijay FP AMS October 16th 2014

CSCI 2041: Introduction

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

Programming Languages and Techniques (CIS120)

Scala, Your Next Programming Language

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Refactoring to Functional. Hadi Hariri

Beyond JavaScript Frameworks: Writing Reliable Web Apps With. Elm. Erik Wendel DevDays Vilnius 2018

Topic 9: Type Checking

Topic 9: Type Checking

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Programming Paradigms

DESIGN, EVOLUTION AND USE

Object-Oriented Programming in Objective-C

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

Functional Programming and the Web

Functional Reactive Programming on ios

CS-XXX: Graduate Programming Languages. Lecture 22 Class-Based Object-Oriented Programming. Dan Grossman 2012

Concepts of Programming Languages

Richard Mallion. Swift for Admins #TEAMSWIFT

CS252 Advanced Programming Language Principles. Prof. Tom Austin San José State University Fall 2013

Tradeoffs. CSE 505: Programming Languages. Lecture 15 Subtyping. Where shall we add useful completeness? Where shall we add completeness?

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

CS558 Programming Languages

CSE341: Programming Languages Interlude: Course Motivation. Zach Tatlock Winter 2018

CSE 333 Lecture 1 - Systems programming

(Refer Slide Time: 4:00)

Front-end Technologies for Formal-Methods Tools

Pushing the Limits. ios 6 Programming TOUCH ADVANCED APPLICATION DEVELOPMENT FOR APPLE IPHONE, IPAD, AND IPOD WILEY. Rob Napier and Mugunth Kumar

CSCE 314 Programming Languages

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Overview. Rationale Division of labour between script and C++ Choice of language(s) Interfacing to C++ Performance, memory

CS162 Week 1. Kyle Dewey. Friday, January 10, 14

Multi-catch. Future Features. Sometimes we need to handle more than one exception in a single catch block:

Concepts of programming languages

CMPSC 311- Introduction to Systems Programming Module: Systems Programming

Scope. Chapter Ten Modern Programming Languages 1

BE PROACTIVE USE REACTIVE

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CSE 333 Lecture 1 - Systems programming

Writing Cognitive Swift Apps developerworks Open Tech Talk March 8, 2017

ITT8060 Advanced Programming

Concurrency: what, why, how

Software Design and Analysis for Engineers

To figure this out we need a more precise understanding of how ML works

CS153: Compilers Lecture 1: Introduction

Overloading, Type Classes, and Algebraic Datatypes

Introduction, Functions

CS558 Programming Languages

Paradigms of computer programming

CIS 194: Homework 4. Due Wednesday, February 18, What is a Number?

IA010: Principles of Programming Languages

CSE 341: Programming Languages

TYPE INFERENCE. François Pottier. The Programming Languages Mentoring ICFP August 30, 2015

It's Time To Get Functional 이건희

MF#K - Introduction to F# GroupM Mødegruppe for Ƒunktionelle Københavnere (MF#K)

Data Types primitive, arrays, objects Java overview Primitive data types in Java

Who am I? Harlan Iverson. Programming enthusiast. Seeker of truth. Imperfect. I'll be wrong about some things. Please correct me if you can.

Functional Programming Lecture 1: Introduction

A Func'onal Introduc'on. COS 326 David Walker Princeton University

Programming Languages and Techniques (CIS120)

CMSC 330: Organization of Programming Languages

Principles of Programming Languages

The Haskell HOP: Higher-order Programming

Transcription:

Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher

Questions What s new in Swift? Is Swift a functional programming language? And what is functional anyway? How useful is this really?

What's'hardly'new'in'Swi,?

Objective-C classes methods protocols categories functions ARC blocks Cocoa values & collections

Objective-C Swift classes methods protocols extensions functions ARC closures Cocoa values & collections

What's'a'bit$new'in'Swi,?

Objective-C Swift + Swift classes methods protocols extensions functions ARC closures Cocoa values & collections structs namespaces access control operator overloading ObjC interop Swift values & collections

What's'really&new'in'Swi,?

Objective-C Swift + Swift + Swift classes structs enums with associated values methods namespaces option types protocols access control pattern matching extensions operator generics functions overloading type inference ARC ObjC interop immutability supports closures Swift values & tuples Cocoa values collections & collections

Objective-C Swift + Swift + Swift classes structs enums with associated values methods namespaces option types protocols access control pattern matching extensions operator generics functions overloading type inference ARC ObjC interop immutability supports closures Swift values & tuples Cocoa values collections & collections

Every really new part of Swift originated in a functional programming language of the 1980s or earlier.

FP

JavaScript*is*brilliant*and*is* succeeding*...*and*i*think*it's*because* of*the*func:onal*stuff. "Douglas"Crockford,"2014

No#ma&er#what#language#you#work# in,#programming#in#a#func7onal#style# provides#benefits.#you#should#do#it# whenever#it#is#convenient... "John"Carmack,"2012.

a"programming"style trea%ng(the func%on as#the#primary#unit#of#abstrac2on

mathematical functions establish true relations computational functions do things which remain true t 0 =5 NSNumber * t0 = @5; f(t 0 ) = 50 NSNumber * pos = f(t0); pos; // => 50 f(t 0 ) = 50 pos = f(t0); pos; //=> 100; (surprise!)

mathematical variables are names we give to values computational variables are like names for places, whose contents can change t 0 =5 t 0 =5 t 0 =6 NSNumber * five = @5; five = @6; //?! five = @7; [five setintegervalue:8]; //?!?!

FP style boils down to Restrictions to emulate the predictability of mathematical functions and variables Idioms to use functions for all abstraction in defining other functions, in hiding information, etc. (Sometimes) type systems to provide compile-time checks on the values moving through functions

STYLE supported by FEATURES Restrictions pure functions immutable data purity and immutability guarantees Idioms combine, pass, & return functions like other values function literals function closures functions as first-class values enums (aka, sum types, tagged unions) Type systems (sometimes) to check valid values algebraic data types type inference fancy types: first-order, recursive, dependent, higher-kinded, constrained, etc..

dynamic Scheme (1975) static ML (1973) SML (1984) Erlang (1986) Haskell (1988) OCaml (1996) Scala (2003) Clojure (2009) F# (2005) Coq, Agda, Idris

So is Swift functional?

Haskell SML Scheme Clojure Swift ObjC purity & immutability help Yes, very. Y Y Y Y meh function literals, closures, first-classness Y Y Y Y Y meh expression-oriented Y Y Y Y N N proper tail calls Y Y Y N?? memory management Y Y Y Y meh meh generics Y Y N N Y N enum types Y Y N N Y N type inference Y Y N N Y N algebraic data types Y Y N N? N super DUPER fancy types Y? N N N N

If this were 1998 Swift would be a niche functional language

Swift is not functional June 10th, 2014 Rob Napier I, for one, welcome our new Haskell overloads Rob Napier Sep 29th, 2014

Objective-C without the C implies something subtractive, but Swift dramatically expands the design space through the introduction of generics and functional programming concepts. Chris Lattner

Should we care?

<opinion>value of FP</opinion> A useful toolbox for processing data, collections and streams. A salutary discipline for simplicity. Pure functions, plain values. When this is enough, it s also the best. And it s often enough. Enums & protocols interfaces clarify thinking, but types can complicate coding. (Safety benefits overrated.) FRP, ReactiveCocoa, functional GUI I remain hopeful, skeptical.

Objective-C Swift + Swift + Swift classes structs enums with associated values methods namespaces option types protocols access control pattern matching extensions operator generics functions overloading type inference ARC ObjC interop immutability supports closures Swift values & tuples Cocoa values collections & collections

Objective-C without the C implies something subtractive, but Swift dramatically expands the design space through the introduction of generics and functional programming concepts. Chris Lattner FP!= type systems

Where Swift/Cocoa fight FP Support for immutable values, but Inexpressive. Needs hash & equals boilerplate for value objects. Inexpressive. No support for updated copy of such value objects, nor of dictionaries, nor any KVC on structs. Unperformant? No functional data structures. Not expression-oriented. (e.g., switch returns no value) Cocoa is subclasserific.

Videos, notes, and exercises from the Coursera Programming Languages course, a great intro to Scheme, SML, and Ruby. <https://class.coursera.org/proglang-2012-001> The talks by Rich Hickey (inventor of Clojure) are very rich: the Value of Values, the Language of the System, etc..

Chris Eidhof, Florian Kugler, and Wouter Swierstra Functional Programming in Swift GitHub RAC devs (jspahrsummers, joshaber, & others) ReactiveCocoa. This work is indirectly influenced by typed FP. <https://github.com/reactivecocoa/reactivecocoa> Maxwell Swadling SwiftZ. Currently translating lots of typed FP idioms into a Swift library. <https://github.com/maxpow4h/swiftz> David Nolen. Om. Functional approach to writing GUIs in the browser, using ClojureScript with Facebook s React, leveraging CSP-style concurrency. Untyped FP. <http://swannodette.github.io/2013/12/17/the-future-of-javascript-mvcs/>

@end

Is ObjC going away? Is it easier now?

Objective-C Swift + Swift + Swift classes structs enums with associated values methods namespaces option types protocols access control pattern matching extensions operator generics functions overloading type inference ARC ObjC interop immutability supports closures Swift values & tuples Cocoa values collections & collections

Objective-C isn't really going anywhere... Even if you start a brand new Swift app for the first time today after this session, you're going to use Cocoa, or Cocoa Touch. You're going to import Foundation. You're going to import UIKit. Those frameworks are written in Objective-C. That means, wherever you look around, there's going to be Objective-C in the picture. And you're going to have to deal with debugging mixed Swift / Objective-C situations.. "Advanced Swift Debugging in LLDB", WWDC2014, session 410, 20m 28m.

Canaries in the ObjC coal mine Swift achieves genuine C-like performance Swift-only types appearing at public API boundaries Deprecation of the ObjC runtime s more dynamic features

Swift is mostly conveniences Swift s support for FP are essentially conveniences vs ObjC: unified syntax for value types and references types, for methods, functions, and closures, type inference easier declarations of immutability More substantive: enums with associated values. Type system?