“Functional Programing in Data Science Projects” by Nathanael Weill https://link.medium.com/UiysKbFl16
Algebraic Data Type:
- Product Type
- Sum Type: Either
Functor (map) :
Very good presentation of Functional C++ by the guru Kevlin Henney.
Piping (Functional Composition) in Channels Asynchronously Concurrency:
Python is multi-paradigm: OO or FP.
Useful Functional Programming Techniques :
- Closure: local variables
- Resursion : stop unlimiting looping
- Lambdas : anonymous function for 1-time throw-away functions
- Nested Function: function returns a function as result.
- Decorators : wrap an existing function with additional features without modifying it.
What is an algebra? by Tikhon Jelvis https://www.quora.com/What-is-an-algebra/answer/Tikhon-Jelvis?ch=3&share=2dd8711d&srid=oZzP
“Basically, an algebra is just an algebraic structure. It’s some set A along with some number of functions closed over the set. It’s a generalization over the structures we normally study: a group is an algebra, a ring is an algebra, a lattice is an algebra… etc.
Algebras have different “signatures” which specify the functions it has. For example, a group is an algebra that has an identity element, a function of one argument and a function of two arguments.
That is, a group with a carrier set A is just a tuple:
⟨A, 0:A, −:A→A, +:A×A→A⟩
For uniformity, we can write all of these as functions in the form An→A, where n is the “arity” of a function—the number of arguments it has. The identity element is a function A0→A, which just identifies a single element from A. Thus, we can talk about the signature of an algebra as the arities of its functions.
A group would be (0, 1, 2) while a ring would be (0, 0, 1, 2, 2).
Generally, the functions of an algebra have to be associative. Sometimes, we also look at other laws—for example, we might want to study algebras with commutative operations like Abelian groups.
So the intuition for an algebra in general is that it’s any structure like a group, a ring or whatever else we like. As the name “structure” implies, these additional operations on a set expose the internal structure of its elements: a group describes symmetries, a lattice describes a partial order and so on.
The study of algebras, then, can be thought of as the study of “structured sets” in general.”
The “war” of OOP vs FP is akin to Applied Math vs Pure Math.
The formers (OOP & Applied Math) are not “rigourous” but practical, compared to the laters (FP & Pure Math) which are elegant but too abstract for popular usage.
OOP: SmallTalk and its followers – C++, C#, Objective-C, Java…
FP: LISP and its followers – Haskell, Clojure, …
The “hybrid” (OOP&FP): Scala, Kotlin (Google: Java ‘cousin’), Swift (Apple: Objective -C ‘cousin’), F# (MicroSoft)
The “cons” of OOP, which are bad for concurrency / parallel computing in multi-cored CPU:
- State changed
- Mutability of data
How to keep state (counter, threshold, etc) in Functional Programming without BAD side effect ?
Haskell & any FP compiler don’t check the Category Theory proof if your codes (eg. fmap) follow Functor’s Laws (eg. Preserve structure, identity) or Monad’s Laws !
I hate Android Architecture since it is based on OO Java, Kotlin is FP cousin of Java, hopefully it could simplify the Android Architecture by getting rid of the complex OO architecture. Here Antonio attempts to simplify it with more terse Kotlin codes, albeit still in the OO architecture spirit.
OO = Object-Oriented, eg. Smalltalk, C++, Java
FP = Functional Programming, aka “Mathematical” Programming Language eg. Lisp, Haskell, Scala (hybrid OO+FP), Kotlin (hybrid OO+FP).
Three “weaknesses” (also “strengths”) of OO :
Tail Recursion in Kotlin:
The Cost of Kotlin:
“Gang of Four” Design Pattern with Kotlin
Functional Programming (FP) Languages : Lisp, Haskell, Scala, Kotlin, etc.
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:
Notice: it has only one parameter “x”.
- Function definition:
- Identifier reference:
- Function application:
II. Currying 柯里化 : (named after Haskell Curry ) for multiple parameters.
written by “Currying” as :
Syntactic Sugar 语法糖 : a notational shorthand. Eg. “cubic”
cubic = λ x . x * x * x
III. Binding: Every parameter (aka variable) must be declared (syntactically binding).
here, x is bound, but z is FREE (error!)
IV. Two Execution Methods:
- rename variables to avoid conflict
- 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 :
- Unbounded “Storage” (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/
Since May 2017 Kotlin released by Google, 12.8% Java developers have converted to Kotlin, yet they still keep to the OO spirit of Java (for Interoperability) , not taking full advantage of FP capability of Kotlin. The OO Design Patterns of Android Java still being used instead of the FP more elegant “Monadic” Design.
1. Interview with Kotlin Designer:
2. Android Studio 3.0 Released : https://www.androidauthority.com/android-studio-3-released-810099/
2a. Android Studio v3.0 with Kotlin built-in & many improvements.
(The previous stable v2.3 needs seperate Kotlin plug-in)
2c: Gradle upgrade
3. Inner / Infix Function
4. Test (Mockito)
5. Function vs Procedure vs Method – Kotlin simplifies all 3 into 1 : Function which always returns a value or UNIT.
6. Kotlin has which Java lacks … “with“, “?”, …
7. Kotlin Operator Overloading aka “Convention”: ‘plus’ / ‘+’
8. JVM Byte Code Generation:
9. Reified Types
10. SICP: Sequence as conventional interfaces: eg. flatmap, map, reduce, fold
12. Generic : Kotlin入门(11)江湖绝技之特殊函数
13. Array <String>: Kotlin入门(4)声明与操作数组
14. ViewPager (Horizontal Swipe)
15. Kotlin 1.2 Beta & Multiplatform iOS
16. Kotlin Edu ( Android Studio 3.0)
17. Kotlin Style Guide
18. Android Layout Foundamental – ConstraintLayout
19. Android SDK
20. Javalin v1.0 – Web Framework for Java + Kotlin
22. Function literals with receiver
23. Sealed classes: (restricted types no “else”)
24. Android Intent
25. Top kotlin tutorials
26. Kotlin Contexts & Shared Preferences
27. Delegation – but not Inheritance (which takes all)
28. Functions or Properties Reference (cool *)
29. Web Framework ktor:
Monoid = an Algebraic structure (“Algebra” ) with: Associativity & Identity
Monad = Monoid + Endo-Functors
Functional Programming “Clojure“-based DeepLearning :