have-you-heard



have-you-heard

0 0


have-you-heard

Short talk (pecha kucha style) on functional programming

On Github mdjnewman / have-you-heard

Hello friend! Have you heard the good news?

(a.k.a. intro to functional programming)

by Matt Newman

There is a widening gap between ambitions and achievements in software engineering … the gap is arising at a time when the consequences of software failure in all its aspects are becoming increasingly serious

Production of large software has become a scare item for management. By reputation it is often an unprofitable morass, costly and unending.

The good systems that are presently working were written by small groups. More than twenty programmers working on a project is usually disastrous.

λ?

(source)

Single responsibility principle

A class function should have only one reason to change.

Small, focused functions are good too!

Open/closed principle

Software entities should be open for extension, but closed for modification.

Composition and higher-order functions to the rescue!

( f ∘ g )( x ) = f ( g( x ) )

Liskov substitution principle

Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.

Don't surprise people!

Also, parametric polymorphism largely avoids these issues.

Interface segregation principle

Clients should not be forced to depend upon interfaces that they do not use.

Specifying small data types is still good practice.

Dependency inversion principle

Depend upon abstractions. Do not depend upon concretions.

FP (especially Haskell) is all about abstractions!

(and IOC containers are just the poor cousin of partial functions)

$ let identity = \x -> x

$ identity "Hallo!"
"Hallo!"

$ (identity . identity) "Hallo!"
"Hallo!"

Parametricity

$ :t identity
identity :: t -> t

a.k.a. money for nothing and your theorems for free

Hoogle & Hayoo

Worlds best imperative language

main :: IO ()
main = do putStrLn "Hello, what is your name?"
          name <- getLine
          putStrLn ("Hello, " ++ name ++ "!")

Higher-order functions - map

upperCaseStrings = strings.stream()
      .map(String::toUpperCase)
      .collect(toList());

Higher-order functions - filter

nonEmptyStrings = strings.stream()
     .filter(x -> !x.isEmpty())
     .collect(toList());

Higher-order functions - reduce

sum = numbers.stream().reduce(0, Integer::sum);

or

sum = numbers.parallelStream().reduce(0, Integer::sum);

Free parallelism too!

(source)

Hello friend! Have you heard the good news? (a.k.a. intro to functional programming) by Matt Newman