FP with Javascript and others

Javascript was the Top #1 popular language 3 years ago (for rich Web Frameworks), now replaced by Python (for Big Data and DeepLearning rich libraries).

As in any pop culture (song, dance, music, fashion, drama, storybooks) which comes fast also fades fast, pop languages (IT, human) too.

Functional Programming (FP) evolves rapidly since 2017 because of the need to write “SAFE” software – say, controller software for nuclear plant, spaceship, MRT train Collision Prevention, driver-less cars, etc – which is lacking in Object-Oriented languages like Java with mutable states – hard to debug when a variable state / value is changed at execution time in unforeseen conditions due to “side effects” i.e. external changes (I/O, web links, parallel processing from concurrent threads, exception errors,… )

Functional Programming is a style, which can be adopted even with non-FP built-in languages like Javascript / Java / C# – albeit awkwardly! it is akin to learning Chinese / Japanese / Korean using English alphabet letters (aka PinYin, or Romanized), so called “隔靴搔痒” (useless attempt as scratching an itch from outside one’s boots – NOWHERE NEAR !)

The purest FP languages are Haskell & Lisp family (Clojure); the hybrid FP/OO languages are Scala, Kotlin (Google) , F# (Microsoft), Swift (Apple).

Unfortunately, as in anything purest (FP), they are “chinoiseries” (好看不好吃) – they are academically elegant but not (commercially) practical in popular platforms eg.JVM / iOS. Hence the hybrid FP/OO from the 2 major mobile phone vendors Apple and Google.

I predict the hybrids are temporary solutions when more people ditch OO (after the 20-year-old Java’s death) and converted to FP with a better and practical FP language – it could be the one written in Chinese with “AI” functionalities (as Chinese characters are ideograms with intrinsic “inferenceable” intelligent features).

1. Immutability

https://hackernoon.com/functional-programming-paradigms-in-modern-javascript-immutability-4e9751ca005c

2. Part II: Non-Pure refactored to Pure => passed the variables as function’s arguments,

=> new variables to store the changed values of the original (to be kept immutable) variables.

3.

View at Medium.com

Kotlin for Beginners

Most Kotlin tutorial beginners make the mistake of bringing in the legacy Java Object-Oriented thinking into Kotlin — blame the auto-convert feature in Android Studio !

Kotlin, likes Haskell, Scala, has the powerful FP (Functional Programming) as the other side of the dual “FP&OO” features. You can write beautifully re-usuable “bottom-up” software components in FP style, rather than in the GoF pattern adopted by O-O – more lengthy, complex, “top-down”, software components.

1. https://www.udemy.com/kotlin-for-absolute-beginners/

2. https://medium.com/jsguru/the-sensible-ravings-of-a-functional-programming-fanboy-75495985b4d5

3. Android Studio 3.0 https://www.raywenderlich.com/177535/beginning-android-development-with-kotlin-part-two-using-android-studio

4. Data class viewmodel made immutable: https://medium.com/@trionkidnapper/viewmodel-and-kotlin-data-class-7d3a3b854805

5. Explicit / Implicit Receiver : “this“, “superhttps://blog.kotlin-academy.com/programmer-dictionary-implicit-receiver-vs-explicit-receiver-da638de31f3c

6. Six Syntactic Sugars: (sealed class etc) https://medium.com/grand-parade/6-magic-sugars-that-can-make-your-kotlin-codebase-happier-part-1-ceee3c2bc9d3

7. Kotlin a la FP : “maybe” etc

https://blog.plan99.net/kotlin-fp-3bf63a17d64a

8. Kotlin : Nothing Type & Either
https://proandroiddev.com/kotlins-nothing-type-946de7d464fb

9. Complete guide to learn kotlin for android development

https://blog.mindorks.com/a-complete-guide-to-learn-kotlin-for-android-development-b1e5d23cc2d8

10. Try Exception:

https://dzone.com/articles/kotlin-try-type-for-functional-exception-handling

11. Generics: In / Out Type Variants:

https://android.jlelse.eu/in-and-out-type-variant-of-kotlin-587e4fa2944c?gi=1d7e2957c9be

Xamarine -Microsoft Cross-Platform Mobile Apps Developer Tool

Xamarine is based on MS Visual Studio to build both IOS and Android Mobile apps with the same codes in C#.

Caveat is the language is C#, slightly less verbose than Java, not functional programming like Kotlin or Scala.

Explore Xamarine / C# if you are from the old Object-Oriented school of programming.

If you are a Mac user, then sorry Xamarine/Visio Studio/.Net / C# only run on PC Windows.

https://www.androidauthority.com/xamarin-cross-platform-android-development-817516/

Gang Of Four O-O Patterns vs Functional Programming Styles

Object-Oriented (O-O) Software Design follows the Gang Of Four (GOF) best practice Design Patterns (which borrow the concept of Architecture “Template” Design Patterns).

“Objects use Patterns, Functions use Styles (aka Monads) .”

“四人帮” Gang of Four (GoF) Object-Oriented vs Functional Programming (FP):

My analogies of O-O vs FP:

