Skip to content

My take on _Functional Programming in Swift_'s Battleship example

Notifications You must be signed in to change notification settings

radex/Battleship

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Battleship

My take on Functional Programming in Swift's Battleship example, using structs, Swift 2 protocol extensions and Core Graphics to render regions in a nicer way.

Here's the code:

let circle1 = Circle(100).offset(200, 150)
let circle2 = Circle(80).offset(100, 200)
let enclosing = Circle(120).offset(160, 170)
let circle4 = Circle(50).offset(200, 150)
visualizer.region = circle1.plus(circle2).intersection(enclosing).minus(circle4)

Results (in pixel sampling and smart drawing modes):

Pixel sampling Smart drawing

The main difference with the book's example is that instead of dealing with CGPoint -> Bool matcher functions directly, I wrap them in a structs (conforming to a RegionType protocol). There are two nice things about this:

  • you can use the more readable, nicely chainable method notation to transform regions
  • in addition to the matcher function, a struct can also hold more information about the nature of the region

The second point means that Circle stores its radius, and .offset, .plus, .intersection, .minus return their own transformation structs that wrap the original regions and hold information about the transformation. This way, we can compose actual regions and draw them really nicely using Core Graphics.

About

My take on _Functional Programming in Swift_'s Battleship example

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages