Guest User

Untitled

a guest
Jan 16th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.41 KB | None | 0 0
  1. const values = [1, 2, 3, 'a', 'b', 'c'];
  2.  
  3. // [] -> []
  4. // [x] -> [x]
  5. // x: xs : y -> [x, y] ++ func(xs)
  6. const melder = values => {
  7. if (values.length <= 1) {
  8. return values;
  9. }
  10.  
  11. const head = values[0];
  12. const tail = values[values.length - 1];
  13. const body = values.slice(1, values.length - 1);
  14.  
  15. return [head, tail].concat(melder(body));
  16. };
  17.  
  18. const res = melder(values);
  19. console.log('res', res);
Add Comment
Please, Sign In to add comment