introduction to functional programming in scala

20
Functional Programming with Scala Introduction by @JacekLaskowski

Upload: jacek-laskowski

Post on 22-Jun-2015

644 views

Category:

Technology


7 download

DESCRIPTION

Presentation from my talk at JEEConf (http://jeeconf.com) in Kiev, Ukraine on 24th of May, 2013

TRANSCRIPT

Page 1: Introduction to Functional Programming in Scala

Functional Programming with ScalaIntroduction by @JacekLaskowski

Page 2: Introduction to Functional Programming in Scala

Jacek Laskowski@JacekLaskowskihttp://blog.japila.pl

Page 4: Introduction to Functional Programming in Scala

HTTP://WWW.ATOMICSCALA.COM/

Page 6: Introduction to Functional Programming in Scala

Functional programmingIn computer science, functional programming

is a programming paradigm that treats computation as the evaluation of

mathematical functions and avoids state and mutable data. It emphasizes the application of

functions, in contrast to the imperative programming style, which emphasizes changes in state.

HTTP://EN.WIKIPEDIA.ORG/WIKI/FUNCTIONAL_PROGRAMMING

Page 7: Introduction to Functional Programming in Scala

Functional programming•Programming paradigm

•Computation = function evaluation

•Avoids state and mutable data

•Function composition

•Expressions everywhere

Page 8: Introduction to Functional Programming in Scala

Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages.

HTTP://WWW.SCALA-LANG.ORG/NODE/104

Scala is also a functional language in the sense that every function is a value.

Page 9: Introduction to Functional Programming in Scala

Keyword to define a Method

Method’s name

Method’s input parameters and

their typesMethod’s

return type (optional)

Method’s body

Page 10: Introduction to Functional Programming in Scala

Expression vs statement

•A statement changes state•it’s called for side effects

•An expression produces result

FROM “ATOMIC SCALA” BY BRUCE ECKEL AND DIANNE MARSH

Page 11: Introduction to Functional Programming in Scala

Everything in Scala is an expression

Page 12: Introduction to Functional Programming in Scala

Is this an expression?

YES!Is this an expression?

YES!

Page 13: Introduction to Functional Programming in Scala

Type => TypeFunction type

Page 14: Introduction to Functional Programming in Scala

Function type examples

•Int => String•(Int, Int) => Int•Int => Int => Int•(Int => Int) => Int

Page 15: Introduction to Functional Programming in Scala

Scala REPL

Page 16: Introduction to Functional Programming in Scala

Collections in Scala

•Vector

•Set

•Map

•Range

Page 17: Introduction to Functional Programming in Scala

Functional trio

•map

•filter

•reduce (foldLeft + foldRight)

Page 18: Introduction to Functional Programming in Scala

Function literal

Page 19: Introduction to Functional Programming in Scala

Function composition

Page 20: Introduction to Functional Programming in Scala

Jacek Laskowski@JacekLaskowskihttp://blog.japila.pl

Pitannja?