Functions are Non-Imperative

Imperative == order dependant


fun fixChatMessageImperative(
    messages: List<ChatMessage>) : List<String> { 
  var fixedMessages: List<String> = ArrayList()
  for (m in messages){ 
    var fixedMessage: String? = m.message
    
              
    if (m.message.contains("bad word"){ 
      fixedMessage = m.message.replace("bad word", "****")
    }
    if (m.user == "SYSTEM"){ 
      fixedMessage = null
    }
    if (fixedMessage != null){ 
      fixedMessage = fixedMessage.toLowerCase()
    }
    
              
    if (fixedMessage != null){ 
      fixedMessages += fixedMessage
    }
  }
  return fixedMessages
}


fun fixChatMessagesNotImperative(
    messages: List<ChatMessage>) : List<String> = 
  messages
    .filter {  user != "SYSTEM" }
    .map {  it.messages.replace("bad word", "****") }
    .map(String::toLowerCase)

Back Home Next