7. just%syntax,%almost
Swi$:
class Foo: UIView {
weak var myView:UIView?
func addAt(#x:CGFloat, y:CGFloat) -> CGFloat {
let v = UIView(frame:CGRectMake(x,y,10,10))
self.addSubview(v)
self.myView = v
return x + y
}
}
21. mathematical variables"
are names
we give to values
t0 = 5
t0 = 5
t0 = 6
computational variables"
are like names for places,
whose contents can change
NSNumber * five = @5;
five = @6; // ?!
five = @7;
[five setIntegerValue:8]; // ?! ?!
22. 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
23. • purity and immutability guarantees
• function literals
• function closures
• functions as first-class values
• enums (aka, sum types, tagged unions)
• algebraic data types
• type inference
• fancy types: first-order, recursive,
dependent, higher-kinded, constrained,
etc..
FEATURES
• Idioms
• combine, pass, & return
functions like other values
• Restrictions
• pure functions
• immutable data
• Type systems (sometimes)
• to check valid values
STYLE
supported by
26. 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
27. If this were 1998
Swift would be a
niche functional language
28. If this were 1998
Swift would be a
niche functional language
… but maybe we’re all functional now? or could be?
36. JSON
NSJSONSerialization Class Reference
…
An object that may be converted to JSON must have the following
properties:
•
The top level object is an NSArray or NSDictionary.
•
All objects are instances of NSString, NSNumber, NSArray,
NSDictionary, or NSNull.
•
All dictionary keys are instances of NSString.
•
Numbers are not NaN or infinity.
!
Other rules may apply. Calling isValidJSONObject: or attempting a
conversion are the definitive ways to tell if a given object can be
converted to JSON data.
40. Either'type
Swi$:
enum DataResult {
case Success(NSData)
case Error(NSError)
// ...
}
switch NSData(contentsOfFile:file, options:0) {
case .Success(let data):
// handle data here then return
case .Error(let error):
// handle error here then return
}
41. SwiftZ (by maxpow4h)
Either and Result:!
//
Result
represents
something
that
could
work
or
be
an
NSError.
//
Say
we
have
2
functions,
the
first
fetches
from
a
web
services,
//
the
second
decodes
the
string
into
a
User.
//
Both
*could*
fail
with
an
NSError,
so
we
use
a
Result<A>.
func
getWeb()
-‐>
Result<String>
{
var
e:
NSError?
let
str
=
doStuff("foo",
e)
return
Result(e,
str)
}
!
func
decodeWeb(str:
String)
-‐>
Result<User>
{
var
e:
NSError?
let
user
=
decode(str,
e)
return
Result(e,
user)
}
!
//
We
can
compose
these
two
functions
with
the
`>>=`
function.
!
let
getUser:
Result<User>
=
getWeb()
>>=
decodeWeb
!
switch
(getUser)
{
case
let
.Error(e):
println("NSError:
(e)")
case
let
.Value(user):
println(user.name)
44. –"Advanced Swift Debugging in LLDB", WWDC2014, session 410, 20m—28m.
“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..”
45. 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
46. • 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/>
• Upcoming book from ObjC.io authors.
49. • Videos, notes, and homework from the Coursera “Programming
Languages” course, a great intro to Scheme, SML, and Ruby.
<https://class.coursera.org/proglang-2012-001>
• Interesting way to do SICP: http://understudyapp.com
• The talks by Rich Hickey (inventor of Clojure) are very rich