kotlin
  • Kotlin
  • Variable
  • Function
  • First Class Citizen
  • High Order Function
  • Pure Function
  • Call by Value, Call by Name
  • Null Safe
  • Generic
  • Expression
    • if - else
    • when
    • try - catch
    • while
    • for
  • Class
    • abstract
    • constructor
    • Data Class
    • Default Value
    • enum
    • inheritance
    • interface
    • open
    • property
    • sealed
  • Kotlin Standard
    • let
    • with
    • apply
    • run
    • also
    • takeIf
    • takeUnless
    • use
    • repeat
  • Collections
    • Mutable
    • Immutable
    • Collections
    • Aggregate operations
      • any
      • all
      • count
      • fold
      • foldRight
      • fold vs foldRight
      • forEach
      • forEachIndexed
      • max
      • maxBy
      • min
      • minBy
      • none
      • reduce
      • reduceRight
      • sum
      • sumBy
    • Filtering operations
      • drop
      • dropLast
      • dropWhile
      • dropLastWhile
      • filter
      • filterNot
      • slice
      • take
      • takeLast
      • takeWhile
    • Mapping operations
      • map
      • mapIndexed
      • flatMap
      • groupBy
    • Elements operations
      • contains
      • elementAt
      • elementAtOrElse
      • elementAtOrNull
      • first
      • firstOrNull
      • indexOf
      • indexOfFirst
      • indexOfLast
      • last
      • lastIndexOf
      • lastOrNull
      • single
      • singleOrNull
    • Generation operations
      • partition
      • plus
      • minus
      • zip
      • unzip
    • Ordering operations
      • reversed
      • sorted
      • sortedBy
      • sortedDescending
      • sortedByDescending
Powered by GitBook
On this page

Pure Function

Pure Function, 순수함수라는 것은 어떠한 함수에 A 라는 입력을 했을때 횟수에 관계없이 언제나, 몇 번을 실행하더라도 같은 입력에는 같은 출력값이 나오는 함수를 말한다.

//순수함수의 예
fun pureFunction(str: String): String = str + "Test"

function("Kotlin")   // "KotlinTest"
function("Kotlin")   // "KotlinTest"
function("Kotlin")   // "KotlinTest"
function("Kotlin")   // "KotlinTest"
//비 순수함수의 예
val sb = Stringbuilder()

fun nonPureFuction(str: String): String = str + sb.append("Test").toString()

println(nonPureFuction("Kotlin"))  //KotlinTest
println(nonPureFuction("Kotlin"))  //KotlinTestTest
println(nonPureFuction("Kotlin"))  //KotlinTestTestTest
println(nonPureFuction("Kotlin"))  //KotlinTestTestTestTest

비순수함수는 순수함수보다 안전하지 못하다.

PreviousHigh Order FunctionNextCall by Value, Call by Name

Last updated 6 years ago