티스토리 뷰

  • Return a Sorted Array without changing the Original Array
    var globalArray = [5, 6, 3, 2, 9]; function nonMutatingSort(arr) {   return [].concat(arr).sort((a,b) => a-b); } nonMutatingSort(globalArray); 
  • A side effect of the sortmethod is that it changes the order of the elements in the original array. One way to avoid this is to first concatenate an empty array to the one being sorted (remember that concatreturns a new array), then run the sortmethod.

 

  • Combine an Array into String Using the join Methodreplace를 사용하지 않고 split과 join만 사용하기+) split 메소드에 정규식 바로 쓸 수 있는 거 처음 알았다…
  • function sentensify(str) { return str.split(/\W/).join(' '); }
  • sentensify("I-like-Star-Wars"); // should return "I like Star Wars"

 

  • Use the every Method to Check that Every Element in an Array Meets a Criteria
  • var numbers = [1, 5, 8, 0, 10, 11]; numbers.every(function(currentValue) { return currentValue < 10; }); // Returns false
  • Use the some Method to Check that Any Elements in an Array Meet a Criteria
  • var numbers = [10, 50, 8, 220, 110, 11]; numbers.some(function(currentValue) { return currentValue < 10; }); // Returns true

 

  • Introduction to Currying and Partial ApplicationThis is useful in your program if you can't supply all the arguments to a function at one time. You can save each function call into a variable, which will hold the returned function reference that takes the next argument when it's available.Similarly, partial applicationcan be described as applying a few arguments to a function at a time and returning another function that is applied to more arguments.
  • //Impartial function function impartial(x, y, z) { return x + y + z; } var partialFn = impartial.bind(this, 1, 2); partialFn(10); // Returns 13
  • // Call a curried function in parts: var funcForY = curried(1); console.log(funcForY(2)); // Prints 3
  • //Un-curried function function unCurried(x, y) { return x + y; } //Curried function function curried(x) { return function(y) { return x + y; } } curried(1)(2) // Returns 3

 

Reference: freeCodeCamp Functional Programming

'공부일지(TIL) > Others' 카테고리의 다른 글

HTML5로 카드 만들기 (with Canvas API)  (0) 2019.06.11
Design Patterns Intro  (0) 2019.05.17
Node File System Module  (0) 2019.04.28
Functional Programming 2  (0) 2019.03.13
Functional Programming 1  (0) 2019.03.12
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함