Lisp’style code with JS                  Lisp’                          by Aleksandr Motsjonovвторник, 5 июля 2011 г.
Lisp’style code with JS                  Lisp’                          Or «The Little JavaScripter»                      ...
Lisp’style code with JS                  Lisp’                          Or how to make JavaScript ugly ...                ...
Scheme’    Lisp’style code with JS                          Or how to make JavaScript ugly ...                            ...
Douglas Crockfordвторник, 5 июля 2011 г.
Douglas Crockfordвторник, 5 июля 2011 г.
Douglas Crockfordвторник, 5 июля 2011 г.
Douglas Crockford                          • Everytime Douglas Crockford blinks his                           eyes, anothe...
Douglas Crockford                          • Everytime Douglas Crockford blinks his                              eyes, ano...
Douglas Crockford                          • Everytime Douglas Crockford blinks his                        eyes, another I...
Douglas Crockford                          • Everytime Douglas Crockford blinks his                        eyes, another I...
Douglas Crockford                          • Everytime Douglas Crockford blinks his                          eyes, another...
Douglas Crockford                          • Everytime Douglas Crockford blinks his                          eyes, another...
Douglas Crockford                          • Everytime Douglas Crockford blinks his                          eyes, another...
вторник, 5 июля 2011 г.
вторник, 5 июля 2011 г.
« ... it did a remarkable                          thing: It could teach you                          to think recursively...
« ... it did a remarkable                          thing: It could teach you                          to think recursively...
« ... it did a remarkable                          thing: It could teach you                          to think recursively...
« ... it did a remarkable                                       thing: It could teach you                                 ...
Primitives            car, cdr, cons, null?, eq?, zero?вторник, 5 июля 2011 г.
Primitives            car, cdr, cons, isNull, eq?, zero?                            null?, isEq, isZeroвторник, 5 июля 201...
Primitives            car, cdr, cons, isNull, eq?, zero?                            null?, isEq, isZero  car = function(li...
Primitives            car, cdr, cons, isNull, eq?, zero?                            null?, isEq, isZero  car = function(li...
Cond                          (cond (question1 answer1)                                (question2 answer2)                ...
Cond                          (cond (question1 answer1)                                (question2 answer2)                ...
Cond                          (cond (question1 answer1)                                (question2 answer2)                ...
Cond                          (cond (question1 answer1)                                (question2 answer2)                ...
Cond                          (cond (question1 answer1)                                (question2 answer2)                ...
Cond                          (cond (question1 answer1)                                (question2 answer2)                ...
Cond                          (cond (question1 answer1)                                (question2 answer2)                ...
Cond                          (cond (question1 answer1)                                (question2 answer2)                ...
Cond                          (cond (question1 answer1)                                (question2 answer2)                ...
Cond                          (cond (question1 answer1)                                (question2 answer2)                ...
Cond                          (cond (question1 answer1)                                (question2 answer2)                ...
Cond                  if (question1){return answer1;}             else if (question2){return answer2;}                  .....
Cond                  if (question1){return answer1;}             else if (question2){return answer2;}                  .....
Cond                  if (question1){return answer1;}             else if (question2){return answer2;}                  .....
Condвторник, 5 июля 2011 г.
Cond                          (cond (question1 answer1)                                (question2 answer2)                ...
Cond                          (cond (question1 answer1)                           cond((question1                         ...
Cond                          (cond (question1 answer1)                           cond([question1                         ...
Cond                          (cond (question1 answer1)                           cond([question1                         ...
Cond                          (cond (question1 answer1)                           cond([question1                         ...
Cond                          (cond (question1 answer1)                           cond([question1                         ...
Cond                           (cond (question1 answer1)                            cond([question1                       ...
Cond                   function isMember(a, lat) {                      return cond(                         [isNull(lat),...
Cond                   function isMember(a, lat) {                      return cond(                         [isNull(lat),...
Cond                          function cond() {                             var args = utils.copy_arr(arguments), i = 0;  ...
Example                     function occur(a, lat){                        return cond(                           [isNull(...
Example                     function occur(a, lat){                        return cond(                           [isNull(...
Example                     function occur(a, lat){                        return cond(                           [isNull(...
Example   function insertR(n, o, lat){      return cond(         [is_null(lat), quote()],         [function(){            ...
Example   function insertR(n, o, lat){      return cond(         [is_null(lat), quote()],         [function(){            ...
Example   function insertR(n, o, lat){      return cond(         [is_null(lat), quote()],         [function(){            ...
Example   function insertR(n, o, lat){      return cond(         [is_null(lat), quote()],         [function(){            ...
What if ...                          function occur(a, lat){                             return cond(                     ...
What if ...                          function occur(a, lat){                             return cond(                     ...
What if ...                          # occur(a, lat){                          function occur(a, lat){                    ...
I am done. Thx!                          # occur(a, lat){                             cond(                               ...
Upcoming SlideShare
Loading in …5
×

Lisp'styled JavaScript

2,367 views

Published on

It's about a way of coding in JavaScript in the most Functional style without any kind of sugar.
Video is here:
http://www.devclub.eu/2011/07/04/lisp-like-javascript/ (it's in Russian)

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,367
On SlideShare
0
From Embeds
0
Number of Embeds
701
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lisp'styled JavaScript

  1. 1. Lisp’style code with JS Lisp’ by Aleksandr Motsjonovвторник, 5 июля 2011 г.
  2. 2. Lisp’style code with JS Lisp’ Or «The Little JavaScripter» by Aleksandr Motsjonovвторник, 5 июля 2011 г.
  3. 3. Lisp’style code with JS Lisp’ Or how to make JavaScript ugly ... by Aleksandr Motsjonovвторник, 5 июля 2011 г.
  4. 4. Scheme’ Lisp’style code with JS Or how to make JavaScript ugly ... by Aleksandr Motsjonovвторник, 5 июля 2011 г.
  5. 5. Douglas Crockfordвторник, 5 июля 2011 г.
  6. 6. Douglas Crockfordвторник, 5 июля 2011 г.
  7. 7. Douglas Crockfordвторник, 5 июля 2011 г.
  8. 8. Douglas Crockford • Everytime Douglas Crockford blinks his eyes, another IE6 instance is killed.вторник, 5 июля 2011 г.
  9. 9. Douglas Crockford • Everytime Douglas Crockford blinks his eyes, another IE6 instance is killed. • Every time you declare a variable in the global scope, Douglas Crockford kills a kitten.вторник, 5 июля 2011 г.
  10. 10. Douglas Crockford • Everytime Douglas Crockford blinks his eyes, another IE6 instance is killed. • Every time you declare a variable in the global scope, Douglas Crockford kills a kitten. • There is only one necessary global, and that is Douglas Crockfordвторник, 5 июля 2011 г.
  11. 11. Douglas Crockford • Everytime Douglas Crockford blinks his eyes, another IE6 instance is killed. • Every time you declare a variable in the global scope, Douglas Crockford kills a kitten. • There is only one necessary global, and that is Douglas Crockford • JohnResig == awesome, but DouglasCrockford === awesomeвторник, 5 июля 2011 г.
  12. 12. Douglas Crockford • Everytime Douglas Crockford blinks his eyes, another IE6 instance is killed. • Every time you declare a variable in the global scope, Douglas Crockford kills a kitten. • There is only one necessary global, and that is Douglas Crockford • JohnResig == awesome, but DouglasCrockford === awesome • “I’m your father, John”вторник, 5 июля 2011 г.
  13. 13. Douglas Crockford • Everytime Douglas Crockford blinks his eyes, another IE6 instance is killed. • Every time you declare a variable in the global scope, Douglas Crockford kills a kitten. • There is only one necessary global, and that is Douglas Crockford • JohnResig == awesome, but DouglasCrockford === awesome • “I’m your father, John” • In truth there exists no anonymous functions in JavaScript because Douglas Crockford loves and names all of his children.вторник, 5 июля 2011 г.
  14. 14. Douglas Crockford • Everytime Douglas Crockford blinks his eyes, another IE6 instance is killed. • Every time you declare a variable in the global scope, Douglas Crockford kills a JavaScript Guru! kitten. • There is only one necessary global, and that is Douglas Crockford • JohnResig == awesome, but DouglasCrockford === awesome • “I’m your father, John” • In truth there exists no anonymous functions in JavaScript because Douglas Crockford loves and names all of his children.вторник, 5 июля 2011 г.
  15. 15. вторник, 5 июля 2011 г.
  16. 16. вторник, 5 июля 2011 г.
  17. 17. « ... it did a remarkable thing: It could teach you to think recursively »вторник, 5 июля 2011 г.
  18. 18. « ... it did a remarkable thing: It could teach you to think recursively » « ... all of the functions in The Little Schemer can be written in JavaScript. »вторник, 5 июля 2011 г.
  19. 19. « ... it did a remarkable thing: It could teach you to think recursively » « ... all of the functions in The Little Schemer can be written in JavaScript. » «So get a copy of The Little Schemer and start recursing ... »вторник, 5 июля 2011 г.
  20. 20. « ... it did a remarkable thing: It could teach you to think recursively » « ... all of the functions in The Little Schemer And so I didn...b e w r i t t e n i n ca JavaScript. » «So get a copy of The Little Schemer and start recursing ... »вторник, 5 июля 2011 г.
  21. 21. Primitives car, cdr, cons, null?, eq?, zero?вторник, 5 июля 2011 г.
  22. 22. Primitives car, cdr, cons, isNull, eq?, zero? null?, isEq, isZeroвторник, 5 июля 2011 г.
  23. 23. Primitives car, cdr, cons, isNull, eq?, zero? null?, isEq, isZero car = function(list) { isZero = function(number){ return list[0]; return number === 0; }; }; cdr = function(list) { isNull = function(list) { return list.slice(1); return list.length === 0; }; }; cons = function(atom, list) { isEq = function(o1, o2) { var tmp = utils.copy_arr(list); return o1 === o2; tmp.splice(0, 0, atom); }; return tmp; };вторник, 5 июля 2011 г.
  24. 24. Primitives car, cdr, cons, isNull, eq?, zero? null?, isEq, isZero car = function(list) { isZero = function(number){ return list[0]; return number === 0; }; }; cdr = function(list) { return list.slice(1); cond ? isNull = function(list) { return list.length === 0; }; }; cons = function(atom, list) { isEq = function(o1, o2) { var tmp = utils.copy_arr(list); return o1 === o2; tmp.splice(0, 0, atom); }; return tmp; };вторник, 5 июля 2011 г.
  25. 25. Cond (cond (question1 answer1) (question2 answer2) ... (else else_answer))вторник, 5 июля 2011 г.
  26. 26. Cond (cond (question1 answer1) (question2 answer2) ... (else else_answer)) (define member? (lambda (a lat) (cond ((null? lat) #f) (else (or (eq? (car lat) a) (member? a cdr(lat)))))))вторник, 5 июля 2011 г.
  27. 27. Cond (cond (question1 answer1) (question2 answer2) ... (else else_answer)) (define member? (lambda (a lat) (cond ((null? lat) #f) (else (or (eq? (car lat) a) (member? a cdr(lat)))))))вторник, 5 июля 2011 г.
  28. 28. Cond (cond (question1 answer1) (question2 answer2) ... (else else_answer)) (define member? (lambda (a lat) (cond ((null? lat) #f) (else (or (eq? (car lat) a) (member? a cdr(lat)))))))вторник, 5 июля 2011 г.
  29. 29. Cond (cond (question1 answer1) (question2 answer2) ... (else else_answer)) (define member? (lambda (a lat) (cond ((null? lat) #f) (else (or (eq? (car lat) a) (member? a cdr(lat)))))))вторник, 5 июля 2011 г.
  30. 30. Cond (cond (question1 answer1) (question2 answer2) ... (else else_answer)) (define member? (lambda (a lat) (cond ((null? lat) #f) (else (or (eq? (car lat) a) (member? a cdr(lat)))))))вторник, 5 июля 2011 г.
  31. 31. Cond (cond (question1 answer1) (question2 answer2) ... (else else_answer)) (define member? (lambda (a lat) (cond ((null? lat) #f) (else (or (eq? (car lat) a) (member? a cdr(lat)))))))вторник, 5 июля 2011 г.
  32. 32. Cond (cond (question1 answer1) (question2 answer2) ... (else else_answer)) (define member? (lambda (a lat) (cond ((null? lat) #f) (else (or (eq? (car lat) a) (member? a cdr(lat)))))))вторник, 5 июля 2011 г.
  33. 33. Cond (cond (question1 answer1) (question2 answer2) ... (else else_answer)) (define member? (lambda (a lat) (cond ((null? lat) #f) (else (or (eq? (car lat) a) (member? a cdr(lat)))))))вторник, 5 июля 2011 г.
  34. 34. Cond (cond (question1 answer1) (question2 answer2) ... (else else_answer)) (define member? (lambda (a lat) (cond ((null? lat) #f) (else (or (eq? (car lat) a) (member? a cdr(lat)))))))вторник, 5 июля 2011 г.
  35. 35. Cond (cond (question1 answer1) (question2 answer2) ... (else else_answer)) (define member? (lambda (a lat) (cond ((null? lat) #f) (else (or (eq? (car lat) a) (member? a cdr(lat)))))))вторник, 5 июля 2011 г.
  36. 36. Cond if (question1){return answer1;} else if (question2){return answer2;} ... else {return else_answer;}вторник, 5 июля 2011 г.
  37. 37. Cond if (question1){return answer1;} else if (question2){return answer2;} ... else {return else_answer;} function isMember(a, lat){ if (isNull(lat)){ return false; }else{ return isEq(car(lat), a) || isMember(a, cdr(lat)); } }вторник, 5 июля 2011 г.
  38. 38. Cond if (question1){return answer1;} else if (question2){return answer2;} ... else {return else_answer;} function isMember(a, lat){ if (isNull(lat)){ return false; }else{ return isEq(car(lat), a) || isMember(a, cdr(lat)); } }вторник, 5 июля 2011 г.
  39. 39. Condвторник, 5 июля 2011 г.
  40. 40. Cond (cond (question1 answer1) (question2 answer2) ... (else else_answer))вторник, 5 июля 2011 г.
  41. 41. Cond (cond (question1 answer1) cond((question1 (question2 answer2) ... (else else_answer))вторник, 5 июля 2011 г.
  42. 42. Cond (cond (question1 answer1) cond([question1 cond((question1 answer1] [question2 answer2] (question2 answer2) ... [else else_answer]) (else else_answer))вторник, 5 июля 2011 г.
  43. 43. Cond (cond (question1 answer1) cond([question1 cond((question1 answer1] cond([question1,answer1], [question2 answer2] (question2 answer2) [question2,answer2], ... [else else_answer]) (else else_answer)) [else,вторник, 5 июля 2011 г.
  44. 44. Cond (cond (question1 answer1) cond([question1 cond((question1 answer1] cond([question1,answer1], [question2 answer2] (question2 answer2) [question2,answer2], ... [else else_answer]) (else else_answer)) [else, [else_answer])вторник, 5 июля 2011 г.
  45. 45. Cond (cond (question1 answer1) cond([question1 cond((question1 answer1] cond([question1,answer1], [question2 answer2] (question2 answer2) [question2,answer2], ... [else else_answer]) (else else_answer)) [else, [else_answer]) else_answer)вторник, 5 июля 2011 г.
  46. 46. Cond (cond (question1 answer1) cond([question1 cond((question1 answer1] cond([question1,answer1], [question2 answer2] (question2 answer2) [question2,answer2], ... [else else_answer]) (else else_answer)) [else, [else_answer]) else_answer) function isMember(a, lat) { return cond( [isNull(lat), false], or( isEq(a, car(lat)), isMember(a, cdr(lat)) ) );вторник, 5 июля 2011 г.
  47. 47. Cond function isMember(a, lat) { return cond( [isNull(lat), false], function() { return or(isEq(a, car(lat)), isMember(a, cdr(lat)) ); } ); };вторник, 5 июля 2011 г.
  48. 48. Cond function isMember(a, lat) { return cond( [isNull(lat), false], function() { return or(isEq(a, car(lat)), isMember(a, cdr(lat)) ); } ); };вторник, 5 июля 2011 г.
  49. 49. Cond function cond() { var args = utils.copy_arr(arguments), i = 0; for (; i < args.length; i++) { if(utils.is_array(args[i])){ if (args[i].length == 1) { return de(args[i][0]); } else if (args[i].length == 2) { if (de(args[i][0])) { return de(args[i][1]); } } else { //throw exception; } }else{ return de(args[i]); } } }; function de(obj) { return utils.is_function(obj) ? obj() : obj; }вторник, 5 июля 2011 г.
  50. 50. Example function occur(a, lat){ return cond( [isNull(lat), 0], [isEqan(a, car(lat)), function(){ return add1(occur(a, cdr(lat))); }], function(){ return occur(a, cdr(lat)); } ); };вторник, 5 июля 2011 г.
  51. 51. Example function occur(a, lat){ return cond( [isNull(lat), 0], [isEqan(a, car(lat)), function(){ return add1(occur(a, cdr(lat))); }], function(){ return occur(a, cdr(lat)); } ); };вторник, 5 июля 2011 г.
  52. 52. Example function occur(a, lat){ return cond( [isNull(lat), 0], [isEqan(a, car(lat)), function(){ return add1(occur(a, cdr(lat))); }], function(){ return occur(a, cdr(lat)); } ); };вторник, 5 июля 2011 г.
  53. 53. Example function insertR(n, o, lat){ return cond( [is_null(lat), quote()], [function(){ return cond ( [or( is_number(car(lat)), is_atom(car(lat)) ), cond( [is_eqan(car(lat), o), function(){ return cons(insertR(n, o, cdr(lat)), n); }], function(){ return cons(car(lat), insertR(n, o, cdr(lat))); } ) ], function(){ return cons(insertR(n, o, car(lat)), insertR(n, o, cdr(lat))); } ); }] ); };вторник, 5 июля 2011 г.
  54. 54. Example function insertR(n, o, lat){ return cond( [is_null(lat), quote()], [function(){ return cond(( cond [or( is_number(car(lat)), is_atom(car(lat)) ), cond( [is_eqan(car(lat), o), function(){ return cons(insertR(n, o, cdr(lat)), n); }], function(){ return cons(car(lat), insertR(n, o, cdr(lat))); } ) ], function(){ return cons(insertR(n, o, car(lat)), insertR(n, o, cdr(lat))); } ); }] ); };вторник, 5 июля 2011 г.
  55. 55. Example function insertR(n, o, lat){ return cond( [is_null(lat), quote()], [function(){ return cond(( cond [or( is_number(car(lat)), is_atom(car(lat)) ), cond( [is_eqan(car(lat), o), function(){ return cons(insertR(n, o, cdr(lat)), n); }], function(){ return cons(car(lat), insertR(n, o, cdr(lat))); } ) ], function(){ return cons(insertR(n, o, car(lat)), insertR(n, o, cdr(lat))); } ); }] ); };вторник, 5 июля 2011 г.
  56. 56. Example function insertR(n, o, lat){ return cond( [is_null(lat), quote()], [function(){ return cond(( cond [or( is_number(car(lat)), is_atom(car(lat)) ), cond( [is_eqan(car(lat), o), function(){ return cons(insertR(n, o, cdr(lat)), n); }], function(){ return cons(car(lat), insertR(n, o, cdr(lat))); } ) ], function(){ return cons(insertR(n, o, car(lat)), insertR(n, o, cdr(lat))); } ); }] ); };вторник, 5 июля 2011 г.
  57. 57. What if ... function occur(a, lat){ return cond( [isNull(lat), 0], [isEqan(a, car(lat)), function(){ return add1(occur(a, cdr(lat))); }], function(){ return occur(a, cdr(lat)); } ); };вторник, 5 июля 2011 г.
  58. 58. What if ... function occur(a, lat){ return cond( [isNull(lat), 0], [isEqan(a, car(lat)), function(){ return add1(occur(a, cdr(lat))); }], function(){ return occur(a, cdr(lat)); } ); };вторник, 5 июля 2011 г.
  59. 59. What if ... # occur(a, lat){ function occur(a, lat){ return cond( cond( [isNull(lat), 0], [isEqan(a, car(lat)), function(){ #{ return add1(occur(a, cdr(lat))); add1(occur(a, cdr(lat))); }], #{ function(){ occur(a, cdr(lat)); return occur(a, cdr(lat)); } ); };вторник, 5 июля 2011 г.
  60. 60. I am done. Thx! # occur(a, lat){ cond( [isNull(lat), 0], [isEqan(a, car(lat)), #{ add1(occur(a, cdr(lat))); }], #{ occur(a, cdr(lat)); } ); };вторник, 5 июля 2011 г.

×