This is the slide for what I shared in JS Group meetup, 2014, Taiwan. It covers what JavaScript could do for making the program more "functional", the benefits, price and the limitation.
3. snowmantw
: Vimer
/ Linux User
λ Haskell learner
G self-studier
JS developer
MS student -> Dept. of Computer Science, NCCU
C C++ PHP JS Haskell Java
6. JavaScript's C-like syntax, including curly braces
and the clunky for statement, makes it appear
to be an ordinary procedural language. This is
misleading because JavaScript has more in
common with functional languages like Lisp or
Scheme than with C or Java. It has arrays
instead of lists and objects instead of property
lists. Functions are first class. It has closures.
You get lambdas without having to balance all
those parens.
“JavaScript: The World's Most Misunderstood Programming Language”
by Douglas Crockford
7. JavaScript's C-like syntax, including curly braces
and the clunky for statement, makes it appear
to be an ordinary procedural language. This is
misleading because JavaScript has more in
common with functional languages like Lisp or
Scheme than with C or Java. It has arrays
instead of lists and objects instead of property
lists. Functions are first class. It has closures.
You get lambdas without having to balance all
those parens.
“JavaScript: The World's Most Misunderstood Programming Language”
by Douglas Crockford
10. function id( a )
{
return a;
}
function curried( a )
{
return function ( b ) {
return a + b;
}
}
First-Class Function
Curry
11. function compose( gn )
{
return function ( fn ) {
return function( a ) {
return gn( fn ( a ) ) ;
}
}
} High-Order Function
12. curried( a )
curried( a ) ( b )
curried( a ) ( c )
curried( b ) ( c )
curried( a ) :: a function
curried( b ) :: another fn
13. Curry can make a
new function
without definition
curried( a )
curried( a ) ( b )
curried( a ) ( c )
curried( b ) ( c )
14. curried( a )
curried( a ) ( b )
curried( a ) ( c )
curried( b ) ( c )
new_fn1 = curried( 1 )
new_fn2 = curried( 2 )
new_fn1 ( 1 ) == 1 + 1
new_fn2 ( 1 ) == 2 + 1
We “defined” two functions
without definition !
15. curried( a )
curried( a ) ( b )
High-Order Function
take one or more fn as an input
OR output a function ( like Curry )
16. curried( a )
curried( a ) ( b )
compose( curried( a ) )
compose( curried( a ) ) ( curried( b ) )
compose( curried( a ) ) ( curried( b ) ) ( a )
Yet another high-order function
A normal function
A value
17. curried( a )
curried( a ) ( b )
compose( curried( 3 ) ) ( curried( 4 ) )
compose( curried( 3 ) ) ( curried( 4 ) ) ( 5 )
Generate value when all conditions are satisfied
21. - Pure Function
- Immutable Variable
- Encapsulated Impure Features
- ( More ) High Order Function
- Lazy Evaluation
- Functional Data Structure
- Type System
Javascript lacks
31. We ( the laugnage ! )
shouldn’t allow pure
functions to mix with
impure functions.
32. A Real World Case ( 2 x 4 hr )
render : function(nst) {
var t = this, s = t.settings, id = t.id, sl = tinymce.ScriptLoader;
// Page is not loaded yet, wait for it
// DEBUG : No, we don't need 'tinymce style' check here......
// Because we use bigpipe tech, this condition statement
// will always keep failed.
//
// snowmantw @ 2012-04-25 18:00:41+08:00
//
// if (!Event.domLoaded) {
// Event.add(window, 'ready', function() {
// t.render();
// });
// return;
// }
41. No pure function can
get and use values
from Monad,
unless we wrap it and
make it impure, too.
42. Monad also make “Statements”
in functional way
main =
printStrLn “This is a interactive program !” >>
printStrLn “----” >>
printStrLn “Input Something: ” >>
readline >>= maybe_str -> case maybe_str of
Nothing -> return ()
Just line -> printStrLn “Input from user: ” >>
printStrLn line
43. Monad also make “Statements”
in functional way
function main( ) {
console.log( “ This is a interactive program ! ” ) ;
console.log( “ ---- “ ) ;
var input = readline( ) ;
if ( “” == input ) { return ; }
console.log( “Input from user: ” ) ;
console.log( input ) ;
}
49. Welcome to The World of Composition
Function composition ( pure ) ‘。’
: make larger and pure function
Monad bind ( impure ) ‘ >>=’
: make useful and larger COMPUTATION
Arrow compose ( purer than Monad ) ‘>>>’
: make useful and larger COMPUTATION
You can build your whole program with composition
50. BTW: Composite "Pattern" is Nothing
fa a = b ; fb b = c ; fc = fb . fa
class functorA {
<Tb> public Tb exec( Ta a ) {
return Tb b;
} }
class functorB{
<Tc> public Tc exec( Tb b ) {
return Tc c;
} }
51. BTW: Composite "Pattern" is Nothing
fa a = b ; fb b = c ; fc = fb . fa
class FunctorCompose
{ public Functor compose( fa, fb )
{
//.......
}
}
// Use in some method
(new FunctorCompose()).compose(fa ,fb)
58. Keep your functions pure ( Dot )
function fn( x ){
return x;
}
$IO.impure = function( x ){
return x + read_from_server ( );
}
All we can use is the '$' and '_'
Impure functions should be restricted in some contexts
59. Underscore.js ( Line )
_.reduce ( [ 1,2,3 ], function(m,n){ return m+n; },0)
`map` and other high order functions
_.map ( [ 1,2,3 ], function(n){ return n+1; } )
http://underscorejs.org/
60. Functional Javascript ( Line )
' x -> y -> x+y '.lambda()(2);
More functional features than Underscore.js
' y x -> x+2*y '.lambda()(2, 3);
http://osteele.com/sources/javascript/functional/
61. jQuery ( Plane )
var pure_val =
$( DOM ) . manipulateDOM ( )
. pureCB ( function ( ) { ... } )
. it_has_unwrapper_fns () ;
"jQuery is Monad“: http://goo.gl/cFErM
http://importantshock.wordpress.com/2009/01/18/jquery-is-a-monad/
http://jquery.com//
It's ( almost ) a " DOM Monad "
62. Javascript Arrowlet ( Plane )
http://www.cs.umd.edu/projects/PL/arrowlets/api-arrowlets.xhtml
Redefine the "Process"
70. But CoffeeScript is just a little language…
- Pure Function
- Immutable Variable
- Encapsulated Impure Features
- ( More ) High Order Function
- Lazy Evaluation
- Functional Data Structure
- Type System
73. Personal Project: FunTang language
Will focus on
- Compile into CoffeeScript
- Immutable Variable
- Pure Function
- Integrated Arrow
- Fully Event-Driven Process
In early stage