Skip to content

Instantly share code, notes, and snippets.

@es-kumagai
Created March 29, 2016 12:59
Show Gist options
  • Save es-kumagai/b60a9f7ba93d2512a04c to your computer and use it in GitHub Desktop.
Save es-kumagai/b60a9f7ba93d2512a04c to your computer and use it in GitHub Desktop.
先ほどの自分の発表で使ったコード、とは若干違うところもありますけど、とりあえず資料を書きながら作ったコードを切り出しました。 #__swift__ #CodePiece
protocol Movable {
associatedtype Unit
func moved(x x:Unit, y:Unit) -> Self
func movedHorizontal(x:Unit) -> Self
func movedVertical(y:Unit) -> Self
}
struct Location {
var x: Int
var y: Int
}
extension Location : Movable {
typealias Unit = Int
func moved(x x:Int, y:Int) -> Location {
return movedHorizontal(x).movedVertical(y)
}
func movedHorizontal(x:Int) -> Location {
return Location(x: self.x + x, y: self.y)
}
func movedVertical(y:Int) -> Location {
return Location(x: self.x, y: self.y + y)
}
}
extension Location : CollectionType {
var startIndex: Int {
return 0
}
var endIndex: Int {
return 2
}
subscript (index: Int) -> Int {
switch index {
case 0:
return x
case 1:
return y
default:
fatalError()
}
}
}
let location = Location(x: 10, y: 20)
let newLocation1 = location.movedHorizontal(10)
let newLocation2 = newLocation1.movedVertical(20)
let newLocation3 = newLocation1.moved(x: 4, y: 8)
location.count // 2
location.indices // 0 ..< 2
location.first! // x
location.last! // y
location.minElement()
location.maxElement()
for v in location {
print(v)
}
import Darwin
print(location.map { $0 * 2 })
func randomMove<T:Movable where T.Unit == Int>(location: T, maxStep: T.Unit) -> T {
let deltaX = Int(arc4random_uniform(UInt32(maxStep)))
let deltaY = Int(arc4random_uniform(UInt32(maxStep)))
return location.moved(x: deltaX, y: deltaY)
}
randomMove(location, maxStep: 100)
import Cocoa
import AVFoundation
protocol MediaType {
var data: NSData { get }
}
protocol PlayerType {
var content: Music? { get }
// var canPlay: Bool { get }
func play() throws
}
enum PlayerError : ErrorType {
case NotReady
}
struct Music : MediaType {
init(_ name: String) {
}
var data: NSData {
return NSData()
}
}
struct NullSound : MediaType {
var data: NSData {
return NSData()
}
}
class AVAudioPlayerEx : AVAudioPlayer {
override init(data: NSData) throws {
super.init()
}
}
extension PlayerType {
var canPlay: Bool {
return content != nil
}
func play() throws {
guard canPlay else {
throw PlayerError.NotReady
}
try AVAudioPlayerEx(data: content?.data ?? NullSound().data)
}
}
final class MusicPlayer : PlayerType {
var content: Music?
init(content: Music?) {
self.content = content
}
}
final class SilentSoundPlayer : PlayerType {
let content: Music? = nil
var canPlay: Bool {
return true
}
// func play() throws {
//
// guard canPlay else {
//
// throw PlayerError.NotReady
// }
//
// try AVAudioPlayerEx(data: NullSound().data)
// }
}
let player1 = MusicPlayer(content: Music("SomeSound.mp3"))
let player2 = SilentSoundPlayer()
let player3: PlayerType = SilentSoundPlayer()
if player1.canPlay {
do {
try player1.play()
print("YES")
}
catch { print("ERROR: \(error)") }
}
else {
print("NO")
}
if player2.canPlay {
do {
try player2.play()
print("YES")
}
catch { print("ERROR: \(error)") }
}
else {
print("NO")
}
if player3.canPlay {
do {
try player3.play()
print("YES")
}
catch { print("ERROR: \(error)") }
}
else {
print("NO")
}
func start<T:PlayerType>(player: T) throws -> Bool {
if player.canPlay {
try player.play()
return true
}
else {
return false
}
}
player1.canPlay
player2.canPlay
player3.canPlay
try? start(player1) // true
try? start(player2) // true
//try? start(player3)
protocol Walkable {
func walk() -> String
}
extension Walkable {
func walk() -> String {
return "By Spilit"
}
}
class Human : Walkable {
func walk() -> String {
return "By Human"
}
}
let human: Walkable = Human()
human.walk()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment