ES6+함수형-함수형 자바스크립트 기본기

Min·2021년 2월 16일
0

JavaScript

목록 보기
17/18
post-thumbnail

함수형 프로그래밍과 JavaScript ES6+ 강의를 수강하며 정리한 내용입니다.

1. 평가와 일급

1) 평가

  • 코드가 계산(Evaluation) 되어 값을 만드는 것

2) 일급

  • 값으로 다룰 수 있다.
  • 변수에 담을 수 있다.
  • 함수의 인자로 사용될 수 있다.
  • 함수의 결과로 사용될 수 있다.
const a = 10; // 변수에 담을 수 있다.
const add10 = a => a + 10; // a + 10 : 함수의 결과로 사용될 수 있다.
const r = add10(a); // add10(a)의 a : 함수의 인자로 사용될 수 있다.
log(r);

2. 일급 함수

일급 함수

  • 함수를 값으로 다룰 수 있다.
  • 조합성과 추상화의 도구
const add5 = a => a + 5; // 함수를 값으로 다루어 add5에 넣을 수 있다.
log(add5); // 함수의 인자로 함수가 사용될 수 있다.
log(add5(5)); // 함수를 평가해 결과로 만들어 값을 다른 함수에게 전달할 수 있다.

const f1 = () => () => 1; // 함수의 결과값으로 함수가 사용될 수 있다.
log(f1()); // () => 1

const f2 = f1();
log(f2); // () => 1
log(f2()); // 출력:1, 원하는 시점에 평가 가능

3. 고차 함수

1) 일급 함수

  • 함수가 값으로 다뤄질 수 있다.

2) 고차 함수

  • 함수를 값으로 다루는 함수

① 함수를 인자로 받아서 실행하는 함수

  • apply1
  • times
const apply1 = f => f(1); // 함수가 함수를 받아 안에서 실행
const add2 = a => a + 2;
log(apply1(add2)); // const apply1 = f => (a => a + 2)(1)
log(apply1(a => a - 1));

// 숫자(n)만큼 함수(f)를 실행하는 함수(times)
const times = (f, n) => {
  let i = -1;
  while (++i < n) f(i);
};

times(log, 3);

times(a => log(a + 10), 3);

② 함수를 만들어 리턴하는 함수 (클로저를 만들어 리턴하는 함수)

  • addMaker
const addMaker = a => b => a + b; // b => a + b 함수를 리턴
// 클로저 : b => a + b 함수는 파라미터 a를 계속 기억
const add10 = addMaker(10);
log(add10(5));
log(add10(10));
profile
slowly but surely

9개의 댓글

comment-user-thumbnail
2025년 4월 15일

Experience Apple Worm, a modern take on the classic snake game featuring 50+ challenging levels, stunning apple-themed graphics, and real-time global rankings. Perfect for casual gamers seeking nostalgic yet innovative gameplay. https://appleworm.org/

답글 달기
comment-user-thumbnail
2025년 4월 15일

Transform your photos into beautiful Studio Ghibli-style artwork with our AI-powered platform. Experience the magic of a Ghibli AI generator for fast, easy, and stunning results in the iconic studio ghibli style. https://ghiblistyleai.org

답글 달기
comment-user-thumbnail
2025년 4월 15일

https://detectorfaceshape.com Get personalized style advice and discover what is my face shape with our advanced AI face shape detector and analyzer. Experience the most accurate face shape detection online.

답글 달기
comment-user-thumbnail
2025년 4월 15일

Obby is a free online game with 35 unique Obby levels. No download required, play on any device with a web browser! https://obby.online

답글 달기
comment-user-thumbnail
2025년 4월 15일

https://kourio.online Kour.io is a fast-paced online multiplayer FPS developed by LEGiON Platforms, blending retro pixel-art aesthetics with modern tactical gameplay. Updated in November 2024, it features 5 dynamic maps, 7 unique character classes, and a weapon progression system that evolves from pistols to rocket launchers. Players engage in cross-platform battles (PC/mobile/browser) across three core modes: Team Deathmatch, Gun Game, and Free-for-All. With over 1.8 million players globally, it supports 60FPS gameplay and community-driven content via a built-in map editor.

답글 달기
comment-user-thumbnail
2025년 4월 15일

https://squaredle.online squaredle is a highly captivating and completely free daily word puzzle game, ingeniously developed by the team at Fubar Games. Offering a significant and refreshing twist on the popular daily word game format popularized by Wordle, squaredle presents a unique challenge that has quickly garnered a dedicated following. Originally launched in 2022 and consistently updated (latest noted update March 2025), this engaging game tasks players with solving six distinct five-letter words that interlock cleverly on a standard 5x5 grid. Success in squaredle relies on logical deduction, a good vocabulary, and strategic use of its intuitive color-coded hint system.

답글 달기
comment-user-thumbnail
2025년 4월 15일

https://flipbottle.org Welcome to bottle flip, the most advanced online bottle flipping game! Challenge yourself with realistic physics, compete globally, and experience the thrill of landing the perfect flip.

Whether you're a casual player or aiming for the leaderboards, our physics-based gameplay provides endless entertainment and challenge. Start flipping now and join millions of players worldwide!

답글 달기
comment-user-thumbnail
2025년 4월 15일

https://bouncy-balls.org The classroom noise management tool designed for teachers supports multiple display modes to make classroom management more intuitive, fun and efficient.

답글 달기
comment-user-thumbnail
2025년 4월 15일

Experience Apple Worm, a modern take on the classic snake game featuring 50+ challenging levels, stunning apple-themed graphics, and real-time global rankings. Perfect for casual gamers seeking nostalgic yet innovative gameplay. https://appleworm.org/

답글 달기