2014年9月7日日曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の7章(高度な関数: 関数を最大限に活用する)、エクササイズ(p.321)をSwiftで考えてみる。

エクササイズ(p.321)

コード(Xcode)

main.swift

//
//  main.swift
//  sample321
//
//  Created by kamimura on 9/7/14.
//  Copyright (c) 2014 kamimura. All rights reserved.
//

import Foundation

let ads:[String] = [
    "William: SBM GSHO likes sports, TV, dining",
    "Matt: SUM NS likes art, movies, theater",
    "Luis: SLM ND likes books, theater, art",
    "Mike: DWM DS likes trucks, sports and bieber",
    "Peter: SAM likes chess, working out and art",
    "Josh: SJM likes sports, movies and theater",
    "Jed: DBM likes theater, books and dining"
]

func sportsNoBieber(s:String) -> Bool {
    return s.find("sports") != -1 && s.find("bieber") == -1
}

func sportsOrWorkout(s:String) -> Bool {
    return s.find("sports") != -1 || s.find("working") != -1
}

func nsTheater(s:String) -> Bool {
    return s.find("NS") != -1 && s.find("theater") != -1
}

func artsTheaterOrDining(s:String) -> Bool {
    return s.find("art") != -1 || s.find("theater") != -1 || s.find("dining") != -1
}

func find(match:(String) -> Bool) {
    println("検索結果:")
    println("-" * 50)
    for ad in ads {
        if match(ad) {
            println(ad)
        }
    }
    println("-" * 50)
}

find(sportsNoBieber)
find(sportsOrWorkout)
find(nsTheater)
find(artsTheaterOrDining)

string.swift

//
//  string.swift
//  string
//
//  Created by kamimura on 8/10/14.
//  Copyright (c) 2014 kamimura. All rights reserved.
//

import Foundation

extension String {
    var swapcaseString:String {
    var result: String = ""
        for ch in self {
            let s = String(ch)
            result += s.uppercaseString == s ? s.lowercaseString : s.uppercaseString
        }
        return result
    }
    var isUpper:Bool { return self.uppercaseString == self }
    var isLower:Bool { return self.lowercaseString == self }
    var length:Int { return (self as NSString).length }
    var strip:String { return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())}
    var floatValue:Float { return (self as NSString).floatValue }
    var doubleValue:Double { return (self as NSString).doubleValue }
    func count(sub:String) -> Int {
        var result:Int = 0
        var s = self
        let index:String.Index = "a".endIndex
        while s != "" {
            if s.hasPrefix(sub) {
                result += 1
                s = s.substringFromIndex(sub.endIndex)
            } else {
                s = s.substringFromIndex(index)
            }
            
        }
        return result
    }
    func find(sub:String, start:Int = 0, end:Int? = nil) -> Int {
        var s = self as NSString
        var s_temp = s.substringFromIndex(start)
        let end_temp:Int = end != nil ? end! : s.length
        for i in start..<end_temp {
            if (s_temp.hasPrefix(sub)) {
                return i
            }
            s_temp = s.substringFromIndex(i + 1) as NSString
        }
        return -1
    }
    func indexAt(i:Int) -> String {
        if i >= 0 {
            return (self as NSString).substringWithRange(NSRange(location: i, length: 1))
        }
        let new_i = self.length + i
        return (self as NSString).substringWithRange(NSRange(location: new_i, length: 1))
    }
    func replace(old_str:String, new_str:String) -> String {
        return self.stringByReplacingOccurrencesOfString(old_str, withString: new_str)
    }
    func slice(start:Int=0, end:Int?=nil) -> String {
        let s = self as NSString
        var new_end:Int
        if (end != nil) {
            new_end = end!
        } else {
            new_end = s.length
        }
        return  s.substringWithRange(NSRange(location: start, length:new_end - start))
    }
    func split(sep:String = "") -> [String] {
        var result:[String] = []
        if (sep == "") {
            for ch in self {
                result.append(String(ch))
            }
            return result
        }
        var s:String = self
        var len = sep.length
        var temp:String = ""
        while s != "" {
            if s.hasPrefix(sep) {
                if temp != "" {
                    result.append(temp)
                    temp = ""
                }
                s = s.slice(start: len)
            } else {
                temp += s.slice(start: 0, end: 1)
                s = s.slice(start: 1)
            }
        }
        if temp != "" {
            result.append(temp)
        }
        return result
    }
    var reversed:String { return "".join(self.split().reverse()) }
    func repeat(n:Int) -> String {
        var result:String = ""
        for i in 0..<n {
            result += self
        }
        return result
    }
}

func * (left:String, right:Int) -> String{
    return left.repeat(right)
}

func * (left:Int, right:String) -> String{
    return right.repeat(left)
}

入出力結果(Console Output, Terminal)

検索結果:
--------------------------------------------------
William: SBM GSHO likes sports, TV, dining
Josh: SJM likes sports, movies and theater
--------------------------------------------------
検索結果:
--------------------------------------------------
William: SBM GSHO likes sports, TV, dining
Mike: DWM DS likes trucks, sports and bieber
Peter: SAM likes chess, working out and art
Josh: SJM likes sports, movies and theater
--------------------------------------------------
検索結果:
--------------------------------------------------
Matt: SUM NS likes art, movies, theater
--------------------------------------------------
検索結果:
--------------------------------------------------
William: SBM GSHO likes sports, TV, dining
Matt: SUM NS likes art, movies, theater
Luis: SLM ND likes books, theater, art
Peter: SAM likes chess, working out and art
Josh: SJM likes sports, movies and theater
Jed: DBM likes theater, books and dining
--------------------------------------------------
Program ended with exit code: 0

0 コメント:

コメントを投稿