First-class citizen(์ผ๊ธ‰ ๊ฐ์ฒด)

  • Function (ํ•จ์ˆ˜)
    1. can assign into variable
    2. can refer to argument(์ธ์ž) of other function
    3. can return as the result of other function
  • function expression(ํ•จ์ˆ˜ ํ‘œํ˜„์‹)
    • can not use before assignment(ํ• ๋‹น)
    • hoisting is not happening unlike 'function declaration'(ํ•จ์ˆ˜ ์„ ์–ธ์‹)

 

Higher order function(๊ณ ์ฐจ ํ•จ์ˆ˜)

ํ•จ์ˆ˜ = ๊ฐ’์„ ์ „๋‹ฌ ๋ฐ›์•„ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค = ๊ฐ’์— ๋Œ€ํ•œ ๋ณต์žกํ•œ ๋กœ์ง์€ ๊ฐ์ถ”์–ด์ ธ ์žˆ๋‹ค = ๊ฐ’ ์ˆ˜์ค€์—์„œ์˜ ์ถ”์ƒํ™”

 

๊ฐ’ ์ˆ˜์ค€์˜ ์ถ”์ƒํ™”: ๋‹จ์ˆœํžˆ ๊ฐ’(value)์„ ์ „๋‹ฌ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ˆ˜์ค€

์‚ฌ๊ณ ์˜ ์ถ”์ƒํ™”: ํ•จ์ˆ˜(์‚ฌ๊ณ ์˜ ๋ฌถ์Œ)๋ฅผ ์ „๋‹ฌ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ˆ˜์ค€

 

๊ณ ์ฐจํ•จ์ˆ˜ = ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ ๋ฐ›๊ฑฐ๋‚˜ ํ•จ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค = ์‚ฌ๊ณ (ํ•จ์ˆ˜)์— ๋Œ€ํ•œ ๋ณต์žกํ•œ ๋กœ์ง์€ ๊ฐ์ถ”์–ด์ ธ ์žˆ๋‹ค = ์‚ฌ๊ณ  ์ˆ˜์ค€์—์„œ์˜ ์ถ”์ƒํ™”

  • a function that takes as a argument or returns a function
  • Callback function
    • function passed into caller(another function) as an argument, which is then invoked(ํ˜ธ์ถœ)
    • depends on caller's condition, can decide callback function to be executed or executed even several times
    • mostly invoked when certain work is done so called as callback
  • Curry function
    • specifically for a function that returns function
    • part of higher order function
  1. When it takes caller(other function) as an argument
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    function triple(num) {
      return num * 3;
    }
     
    function doubleNum(func, num) {
      return func(num);
    }
     
    // ํ•จ์ˆ˜ doubleNum์€ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” ๊ณ ์ฐจ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
    // ํ•จ์ˆ˜ doubleNum์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ž func์— ํ•จ์ˆ˜๊ฐ€ ๋“ค์–ด์˜ฌ ๊ฒฝ์šฐ
    // ํ•จ์ˆ˜ func๋Š” ํ•จ์ˆ˜ doubleNum์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
    // ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ, ํ•จ์ˆ˜ double์€ ํ•จ์ˆ˜ doubleNum์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
    let output = doubleNum(triple4);
    console.log(output); // -> 12
    cs
  2. When it returns function
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    function adder(added) {
      return function (num) {
        return num + added;
      };
    }
     
    // ํ•จ์ˆ˜ adder๋Š” ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๊ณ ์ฐจ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
    // adder๋Š” ์ธ์ž ํ•œ ๊ฐœ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ํ•จ์ˆ˜(์ต๋ช… ํ•จ์ˆ˜)๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
    // ๋ฆฌํ„ด๋˜๋Š” ์ต๋ช… ํ•จ์ˆ˜๋Š” ์ธ์ž ํ•œ ๊ฐœ๋ฅผ ๋ฐ›์•„์„œ added์™€ ๋”ํ•œ ๊ฐ’์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
     
    // adder(5)๋Š” ํ•จ์ˆ˜์ด๋ฏ€๋กœ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์—ฐ์‚ฐ์ž '()'๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    let output = adder(5)(3); // -> 8
    console.log(output); // -> 8
     
    // adder๊ฐ€ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ณ€์ˆ˜์— ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    // javascript์—์„œ ํ•จ์ˆ˜๋Š” ์ผ๊ธ‰ ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
    const add3 = adder(3);
    output = add3(2);
    console.log(output); // -> 5
    cs
  3. When it takes function as argument and returns function
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    function double(num) {
      return num * 2;
    }
     
    function doubleAdder(added, func) {
      const doubled = func(added);
      return function (num) {
        return num + doubled;
      };
    }
     
    // ํ•จ์ˆ˜ doubleAdder๋Š” ๊ณ ์ฐจ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
    // ํ•จ์ˆ˜ doubleAdder์˜ ์ธ์ž func๋Š” ํ•จ์ˆ˜ doubleAdder์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์ž…๋‹ˆ๋‹ค.
    // ํ•จ์ˆ˜ double์€ ํ•จ์ˆ˜ doubleAdder์˜ ์ฝœ๋ฐฑ์œผ๋กœ ์ „๋‹ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
     
    // doubleAdder(5, double)๋Š” ํ•จ์ˆ˜์ด๋ฏ€๋กœ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๊ธฐํ˜ธ '()'๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    doubleAdder(5double)(3); // -> 13
     
    // doubleAdder๊ฐ€ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ณ€์ˆ˜์— ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์ผ๊ธ‰ ๊ฐ์ฒด)
    const addTwice3 = doubleAdder(3double);
    addTwice3(2); // --> 8
    cs

 

Built-in higher order functions(๋‚ด์žฅ ๊ณ ์ฐจ ํ•จ์ˆ˜)

    • Array.prototype.filter() ๋ถ„๋ฅ˜ํ•ด์„œ ๊ฑฐ๋ฅด๊ธฐ!!
      • filter out with elements that satisfy certain condition from array
      • condition has to be delivered in function form as an argument of filter method
      • part of higher order function since function takes as an argument
      • returns back as an 'ARRAY' to callback function that takes argument
      • 'CALLBACK' function will return a Boolean value
        • If result is 'TRUE', it will create an array with that element, if not, NO
      • 1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        function getIndex(arr, num) {
          return arr.filter(function (el) {
            return el < num;
          }).length// make an array with true value and get the length of that array
        }
         
        function filterOddLengthWords(words) {
          return words.filter(function (word) {
            return word.length % 2 !== 0// create an array that is true 
          });
        cs
    • Array.prototype.map() ๋Œ€์‘, ๊ฐฏ์ˆ˜๋ฅผ ๋งž์ถฐ์ค˜์•ผ ๋จ ๋ฐฐ์—ด์ž์ฒด์˜ ์ˆ˜์ •์šฉ!!
      • creates a 'NEW ARRAY' populated with result of calling a provided function on every element in the calling array
      • can extract values from list of objects
        1
        2
        3
        4
        5
        function getOnlyNames(arr) {
          return arr.map(function (el){
            return el.name// extract value of name through map filter
          })
        }
        cs
    • Array.protoype.reduce() ๋ณ€ํ™˜์ด ๋˜์–ด ์‘์ถ•, ์—ฐ์‚ฐ์šฉ!!
      •  executes a reducer function(that you provide) on each element of the array, resulting in 'SINGLE OUTPUT VALUE'
      • can make various type
      • arr.reduce(callback[, initialValue])
        • ์ดˆ๊ธฐ๊ฐ’ ์„ค์ • ์ค‘์š”!
          • if they want empty array, put [] like this
        • if there is initial value, it becomes first value to calculate(A value to use as the first argument to the first call of the callback. If no initialValue is supplied, the first element in the array will be used as the initial accumulator value and skipped as currentValue.)
          1
          2
          3
          4
          5
          function computeProductOfAllElements(arr) {
            return arr.reduce(function(acc, cur){
              return acc * cur
            }, 1//1 is initial value
          }
          cs
      • use when you want to merge arrays by using concat
        • 1
          2
          3
          4
          5
          function joinArrayOfArrays(arr){
            return arr.reduce(function(acc, cur){
              return acc.concat(cur)
            })
          }
          cs
      • If array is empty and there is no initial value, it will return 'typeerror'
      • If array is empty and there is inital value, it will return initial value

  • Array.prototype.sort()
    • sorts the elements of an array in place and returns the 'SORTED ARRAY'
    • default sort order is ascending, built upon converting elements into string then comparing their sequences of UTF-16  code units values
    • a[key] > b[key]=> a-b=positive(3-1=2)
      • if it is true, return 1 which makes A to go to back
    • a[key] < b[key] => a-b=negative(1-3=-2)
      • if it is true, return -1, which makes B to go to back
    • a[key] = b[key] 
      • return 0, leave as it is
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        items.sort(function (a, b) {
          if (a.value > b.value) {
            return 1;
          }
          if (a.value < b.value) {
            return -1;
          }
          // a must be equal to b
          return 0;
        });
        cs
  • Array.prototype.some()
    • tests whether 'AT LEAST ONE ELEMENT' in the array 'PASSES' the test implemented by the provided function
    • returns a Boolean Value
  • Array.prototype.every()
    • tests whether 'ALL ELEMENTS' in the array 'PASS' the test implemented by the provided function
    • returns a Boolean value
  • Array.prototype.forEach()
    • executes a provided function 'ONCE FOR EACH ARRAY ELEMENT'
  • Array.prototype.find()
    • returns the value of the 'FIRST ELEMENT' in the provided array that satisfies the provided testing function

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

TIL 2020.11.12  (0) 2020.11.14
TIL 2020.11.11  (0) 2020.11.14
TIL 2020.11.08  (0) 2020.11.08
TIL 2020.11.07  (0) 2020.11.07
TIL 2020.11.06  (0) 2020.11.06

+ Recent posts