Advertisement
staafl

underscore hw

Aug 25th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. $$ = λ = function(body) {
  2. return function() {
  3. var a = arguments;
  4. var x = arguments[0];
  5. var y = arguments[1];
  6. var z = arguments[2];
  7. return eval("("+body+")");
  8. };
  9. };
  10.  
  11. // 1 Write a method that from a given array of students finds all students whose first name is before its last name alphabetically. Print the students in descending order by full name. Use Underscore.js
  12.  
  13. var protoStudent = {
  14. fullName: function() { return this.firstName + " " + this.lastName; }
  15. };
  16.  
  17. var makeStudent = function(fname, lname, age, marks) {
  18. return _.extend(Object.create(protoStudent),
  19. { firstName: fname, lastName: lname, age: age, marks: marks });
  20. };
  21.  
  22. var makeStudents = function() {
  23. return _.map(arguments, λ('makeStudent.apply(null,x)'));
  24. };
  25.  
  26. var students = makeStudents(
  27. ['Joro','Peshev',25,[4,4.5,6,2]],
  28. ['Aaaaa', 'Zzzzz', 100,[]],
  29. ['Minka', 'Tosheva', 22,[2.5,6.0,4.0]],
  30. ['Pesho','Jorev', 30,[6.0,5.5]]
  31. );
  32.  
  33. var task1 = function() {
  34. var ourStudents = _.chain(students)
  35. .filter(λ('x.firstName < x.lastName'))
  36. .sortBy(λ('x.fullName()'))
  37. .values()
  38. .reverse();
  39.  
  40. _(ourStudents).each(λ('console.log(x.fullName())'));
  41. };
  42.  
  43. // 2 Write function that finds the first name and last name of all students with age between 18 and 24. Use Underscore.js
  44.  
  45. var task2 = function() {
  46. return _.chain(students)
  47. .filter(λ('18 <= x.age && x.age <= 24'))
  48. .map(λ('x.fullName()'))
  49. .value();
  50. };
  51.  
  52. // 3 Write a function that by a given array of students finds the student with highest marks
  53.  
  54. var average = function(arr) {
  55. return _.reduce(arr, λ('x+y'), 0) / arr.length;
  56. };
  57.  
  58. var task3 = function() {
  59. return _(students)
  60. .max(λ('average(x.marks)'));
  61. };
  62.  
  63. // 4 Write a function that by a given array of animals, groups them by species and sorts them by number of legs
  64.  
  65. var makeAnimals = function() {
  66. return _(arguments)
  67. .map(λ('{ name: x[0], species: x[1], legs: x[2] }'));
  68. };
  69.  
  70. var animals = makeAnimals(
  71. ["ginger", "rabbit", 4],
  72. ["winner", "dog", 4],
  73. ["chuck", "chicken", 2],
  74. ["chernobyl", "chicken", 6],
  75. ["harold", "spider", 8],
  76. ["smith", "pig", 4],
  77. ["samuel", "octopus", 8],
  78. ["horace the survivor", "pig", 3]
  79. );
  80.  
  81. var task4 = function() {
  82. var groups = _(animals).groupBy(λ('x.species'));
  83.  
  84. // sort animals in each group?
  85. // var sortedGroups = _(groups).map(λ('_(x).sortBy(λ("x.legs"))'));
  86.  
  87. // sort all groups?
  88. var sortedGroups = _(groups).sortBy(λ('x[0].legs'));
  89.  
  90. return sortedGroups;
  91. };
  92.  
  93. // 5 By a given array of animals, find the total number of legs
  94. // Each animal can have 2, 4, 6, 8 or 100 legs
  95.  
  96. var task5 = function() {
  97. return _.chain(animals)
  98. .pluck('legs')
  99. .reduce(λ('x+y'), 0)
  100. .value();
  101. };
  102.  
  103. // 6 By a given collection of books, find the most popular author (the author with the biggest number of books)
  104.  
  105. var books = _.map(
  106. [["Stephen King", "The Dark Tower"],
  107. ["George Orwell", "Animal Farm"],
  108. ["William Shakespeare", "Much Ado About Nothing"],
  109. ["George Orwell", "Homage to Catalonia"],
  110. ["Robert Heinlein", "Starship Troopers"]],
  111. λ('{author: x[0], title: x[1]}'));
  112.  
  113. var task6 = function() {
  114. return _.chain(books)
  115. .groupBy(λ('x.author'))
  116. .max(λ('x.length'))
  117. .value()[0].author;
  118. };
  119.  
  120. // 7 By an array of people find the most common first and last name. Use underscore.
  121.  
  122. var people = makeStudents(
  123. ['Joro','Peshev'],
  124. ['Minka', 'Tosheva'],
  125. ['Pesho','Jorev'],
  126. ['Joro','Goshev'],
  127. ['Joro', 'Mishev'],
  128. ['Pesho','Goshev'],
  129. ['Minkia','Gosheva'],
  130. ['Pesho','Peshev'],
  131. ['Gosho','Mishev'],
  132. ['Joro','Mishev']
  133. );
  134.  
  135. var task7 = function() {
  136. var firstName =
  137. _.chain(people)
  138. .groupBy(λ('x.firstName'))
  139. .max(λ('x.length'))
  140. .value()[0].firstName;
  141.  
  142. var lastName =
  143. _.chain(people)
  144. .groupBy(λ('x.lastName'))
  145. .max(λ('x.length'))
  146. .value()[0].lastName;
  147.  
  148. return [firstName, lastName];
  149. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement