are you ready for functional programming javascript vs. scala · why scala? why javascript? high...

16
Are you ready for Are you ready for Are you ready for Are you ready for programming programming programming programming JavaScript vs. Sc JavaScript vs. Sc JavaScript vs. Sc JavaScript vs. Sc Elizabeta Ilievska r functional r functional r functional r functional cala cala cala cala Goran Kopevski

Upload: others

Post on 15-Jun-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Are you ready for functional programming JavaScript vs. Scala · Why Scala? Why Javascript? High Order Functions Closures Currying Tail recursion Immutability Lazy evaluation & Memoization

Are you ready for functional Are you ready for functional Are you ready for functional Are you ready for functional

programmingprogrammingprogrammingprogrammingprogrammingprogrammingprogrammingprogramming

JavaScript vs. ScalaJavaScript vs. ScalaJavaScript vs. ScalaJavaScript vs. Scala

Elizabeta Ilievska

Are you ready for functional Are you ready for functional Are you ready for functional Are you ready for functional

JavaScript vs. ScalaJavaScript vs. ScalaJavaScript vs. ScalaJavaScript vs. Scala

Goran Kopevski

Page 2: Are you ready for functional programming JavaScript vs. Scala · Why Scala? Why Javascript? High Order Functions Closures Currying Tail recursion Immutability Lazy evaluation & Memoization

AgendaAgendaAgendaAgenda

○ Why functional programming?

○ Imperative vs Functional programming

○ Why Scala? Why Javascript?

○ High Order Functions○ High Order Functions

○ Closures

○ Currying

○ Tail recursion

○ Immutability

○ Lazy evaluation & Memoization

○ Benefits & Conclusion

Page 3: Are you ready for functional programming JavaScript vs. Scala · Why Scala? Why Javascript? High Order Functions Closures Currying Tail recursion Immutability Lazy evaluation & Memoization

Why functional programming?Why functional programming?Why functional programming?Why functional programming?

○ Concurrency

○ Parallelism

○ Spend more time on thinking about the implications of results rather than how to generate them○ Spend more time on thinking about the implications of results rather than how to generate them

○ Limits of silicon technology reached

○ Safe ways of programming

○ Safe reuse of subprograms

○ Generic routines

and the list can go to infinity ….

Why functional programming?Why functional programming?Why functional programming?Why functional programming?

Spend more time on thinking about the implications of results rather than how to generate themSpend more time on thinking about the implications of results rather than how to generate them

Page 4: Are you ready for functional programming JavaScript vs. Scala · Why Scala? Why Javascript? High Order Functions Closures Currying Tail recursion Immutability Lazy evaluation & Memoization

Imperative vs. Functional programmingImperative vs. Functional programmingImperative vs. Functional programmingImperative vs. Functional programming

Imperative programming describes the steps to solve a problem.

Functional programming focuses on what needs to be done rather the steps that need to be taken.

Characteristic Imperative approachCharacteristic Imperative approach

Programmer focus How to perform tasks (algorithms) and

how to track changes in state.

State changes Important.

Order of execution Important.

Primary flow control Loops, conditionals, and function

(method) calls.

Primary manipulation unit Instances of structures or classes.

Imperative vs. Functional programmingImperative vs. Functional programmingImperative vs. Functional programmingImperative vs. Functional programming

Imperative programming describes the steps to solve a problem.

Functional programming focuses on what needs to be done rather the steps that need to be taken.

Functional approachFunctional approach

How to perform tasks (algorithms) and What information is desired and what

transformations are required.

Non-existent.

Low importance.

Loops, conditionals, and function Function calls, including recursion.

Instances of structures or classes. Functions as first-class objects and data collections.

Page 5: Are you ready for functional programming JavaScript vs. Scala · Why Scala? Why Javascript? High Order Functions Closures Currying Tail recursion Immutability Lazy evaluation & Memoization

Imperative vs. Functional programmingImperative vs. Functional programmingImperative vs. Functional programmingImperative vs. Functional programming

Imperative

Functional

Imperative vs. Functional programmingImperative vs. Functional programmingImperative vs. Functional programmingImperative vs. Functional programming

Imperative

Functional

Page 6: Are you ready for functional programming JavaScript vs. Scala · Why Scala? Why Javascript? High Order Functions Closures Currying Tail recursion Immutability Lazy evaluation & Memoization

Why Scala?Why Scala?Why Scala?Why Scala?

○ Cuts down the boilerplate○ Cuts down the boilerplate

○ Tightly fuses object-oriented and functional programming concepts

○ Runs on the JVM and interoperates with Java :)

○ Excellent basis for concurrent, parallel and distributed computing

○ Deliver things faster with less code

oriented and functional programming concepts

Runs on the JVM and interoperates with Java :)

Excellent basis for concurrent, parallel and distributed computing

Page 7: Are you ready for functional programming JavaScript vs. Scala · Why Scala? Why Javascript? High Order Functions Closures Currying Tail recursion Immutability Lazy evaluation & Memoization

Why Javascript?Why Javascript?Why Javascript?Why Javascript?

Two reasons according to Douglas Crockford:

○ You don’t have a choice

○ Javascript is really good

Page 8: Are you ready for functional programming JavaScript vs. Scala · Why Scala? Why Javascript? High Order Functions Closures Currying Tail recursion Immutability Lazy evaluation & Memoization

Higher Order FunctionsHigher Order FunctionsHigher Order FunctionsHigher Order Functions

○ Functions can take other functions as arguments

○○ Functions are first-order citizens

○ Can be assigned to variables, array entries and properties of other objects

○ Can be arguments/return values from other functions

○ Functions can posses properties that can be dynamically created and assigned

○ Functions can be created via literals

Higher Order FunctionsHigher Order FunctionsHigher Order FunctionsHigher Order Functions

Functions can take other functions as arguments

Can be assigned to variables, array entries and properties of other objects

Can be arguments/return values from other functions

Functions can posses properties that can be dynamically created and assigned

Page 9: Are you ready for functional programming JavaScript vs. Scala · Why Scala? Why Javascript? High Order Functions Closures Currying Tail recursion Immutability Lazy evaluation & Memoization

ClosuresClosuresClosuresClosures

○ One of the main features of each functional languages

○ The scope created when function is declared○ The scope created when function is declared

○ Allows the functions to access and manipulate external variables

Possibilities?

endless….

One of the main features of each functional languages

Allows the functions to access and manipulate external variables

Page 10: Are you ready for functional programming JavaScript vs. Scala · Why Scala? Why Javascript? High Order Functions Closures Currying Tail recursion Immutability Lazy evaluation & Memoization

CurryingCurryingCurryingCurrying

○ Produce a new function by combining a function and an argument

○ Two ways of using currying::○ Two ways of using currying::

○ By defining functions that return other functions.

○ By defining a function with multiple parameter lists.

○ Practical usage:

○ var arguments

○ Adapter pattern

Produce a new function by combining a function and an argument

By defining functions that return other functions.

By defining a function with multiple parameter lists.

Page 11: Are you ready for functional programming JavaScript vs. Scala · Why Scala? Why Javascript? High Order Functions Closures Currying Tail recursion Immutability Lazy evaluation & Memoization

Tail recursionTail recursionTail recursionTail recursion

Feature that each functional language must have!Feature that each functional language must have!

The bad news for JS: stack overflow

But in reality, stack overflow in JS = you are doing it wrong

Feature that each functional language must have!Feature that each functional language must have!

But in reality, stack overflow in JS = you are doing it wrong

Page 12: Are you ready for functional programming JavaScript vs. Scala · Why Scala? Why Javascript? High Order Functions Closures Currying Tail recursion Immutability Lazy evaluation & Memoization

ImmutabilityImmutabilityImmutabilityImmutability

Immutable object = unchangeable object

Main benefits:Main benefits:

○ ConcurrencyConcurrencyConcurrencyConcurrency

The Free Lunch Is Over - A Fundamental Turn Toward Concurrency in Software.

○ Debugging

○ Unit testing

Avoiding mutable state means programming functionally

A Fundamental Turn Toward Concurrency in Software.

Avoiding mutable state means programming functionally

Page 13: Are you ready for functional programming JavaScript vs. Scala · Why Scala? Why Javascript? High Order Functions Closures Currying Tail recursion Immutability Lazy evaluation & Memoization

Lazy evaluation & MemoizationLazy evaluation & MemoizationLazy evaluation & MemoizationLazy evaluation & Memoization

Do things as late as possible and never do them tw

really needed. TIme is money!really needed. TIme is money!

Memoization - a way of optimizing code so that it will return cached results for the same inputs.

Lazy evaluation & MemoizationLazy evaluation & MemoizationLazy evaluation & MemoizationLazy evaluation & Memoization

twice. Delay execution as much as possible until it is

a way of optimizing code so that it will return cached results for the same inputs.

Page 14: Are you ready for functional programming JavaScript vs. Scala · Why Scala? Why Javascript? High Order Functions Closures Currying Tail recursion Immutability Lazy evaluation & Memoization

Benefits & ConclusionBenefits & ConclusionBenefits & ConclusionBenefits & Conclusion

○ No side effects when programming

○ Reusability○ Reusability

○ Faster execution

○ No banana problem

○ Scala will have a great future

○ Javascript is already the most popular languageJavascript is already the most popular language

Page 15: Are you ready for functional programming JavaScript vs. Scala · Why Scala? Why Javascript? High Order Functions Closures Currying Tail recursion Immutability Lazy evaluation & Memoization

Questions?Questions?Questions?Questions?

Page 16: Are you ready for functional programming JavaScript vs. Scala · Why Scala? Why Javascript? High Order Functions Closures Currying Tail recursion Immutability Lazy evaluation & Memoization

Thanks for your attentionThanks for your attentionThanks for your attentionThanks for your attention

Elizabeta Ilievska

Senior Developer @ Endava

skype: en_eilievska

linkedin: elizabeta-ilievska-8b57002b

Thanks for your attentionThanks for your attentionThanks for your attentionThanks for your attention

Goran Kopevski

Senior Developer @ Endava

skype: goran_kopevski

linkedin: goran-kopevski-88444954

github: https://github.com/gkopevski