Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const bq1 = node('blockquote', 'quote');
  2. const bq2 = node('p', 'another quote');
  3. const processedHtml = map(element => {
  4.     if( is('blockquote', element)) {
  5.         return node('p', value(element));
  6.     }
  7.     return element;
  8.     }, append(append(make(), bq1), bq2));
  9.  
  10. export const map = (func, elements) => {
  11.     if (isEmpty(elements)) {
  12.         return l();
  13.     }
  14.     const newElement = func(head(elements);
  15.     return cons(newElement, map(func, tail(elements)));
  16. }
  17.  
  18. export const filter = (func, elements) => {
  19.     if (isEmpty(elements)) {
  20.         return l();
  21.     }
  22.     const current = head(elements);
  23.     const tailElements = tail(elements);
  24.     if (func(current)) {
  25.         return cons(current, filter(func, tailElements);
  26.     }
  27.     return filter(func, tailElements);
  28. };
  29. ///итеративно!
  30. export const filter = (func, elements) => {
  31.   const iter = (dom, acc) => {
  32.     if (isEmpty(dom)) {
  33.       return acc;
  34.     }
  35.     const current = head(dom);
  36.     const tailElements = tail(dom);
  37.     if (func(current)) {
  38.       return iter(tailElements, cons(current, acc));
  39.     }
  40.     return iter(tailElements, acc);
  41.   }
  42.   return reverse(iter(elements, l()));
  43. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement