Functional Programming “Design Pattern”
Design Pattern is some kind of techniques that GoF summarized that you can apply to solve particular software problem.

But there’s no such thing called “Design Pattern” in Functional Programming

There is only one Design Principle in FP

Remove all side effects

To achieve that,  you have the ultimate Batman Category Theory utility belt


but lets start from…

A Simple Program

  • read file from a S3 bucket 
  • process file
  • upload file to another S3 bucket
which can be easily interpret into the following code.

val sourceBucketName = "source-bucket"
val fileName = "fileA"
val targetBucketName = "target-bucket"
val awsClient = AmazonS3ClientBuilder.standard().build()
def pureBusinessProcess(content: String): String = ???
def program = {
  val content = awsClient.getObject(sourceBucketName, fileName)
  val result = pureBusinessProcess(content)
  awsClient.putObject(targetBucketName, fileName)
}

Scala 101

val sourceBucketName = "source-bucket"
// ^ val define a name for a constant, type is auto infer by compiler
val fileName = "fileA"
val targetBucketName = "target-bucket"
val awsClient = AmazonS3ClientBuilder.standard().build()
//                              not implemented yet v
def pureBusinessProcess(content: String): String = ???
// ^ def will define a method  ^        ^
//                             ^   parameter type annotation
//   parameter type annotation ^ 
def program = {
//            ^ a block: value of last expression will be returned
  val content = awsClient.getObject(sourceBucketName, fileName)
  val result = pureBusinessProcess(content)
  awsClient.putObject(targetBucketName, fileName)
}

It’s very clean and readable code, isn’t it

but readable doesn’t change the fact that

It’s a shitty piece of code on production