Created
November 16, 2011 02:23
-
-
Save seratch/1369078 to your computer and use it in GitHub Desktop.
#daimonscala 22 ハンズオン
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ----------------- | |
| // #daimonscala 22 ハンズオン | |
| // ----------------- | |
| // 1. ListからMapをつくろう | |
| val list = List("Scala", "http://scala-lang.org", "Java", "http://java.net/", "Haskell", "http://haskell.org/") | |
| // Map(Scala -> http://scala-lang.org, Java -> http://java.net/, Haskell -> http://haskell.org/) | |
| // ----------------- | |
| // 2. ユニークに絞り込もう | |
| val list = List(1,2,1,3,2,4,4,2,4,3) | |
| // List(1, 2, 3, 4) | |
| // ----------------- | |
| // 3. 並び替えて平方にしよう | |
| val list = List(2,3,0,1,5,4) | |
| // List(0, 1, 4, 9, 16, 25) | |
| // ----------------- | |
| // 4. for式を使って以下のようなListをつくろう | |
| List( | |
| (1,1,1), | |
| (2,1,2), | |
| (2,2,4), | |
| (3,1,3), | |
| (3,2,6), | |
| (3,3,9), | |
| (4,1,4), | |
| (4,2,8), | |
| (4,3,12), | |
| (4,4,16), | |
| (5,1,5), | |
| (5,2,10), | |
| (5,3,15), | |
| (5,4,20), | |
| (5,5,25) | |
| ) | |
| // ----------------- | |
| // 5. CSVを出力しよう | |
| val h = List("name","lang","country") | |
| val ds = List(("Andy","Scala","Japan"),("Brian","Python","America"),("Samir","Java","India")) | |
| // name,lang,country | |
| // Andy,Scala,Japan | |
| // Brian,Python,America | |
| // Samir,Java,India | |
| // ----------------- | |
| // 6. 未成年と成人に分類しよう | |
| case class Person(name: String, age: Int) | |
| val people = List( | |
| Person("Andy",34), | |
| Person("Bob",18), | |
| Person("Charles",45), | |
| Person("Eric",13) | |
| ) | |
| // adults: List[Person] = List(Person(Andy,34), Person(Charles,45)) | |
| // minors: List[Person] = List(Person(Bob,18), Person(Eric,13)) | |
| // ----------------- | |
| // 7. 並列コレクションを使ってみよう(Scala 2.9.0以降) | |
| Range(1,100).par foreach { e => print(e +",") } | |
| // ----------------- | |
| // 8. flatMapを使ってみよう1 | |
| val input = List(1,2,3,"a","3",4) | |
| val square = (a: Any) => a match { | |
| case i: Int => Some(i*i) | |
| case _ => None | |
| } | |
| // List(1, 4, 9, 16) | |
| // ----------------- | |
| // 9. flatMapを使ってみよう2 | |
| case class Device(name: String) | |
| case class User(name: String, devices: List[Device]) | |
| val users = List( | |
| User("Andy", List(Device("iPhone"), Device("iPad"))), | |
| User("Brian", List(Device("iPhone"))), | |
| User("Charles", List(Device("Android"), Device("iPhone"))), | |
| User("Dennis", List(Device("iPad"), Device("Android"), Device("iPad"))), | |
| User("Eric", Nil) | |
| ) | |
| // Map(iPad -> 3, iPhone -> 3, Android -> 2) | |
| // ----------------- | |
| // 10. andThenを使ってUNIXパイプのような処理を書いてみよう | |
| val emails = List( | |
| "[email protected]", | |
| "[email protected]", | |
| "[email protected]", | |
| "[email protected]", | |
| "[email protected]", | |
| "[email protected]", | |
| "[email protected]", | |
| "[email protected]", | |
| "[email protected]", | |
| "[email protected]" | |
| ) | |
| // $ cat emails.txt | |
| // [email protected] | |
| // [email protected] | |
| // [email protected] | |
| // [email protected] | |
| // [email protected] | |
| // [email protected] | |
| // [email protected] | |
| // [email protected] | |
| // [email protected] | |
| // [email protected] | |
| // $ awk -F@ '{print $2}' emails.txt | sort | uniq -c | |
| // 1 aol.com | |
| // 1 facebook.com | |
| // 3 gmail.com | |
| // 2 hotmail.com | |
| // 1 yahoo.co.jp | |
| // 2 yahoo.com | |
| // | |
| // Map(hotmail.com -> 2, gmail.com -> 3, aol.com -> 1, facebook.com -> 1, yahoo.co.jp -> 1, yahoo.com -> 2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment