2014年9月10日水曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、 Elisabeth Robson (著)、 O'Reilly Media )のChapter 10(Liberated functions: First Class Functions)、EXERCISE(p.448)をSwiftで考えてみる。

EXERCISE(p.448)

コード(Xcode)

main.swift

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

import Foundation

struct Passenger {
    let name:String
    let paid:Bool
}

let passengers:[Passenger] = [
    Passenger(name: "Jane Doloop", paid: true),
    Passenger(name: "Dr. Evel", paid: true ),
    Passenger(name: "Sue Property", paid: false),
    Passenger(name: "John Funcall", paid: true)
]

func checkNotPaid(passenger:Passenger) -> Bool {
    return !passenger.paid
}

func processPassengers(passengers:[Passenger], testFunc:(Passenger) -> Bool) -> Bool {
    for passenger in passengers {
        if testFunc(passenger) {
            return false
        }
    }
    return true
}

func printPassenger(passenger:Passenger) -> Bool {
    if checkNotPaid(passenger) {
        println("\(passenger.name) have not paid.")
    } else {
        println("\(passenger.name) have paid")
    }
    return false
}

processPassengers(passengers, printPassenger)

入出力結果(Console Output)

Jane Doloop have paid
Dr. Evel have paid
Sue Property have not paid.
John Funcall have paid
Program ended with exit code: 0

0 コメント:

コメントを投稿