In FP, contracts don't lie

Let's look an example of some imperative code:

type Dividend struct {   Val int}func (n Dividend) Divide(divisor int) int {   return n.Val/divisor}func main() {   d := Dividend{2}   fmt.Printf("%d", d.Divide(0))}

What is our contract in the preceding code?

The contract is our method's signature: func (n Dividend) Divide(divisor int) int

What three questions must our contract answer?

  1. What does our contract expect? 
  • Answer: It expects the following: 
    • The Dividend.Val to be populated with an int
    • The divisor to be an int
  1. What does our contract guarantee? 
  • Answer: It promises to return an integer
  1. What does the contract maintain? 
  • Answer: Not applicable in this simple case

What happens when we run the preceding ...

Get Learning Functional Programming in Go now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.