0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Scala List : 高階演算子

Posted at

高階演算子

別の関数をパラメーターとして受け取る関数。

Sample

    val words = List("We", "are", "Scala", "!")
    val oneToFile = List(1, 2, 3, 4, 5)
    val minusFiveToFive = List(-5, -4, -3, -2, 1, 0, 1, 2, 3, 4, 5)
    val zeroList = List(0, 0, 0, 0, 0)
    var value = 0

    // map
    val m = words map (_.toList)
    println(m) // List(List(W, e), List(a, r, e), List(S, c, a, l, a), List(!))

    // flatMap
    val fMap = words flatMap (_.toList)
    println(fMap) // List(W, e, a, r, e, S, c, a, l, a, !)

    // foreach
    oneToFile foreach (value += _)
    println(value) // 15

    // filter
    val fil = oneToFile filter (_ % 2 == 0)
    println(fil) // List(2, 4)

    // partition
    val par = oneToFile partition (_ % 2 == 0)
    println(par) // (List(2, 4),List(1, 3, 5))

    // find
    val fin = oneToFile find (_ % 2 == 0)
    println(fin) // Some(2)

    // takeWhile
    val tWhile = minusFiveToFive takeWhile (_ < 0)
    println(tWhile) // List(-5, -4, -3, -2)

    // dropWhile
    val dWhile = minusFiveToFive dropWhile (_ < 0)
    println(dWhile) // List(1, 0, 1, 2, 3, 4, 5)

    // span
    val spa = minusFiveToFive span (_ < 0)
    println(spa) // (List(-5, -4, -3, -2),List(1, 0, 1, 2, 3, 4, 5))

    // forall
    val fAll = List(0, 0, 0, 0, 0) forall (_ == 0)
    println(fAll) // true

    // exists
    val exi = List(1, 1, 0, 1, 1) exists (_ == 0)
    println(exi) // true

    // foldLeft
    val fLeft = oneToFile.foldLeft(0)((x, y) => x + y)
    println(fLeft) // 15

    // foldRight
    val fRight = oneToFile.foldRight(0)((x, y) => x + y)
    println(fRight) // 15

    // sortWith
    val sWith = words sortWith (_.length > _.length)
    println(sWith) // List(Scala, are, We, !)

GitHub

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?