Skip to content

Latest commit

 

History

History
273 lines (190 loc) · 5.88 KB

slides.org

File metadata and controls

273 lines (190 loc) · 5.88 KB

Functional Programming in Scala

Functional Programming

Inspiration

Or, let’s face it… copying

Runar Bjarnasson - Introduction to Functional Programming

Who am I?

  • Bill Carlson
  • Innovation Developer at Cotiviti Labs
  • I get paid to write purely functional programs!

What is Functional Programming?

Programming with Functions

What is a Function?

A function f: A => B maps any value from one set to exactly one value in another set.

And nothing else

What does that mean?

  • Totality: A function will return a single value for all values of A.
  • Determinism: Every time you call a function with the same arguments, you will always get the same result.
  • Purity: The result of the function is the only effect.

Referential Tranparency

You can replace all occurrences of a function call with the result of that call without changing the program.

Example

val string = "some text"
val s1 = string.reverse
val s2 = string.reverse
val s3 = s1 + s2
val sb = new StringBuilder("some text")
val s1 = sb.reverse
val s2 = sb.reverse
val s3 = s1 append s2

What about…

  • Console?
  • File access?
  • Network?
  • Exceptions?

Higher-Order Functions

Oh, boy… live code…

Data Structures

List

Empty List: Nil

Non-empty list (cons): A :: List[A]

To build large lists, just add to a smaller list:

val list1 = 2 :: 3 :: 4 :: 5 :: Nil  // List(2, 3, 4, 5)

val list2 = 1 :: list1

It’s time for more code!

Option[A]

  • Some[A]
  • None
def fold[A](o: Option[A], z: B)(f: A => B): B = o match {
  case Some(a) => f(a)
  case None => z
}

Either[E, A]

  • Left[E]
  • Right[A]
def fold[E, A](e: Either[E, A], z: E => B, f: A => B): B = e match {
  case Left(e) => z(e)
  case Right(a) => f(a)
}

Interpreters

Can this be extended?

Types

What function is this?

def ????(i: Int): Int

def inc(i: Int) = i + 1

def timesTwo(i: Int) = i * 2

def abs(i: Int) = if (i < 0) -i else i

???

def ????[A](i: A): A

def identity[A](i: A): A = i

[T]he purpose of abstracting is not to be vague, but to create a new semantic level in which one can be absolutely precise.

– Edsger W. Dijkstra, “The Humble Programmer”

Algebras

What about…

  • Console?
  • File access?
  • Network?
  • Exceptions?

Console

sealed trait Console[A]
case class Print(s: String) extends Console[Unit]
case object Read extends Console[Option[String]]

File

sealed trait File[A]
case class Open(p: Path) extends File[Unit]
case class Write(data: Array[Byte]) extends File[Unit]
case object Read extends File[Array[Byte]]
case object Truncate extends File[Unit]

And so on…

An algebra is an abstract set of operations

Provides laws which must hold true

Using algebras, combinators, and folds, we simplify (evaluate) the program to a single value.

…maybe a good topic for next time?

?

Thank you!

Bill Carlson

[email protected]

Twitter: @coacoas

https://github.com/coacoas