開発環境
- OS X Mavericks - Apple(OS)
- Xcode 6.0 Beta
- Swift (プログラミング言語)
Head First JavaScript Programming (Eric T. Freeman (著)、 Elisabeth Robson (著)、 O'Reilly Media )のChapter 10(Liberated functions: First Class Functions)、EXERCISE(p.463)をSwiftで考えてみる。
EXERCISE(p.463)
コード(Xcode)
main.swift
//
// main.swift
// sample463
//
// Created by kamimura on 9/15/14.
// Copyright (c) 2014 kamimura. All rights reserved.
//
import Foundation
struct Cola : Printable {
let name:String
let calories:Int
let color:String
let sold:Int
var description:String { return "Name: \(name), Calories: \(calories), Color: \(color), Sold: \(sold)"}
}
var products:[Cola] = [
Cola( name: "Grapefruit", calories: 170, color: "red", sold: 8200 ),
Cola( name: "Orange", calories: 160, color: "orange", sold: 12101 ),
Cola( name: "Cola", calories: 210, color: "caramel", sold: 25412 ),
Cola( name: "Diet Cola", calories: 0, color: "caramel", sold: 43922 ),
Cola( name: "Lemon", calories: 200, color: "clear", sold: 14983 ),
Cola( name: "Raspberry", calories: 180, color: "pink", sold: 9427 ),
Cola( name: "Root Beer", calories: 200, color: "caramel", sold: 9909 ),
Cola( name: "Water", calories: 0, color: "clear", sold: 62123 )
]
func compareName(colaA:Cola, colaB:Cola) -> Bool {
return colaA.name < colaB.name
}
func compareCalories(colaA:Cola, colaB:Cola) -> Bool {
return colaA.calories < colaB.calories
}
func compareColor(colaA:Cola, colaB:Cola) -> Bool {
return colaA.color < colaB.color
}
func printProducts(products:[Cola]) {
for product in products {
println(product)
}
}
products.sort(compareName)
println("Products sorted by name:")
printProducts(products)
products.sort(compareCalories)
println("Products sorted by calroies:")
printProducts(products)
products.sort(compareColor)
println("products sorted by color:")
printProducts(products)
入出力結果(Console Output)
Products sorted by name: Name: Cola, Calories: 210, Color: caramel, Sold: 25412 Name: Diet Cola, Calories: 0, Color: caramel, Sold: 43922 Name: Grapefruit, Calories: 170, Color: red, Sold: 8200 Name: Lemon, Calories: 200, Color: clear, Sold: 14983 Name: Orange, Calories: 160, Color: orange, Sold: 12101 Name: Raspberry, Calories: 180, Color: pink, Sold: 9427 Name: Root Beer, Calories: 200, Color: caramel, Sold: 9909 Name: Water, Calories: 0, Color: clear, Sold: 62123 Products sorted by calroies: Name: Diet Cola, Calories: 0, Color: caramel, Sold: 43922 Name: Water, Calories: 0, Color: clear, Sold: 62123 Name: Orange, Calories: 160, Color: orange, Sold: 12101 Name: Grapefruit, Calories: 170, Color: red, Sold: 8200 Name: Raspberry, Calories: 180, Color: pink, Sold: 9427 Name: Lemon, Calories: 200, Color: clear, Sold: 14983 Name: Root Beer, Calories: 200, Color: caramel, Sold: 9909 Name: Cola, Calories: 210, Color: caramel, Sold: 25412 products sorted by color: Name: Diet Cola, Calories: 0, Color: caramel, Sold: 43922 Name: Root Beer, Calories: 200, Color: caramel, Sold: 9909 Name: Cola, Calories: 210, Color: caramel, Sold: 25412 Name: Water, Calories: 0, Color: clear, Sold: 62123 Name: Lemon, Calories: 200, Color: clear, Sold: 14983 Name: Orange, Calories: 160, Color: orange, Sold: 12101 Name: Raspberry, Calories: 180, Color: pink, Sold: 9427 Name: Grapefruit, Calories: 170, Color: red, Sold: 8200 Program ended with exit code: 0
0 コメント:
コメントを投稿