Skip to content

Instantly share code, notes, and snippets.

@seratch
Created November 16, 2011 11:58
Show Gist options
  • Save seratch/1369918 to your computer and use it in GitHub Desktop.
Save seratch/1369918 to your computer and use it in GitHub Desktop.
#daimonscala 22 My Work
// -----------------
// #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)
// -----------------
/*
(回答例)
1.
val list = List("Scala", "http://scala-lang.org", "Java", "http://java.net/", "Haskell", "http://haskell.org/")
list sliding(2,2) map { case e => (e(0), e(1)) } toMap
2.
val list = List(1,2,1,3,2,4,4,2,4,3)
list distinct
3.
val list = List(2,3,0,1,5,4)
list sortWith(_<_) map { i => i * i }
for(i <- list sorted) yield i * i
4.
(for(i <- 1 to 5; j <- 1 to i; k = i*j) yield (i,j,k)).toList
5.
val h = List("name","lang","country")
val ds = List(("Andy","Scala","Japan"),("Brian","Python","America"),("Samir","Java","India"))
println(h.mkString(","))
ds map { d => d.productIterator mkString(",") } foreach println
6.
case class Person(name: String, age: Int)
val people = List(
Person("Andy",34),
Person("Bob",18),
Person("Charles",45),
Person("Eric",13)
)
val (adults, minors) = people partition { p => p.age > 20 }
8.
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,2,3,"a","3",4) flatMap { i => square(i) }
9.
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)
)
users flatMap { user => user.devices } groupBy { _.name } map { case (name, list) => (name, list.size) }
10.
val emails = List(
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]"
)
val extractDomain = (emails: List[String]) => emails map { case email => email.split("@").tail.head }
val sort = (domains: List[String]) => domains sortWith { _ < _ }
val uniqCount = (domains: List[String]) => domains groupBy { domain => domain } map { case (name, list) => (name, list.size) }
val main = extractDomain andThen sort andThen uniqCount
main(emails)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment