Share
Embed
Functional Programming Is Awesome
An introduction/overview of the properties of functional programming, followed by an exploration of some benefits. Code examples are in either javascript or scala.
Kyle Willett
May 18, 2016
Transcript
93 slides
-
First: the properties of functional programming Second: the benefits of
functional programming -
First Class Functions There is no restriction on the use
of functions. -
First Class Functions There is no restriction on the use
of functions. 1) Return results can be functions. (i.e. Functions can return functions.) -
First Class Functions There is no restriction on the use
of functions. 1) Return results can be functions. (i.e. Functions can return functions.) -
First Class Functions There is no restriction on the use
of functions. 1) Return results can be functions. (i.e. Functions can return functions.) -
First Class Functions There is no restriction on the use
of functions. 1) Return results can be functions. (i.e. Functions can return functions.) 2) Arguments can be functions. (i.e. Functions can take functions.) -
First Class Functions There is no restriction on the use
of functions. 1) Return results can be functions. (i.e. Functions can return functions.) 2) Arguments can be functions. (i.e. Functions can take functions.) -
First Class Functions There is no restriction on the use
of functions. 1) Return results can be functions. (i.e. Functions can return functions.) 2) Arguments can be functions. (i.e. Functions can take functions.) -
First Class Functions There is no restriction on the use
of functions. 1) Return results can be functions. (i.e. Functions can return functions.) 2) Arguments can be functions. (i.e. Functions can take functions.) makeAdd and addAndMultiply are higher-order functions. -
Referential Transparency The values of expressions do not change. •
They are context independent. (doesn’t matter when or where) -
Referential Transparency The values of expressions do not change. •
They are context independent. (doesn’t matter when or where) • The same input produces same output. (predictable, deterministic program) -
Referential Transparency The values of expressions do not change. •
They are context independent. (doesn’t matter when or where) • The same input produces same output. (predictable, deterministic program) -
Referential Transparency The values of expressions do not change. •
They are context independent. (doesn’t matter when or where) • The same input produces same output. (predictable, deterministic program) -
Referential Transparency The values of expressions do not change. •
They are context independent. (doesn’t matter when or where) • The same input produces same output. (predictable, deterministic program) -
Referential Transparency The values of expressions do not change. •
They are context independent. (doesn’t matter when or where) • The same input produces same output. (predictable, deterministic program) -
Referential Transparency The values of expressions do not change. •
They are context independent. (doesn’t matter when or where) • The same input produces same output. (predictable, deterministic program) -
Referential Transparency The values of expressions do not change. •
They are context independent. (doesn’t matter when or where) • The same input produces same output. (predictable, deterministic program) -
Referential Transparency The values of expressions do not change. •
They are context independent. (doesn’t matter when or where) • The same input produces same output. (predictable, deterministic program) RT just doesn’t work for imperative programming. -
No Side Effects Side effect free functions: • Don’t modify
state outside of their scope. -
No Side Effects Side effect free functions: • Don’t modify
state outside of their scope. • Don’t have any observable interaction with their surrounding context. -
No Side Effects Side effect free functions: • Don’t modify
state outside of their scope. • Don’t have any observable interaction with their surrounding context. • Are idempotent. -
No Side Effects Side effect free functions: • Don’t modify
state outside of their scope. • Don’t have any observable interaction with their surrounding context. • Are idempotent. • Are eligible for referential transparency. -
No Side Effects Side effect free functions: • Don’t modify
state outside of their scope. • Don’t have any observable interaction with their surrounding context. • Are idempotent. • Are eligible for referential transparency. -
No Side Effects Side effect free functions: • Don’t modify
state outside of their scope. • Don’t have any observable interaction with their surrounding context. • Are idempotent. • Are eligible for referential transparency. -
No Side Effects Side effect free functions: • Don’t modify
state outside of their scope. • Don’t have any observable interaction with their surrounding context. • Are idempotent. • Are eligible for referential transparency. -
Expressive Declarative programming: • Expresses what to do, not how
to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) -
Expressive Declarative programming: • Expresses what to do, not how
to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation. -
Expressive Declarative programming: • Expresses what to do, not how
to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation. -
Expressive Declarative programming: • Expresses what to do, not how
to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation. -
Expressive Declarative programming: • Expresses what to do, not how
to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation. -
Expressive Declarative programming: • Expresses what to do, not how
to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation. -
Expressive Declarative programming: • Expresses what to do, not how
to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation. Abstractions over evaluation are used to model effects. -
Expressive Declarative programming: • Expresses what to do, not how
to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation. Abstractions over evaluation are used to model effects. -
Expressive Declarative programming: • Expresses what to do, not how
to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation. Abstractions over evaluation are used to model effects. -
Expressive Declarative programming: • Expresses what to do, not how
to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation. Abstractions over evaluation are used to model effects. -
Expressive Declarative programming: • Expresses what to do, not how
to do it. • Is not sequential instructions. (doesn’t describe control flow of execution) • Achieves separation of concerns between description and implementation. Abstractions over evaluation are used to model effects. -
Composable Build complexity and remain expressive: • Assemble building blocks
of data types and functions. -
Composable Build complexity and remain expressive: • Assemble building blocks
of data types and functions. • Use a toolbox of core, reusable, and proven tools -
Composable Build complexity and remain expressive: • Assemble building blocks
of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. -
Composable Build complexity and remain expressive: • Assemble building blocks
of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well. -
Composable Build complexity and remain expressive: • Assemble building blocks
of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well. -
Composable Build complexity and remain expressive: • Assemble building blocks
of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well. -
Composable Build complexity and remain expressive: • Assemble building blocks
of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well. -
Composable Build complexity and remain expressive: • Assemble building blocks
of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well. -
Composable Build complexity and remain expressive: • Assemble building blocks
of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well. -
Composable Build complexity and remain expressive: • Assemble building blocks
of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well. -
Composable Build complexity and remain expressive: • Assemble building blocks
of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well. -
Composable Build complexity and remain expressive: • Assemble building blocks
of data types and functions. • Use a toolbox of core, reusable, and proven tools • Create new computations quickly from existing ones. • Utilize simple components that do one thing well. The goal: not heaps of special cases and tangled conditions, but an expressive domain composed of simple parts. -
Modular High cohesion and loose coupling: • Immutable data simplifies
concurrency. • Functional abstractions separate concerns elegantly. -
Modular High cohesion and loose coupling: • Immutable data simplifies
concurrency. • Functional abstractions separate concerns elegantly. • Required reasoning about code is reduced and localized. -
Modular High cohesion and loose coupling: • Immutable data simplifies
concurrency. • Functional abstractions separate concerns elegantly. • Required reasoning about code is reduced and localized. • Pure functions can be easily reused. -
Testable • Does an input equal an expected output? No
mocking required. -
Testable • Does an input equal an expected output? No
mocking required. • Previously tested “tools” can be used with confidence. -
Testable • Does an input equal an expected output? No
mocking required. • Previously tested “tools” can be used with confidence. • Properties can be verified with property-based testing. -
Testable • Does an input equal an expected output? No
mocking required. • Previously tested “tools” can be used with confidence. • Properties can be verified with property-based testing. • Properties expressed by types are automatically “tested” by the compiler. -
Learn More Functional Programming in Scala by Paul Chiusano and
Rúnar Bjarnason (on Safari) Functional and Reactive Domain Modeling by Debasish Ghosh (Early Access) 4 upcoming on-demand coursera courses from the Scala Center at EPFL