1. Kungfu (or any “hard” martial arts) vs Taiji (“Soft” Chi-based “气” style as demonstrated by Alibaba.com Jack Ma in the video below) :

2. Western Alphabet-based Words (long chain of letters, long bytes) vs Chinese Ideogram-based Words (short, less bytes).
Eg. “Software” (8 bytes) = “软件” (2× double-bytes = 4 bytes)

Notes:

  1. Java & Android practice GoF patterns, making the software on one hand well designed, on the other hand over lengthy and over complicated for the sake of following the GoF patterns.
  2. The Functional Programming “FP” design which is based on Functions, rather than Objects (as in O-O), should avoid the GoF patterns but pure FP spirit – Monadic Design (Monad I/O, Monad Reader, Apply, Reduce, Map, Flatmap, etc).
  3. Kotlin is a hybrid of O-O and FP. In order to be convertible to Java codes on Android platform, GoF pattern is often used a la Java style though coded in Kotlin. (https://www.raywenderlich.com/168038/common-design-patterns-android-kotlin)
  4. This is in contrast to another hybrid OO-FP language (also a JVM language like Kotlin & Java) Scala which adopts the pure FP elegance.

Below are some common GoF O-O patterns :

1. Factory Pattern

https://dzone.com/articles/java-the-factory-pattern

2. Observer Pattern (Subscribe-Publish)

https://dzone.com/articles/the-observer-pattern-using-modern-java

3. Strategy Pattern (ConditionalInterface”)

https://dzone.com/articles/java-the-strategy-pattern

4. Singleton Pattern 单枪走天下 Object with 1 and only 1 instance <=> “Compound Object” (in Kotlin)

Ref:

1. Functional Kotlin
http://functionalkotlin.com

2. SICP: Sequence as conventional interfaces: eg. flatmap, map, reduce, fold

https://dzone.com/articles/conventional-interfaces-in-functional-programming

3. Do not fear Monads:

A Fistful of Monads

4. OO vs FP?

https://www.codenewbie.org/blogs/object-oriented-programming-vs-functional-programming

Lambda Calculus – The Math Behind Functional Programming

Functional Programming (FP) Languages : Lisp, Haskell, Scala, Kotlin, etc.

Other non-FP influenced by Lambda Calculus: Python, Ruby, Javascript, Java 8, C++

Inventor of Lambda Calculus : Alonzo Church (1903 – 1995), whose student in Princeton University (1936-1938) was Alan Turing (The Father of Artificial Intelligence).

Lambda Calculus is not : another Differential Calculus !

Note: Calculus has a meaning of manipulating symbolic expressions : either in functions (differentiation, integration) or computations.

Lambda Calculus is almost programming!

I. Syntax of Lambda Calculus: \boxed {\lambda \text { param . body }}

eg. \lambda \: x \: . \: x + 1

Notice: it has only one parameter “x”.

  1. Function definition: \lambda
  2. Identifier reference: x
  3. Function application: x + 1

II. Currying 柯里化 : (named after Haskell Curry ) for multiple parameters.

eg. \lambda \: x \: . \: (\lambda \: y \: . \: x + y)

written by “Currying” as : \boxed { \lambda \: x \: y \: . \: x + y}

Syntactic Sugar 语法糖 : a notational shorthand. Eg. “cubic”
cubic = λ x . x * x * x

III. Binding: Every parameter (aka variable) must be declared (syntactically binding).

eg. \lambda \: x \: . \: z x

here, x is bound, but z is FREE (error!)

IV. Two Execution Methods:

1. \alpha \text{ conversion }

  • rename variables to avoid conflict

2. \beta \text{ reduction} \text { - Apply a function}

  • Eager evaluation strategy : Right to Left (innermost expression first to outermost) or
  • Lazy evaluation strategy : Left to Right (outermost expression first to innermost) – don’t compute the value of an expression until you need to – (save memory space and computing time)
  • Most FP are Lazy.
  • Most Procedural (Imperative) languages (C, Fortran, Basic, …) are Eager.

V. Lambda Calculus fulfilling the 3 conditions for “Turing Complete” Computation :

  • UnboundedStorage” (not necessarily a physical device) – generate arbitrarily complicated values in a variable or many functions without bound.
  • Arithmetic – Church numerals (Peano arithmetic using functions): eg z=0, s= z+ 1 => 1 = λ s z . s z => 2 = λ s z . s ( s z ) … => 7= λ s z . s (s(s(s(s(s(s(z )))))))
  • Control Flow – TRUE = λ t f . t / FALSE = λ t f . f / BoolAnd = λ x y . x y FALSE / BoolOr = λ x y . x TRUE y / Repetition by Recursion (Y Combinator )

Conclusion: Lambda Calculus = “Computer on paper”

VI. Type – Consistent Model (notation “:“)

eg. λ x : I . x + 3 ( I is Integer Type)

=> The result (x + 3) is also Type I since by inference “+” is of Type I -> I

Reference: “Good Math” by Mark Chu-Carroll https://www.amazon.com/Good-Math-Computation-Pragmatic-Programmers/dp/1937785335/