Skip to main content
added 1 characters in body
Source Link
Dakotah North
  • 3.4k
  • 1
  • 23
  • 32

The first step to be able to recognize the difference between imperative and functional styles in code. For example, Odersky in Programming in Scala highlights that one telltale sign is

that if code contains any vars [variables in Scala that can be modified] is is probably imperative style. If the code contains no vars at all--i.e., it contains only vals--it is probably in a functional style. One way to move towards a functional style, therefore, i s to try to program without vars.

For example, consider this while loop:

var i = 0 while (i < args.length) { println(args(i)) i+=1 } 

The var is the sign it is imperative. This is Scala would become

def printargs(args: Array[String]): Unit = { args.foreach(println) } 

But this code isn't purely functional yet because it has side effects.

From a functional point of view this should be:

def formatArgs(args: Array[String]) = args.mkString("\n")

def formatArgs(args: Array[String]) = args.mkString("\n") 

So ... to determine if you know functional programming, are you writing code without the equivalent of vars and no local side effects!?

The first step to be able to recognize the difference between imperative and functional styles in code. For example, Odersky in Programming in Scala highlights that one telltale sign is

that if code contains any vars [variables in Scala that can be modified] is is probably imperative style. If the code contains no vars at all--i.e., it contains only vals--it is probably in a functional style. One way to move towards a functional style, therefore, i s to try to program without vars.

For example, consider this while loop:

var i = 0 while (i < args.length) { println(args(i)) i+=1 } 

The var is the sign it is imperative. This is Scala would become

def printargs(args: Array[String]): Unit = { args.foreach(println) } 

But this code isn't purely functional yet because it has side effects.

From a functional point of view this should be:

def formatArgs(args: Array[String]) = args.mkString("\n")

So ... to determine if you know functional programming, are you writing code without the equivalent of vars and no local side effects!?

The first step to be able to recognize the difference between imperative and functional styles in code. For example, Odersky in Programming in Scala highlights that one telltale sign is

that if code contains any vars [variables in Scala that can be modified] is is probably imperative style. If the code contains no vars at all--i.e., it contains only vals--it is probably in a functional style. One way to move towards a functional style, therefore, i s to try to program without vars.

For example, consider this while loop:

var i = 0 while (i < args.length) { println(args(i)) i+=1 } 

The var is the sign it is imperative. This is Scala would become

def printargs(args: Array[String]): Unit = { args.foreach(println) } 

But this code isn't purely functional yet because it has side effects.

From a functional point of view this should be:

def formatArgs(args: Array[String]) = args.mkString("\n") 

So ... to determine if you know functional programming, are you writing code without the equivalent of vars and no local side effects!?

Source Link
Dakotah North
  • 3.4k
  • 1
  • 23
  • 32

The first step to be able to recognize the difference between imperative and functional styles in code. For example, Odersky in Programming in Scala highlights that one telltale sign is

that if code contains any vars [variables in Scala that can be modified] is is probably imperative style. If the code contains no vars at all--i.e., it contains only vals--it is probably in a functional style. One way to move towards a functional style, therefore, i s to try to program without vars.

For example, consider this while loop:

var i = 0 while (i < args.length) { println(args(i)) i+=1 } 

The var is the sign it is imperative. This is Scala would become

def printargs(args: Array[String]): Unit = { args.foreach(println) } 

But this code isn't purely functional yet because it has side effects.

From a functional point of view this should be:

def formatArgs(args: Array[String]) = args.mkString("\n")

So ... to determine if you know functional programming, are you writing code without the equivalent of vars and no local side effects!?