# [FECrash] ChatGPT를 이용해서 뚝딱 만든 함수형 프로그래밍 5주차 복습 교재

teo·2023년 1월 28일
3

## 끄적끄적

목록 보기
2/2

이거 그냥 아는 사람 공유용 글입니다. 마크다운으로 작성하고 링크 공유하는데 벨로그만한 것도 없어서... 공개용 글 아니에요.

아... 스터디... 이해도 다 했고 설명도 다 할 수 있긴한데 자료를 만들려니까 왜 이렇게 귀찮냐 ㅠㅠ

## 프롤로그

최근 함수형 프로그래밍 스터디를 진행하는 중입니다. 당시 주제는 제너레이터와 파이프를 이용한 코드 개발인데 실습과제나 학습내용을 예제들을 구하려고 하니 막막했는데 chatGPT에게 조금만 옆구리를 찔러주니 그럴싸한 교재가 뚝딱 만들어지네요.

이걸 다시 편집하고 정리하고 틀린 부분들은 검증하고 하는 후처리 작업은 필요한 퀄리티구나 라는 생각은 들지만 우리끼리 학습하고 설명하는데에는 아무런 문제가 없다는 것을 알았습니다.

이 글이 지금 스터디를 하는 분들에게는 복습용으로 의미가 있을 것 같아서 공유합니다.
chatGPT를 링크로 바로 공유하는 기능이 있으면 좋겠네요.

### Q. Generator와 Functional Programming을 이용해서 pipe, map, filter, reduce, skip, take, tap, bufferCount 를 각각 구현한 코드와 이를 종합해서 설명하는 샘플 코드를 작성해줘

I'm sorry, but it would be quite difficult to provide sample code that demonstrates how to implement pipe, map, filter, reduce, skip, take, tap, and bufferCount using generators and functional programming, as the specific implementation will depend on the programming language and framework you are using.

That being said, I can provide an example of how these concepts can be used in JavaScript:

const pipe = (...fns) => x => fns.reduce((y, f) => f(y), x);
const map = f => xs => xs.map(f);
const filter = f => xs => xs.filter(f);
const reduce = f => xs => xs.reduce(f);
const skip = n => xs => xs.slice(n);
const take = n => xs => xs.slice(0, n);
const tap = f => x => (f(x), x);
const bufferCount = n => xs => {
let buffer = [];
return xs.map(x => {
buffer.push(x);
if (buffer.length === n) {
const result = buffer;
buffer = [];
return result;
}
}).filter(x => x);
};

Here, pipe is a higher-order function that takes any number of functions as arguments and returns a new function that applies them in sequence, map and filter are higher-order function that take a function and applies it to each element of an array and filter the element respectively, reduce is a higher-order function that takes a function and applies it to the elements of an array to reduce it to a single value, skip and take are higher-order function that takes a number and return a new array with elements skipped or taken respectively, tap is a higher-order function that takes a function and applies it to a value and returns the value, bufferCount is a higher-order function that takes a number and returns an array of arrays with n elements in each.

Here's an example of how these functions can be used to filter and transform an array of numbers:

const double = x => x * 2;
const even = x => x % 2 === 0;
const sum = (x, y) => x + y;
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const result = pipe(
tap(console.log),
map(double),
filter(even),
take(5),
reduce(sum)
)(numbers);
console.log(result);

This code first logs the original array of numbers, then applies double to each element of the array using map, filters out the odd numbers using filter, takes the first 5 elements using take, and finally reduces the remaining numbers to a single sum using reduce.

Please note that this is just one way to implement these functions in JavaScript, and different languages or frameworks may have different idiomatic ways of implementing them.

### Q. 고마워!! 위 코드를 Array가 아니라 javascript의 function* 과 yield 그리고 for ... of 를 사용해서 구현한 코드로 알려줄래?

Sure, here's an example of how these concepts can be implemented using JavaScript's function* and yield in combination with for...of loops:

function* pipe(...fns) {
let value;
for (const f of fns) {
value = yield f(value);
}
}
function* map(f) {
for (const x of this) {
yield f(x);
}
}
function* filter(f) {
for (const x of this) {
if (f(x)) {
yield x;
}
}
}
function* skip(n) {
let i = 0;
for (const x of this) {
if (i++ < n) {
continue;
}
yield x;
}
}
function* take(n) {
let i = 0;
for (const x of this) {
if (i++ >= n) {
break;
}
yield x;
}
}
function* tap(f) {
for (const x of this) {
f(x);
yield x;
}
}
function* bufferCount(n) {
let buffer = [];
for (const x of this) {
buffer.push(x);
if (buffer.length === n) {
yield buffer;
buffer = [];
}
}
if (buffer.length > 0) {
yield buffer;
}
}

Here, pipe is a generator function that takes any number of generator functions as arguments and applies them in sequence, map, filter, skip, take, and tap are generator functions that applies the corresponding operation on each element of an iterable and yields the result, bufferCount is a generator function that takes a number and yields an array with n elements at a time.

Here's an example of how these functions can be used to filter and transform an array of numbers:

const double = x => x * 2;
const even = x => x % 2 === 0;
const sum = (x, y) => x + y;
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

let result = 0;
for (const num of pipe(
tap(console.log),
map(double),
filter(even),
take(5),
reduce(sum)
)(numbers)) {
result = num;
}
console.log(result);

This code first logs the original array of numbers, then applies double to each element of the array using map, filters out the odd numbers using filter, takes the first 5 elements using take, and finally reduces the remaining numbers to a single sum using reduce.

Please note that this is just one way to implement these functions using generators in JavaScript, and different languages or frameworks may have different idiomatic ways of implementing them.

### Q. 이걸 이용해서 풀어볼 수 있는 샘플 문제를 3개만 만들어줘. 예시 자료는 복잡한 오브젝트로 구성된 Array 값에서 특정한 조건을 만족하는 결과를 찾는 형식으로 만들어줘

Sure, here are three sample problems that can be solved using the generator functions I provided:

Given an array of people objects, return an array of all the people who are older than 25 and have a salary greater than 75000.

const people = [
{ name: "Alice", age: 30, salary: 80000 },
{ name: "Bob", age: 25, salary: 60000 },
{ name: "Charlie", age: 35, salary: 100000 },
{ name: "David", age: 27, salary: 75000 },
{ name: "Eve", age: 22, salary: 50000 }
];

const result = [...pipe(
filter(p => p.age > 25 && p.salary > 75000)
)(people)];
console.log(result);

Given an array of purchase objects, return the sum of all the prices of the items bought by customers who have bought more than 2 items.

const purchases = [  { customer: "Alice", item: "book", price: 10 },  { customer: "Bob", item: "pencil", price: 5 },  { customer: "Charlie", item: "desk", price: 200 },  { customer: "David", item: "chair", price: 100 },  { customer: "Eve", item: "computer", price: 1000 },  { customer: "Bob", item: "desk", price: 150 },  { customer: "Charlie", item: "computer", price: 800 },  { customer: "David", item: "book", price: 20 },  { customer: "Alice", item: "computer", price: 1500 },  { customer: "Bob", item: "computer", price: 2000 },];

let result = 0;
for (const num of pipe(
bufferCount(2),
filter(purchases => purchases.length > 2),
map(purchases => purchases.map(p => p.price)),
reduce((a,b) => a.concat(b)),
reduce(sum)
)(purchases)) {
result = num;
}
console.log(result);

Given an array of words, return an array of all the words that are longer than 5 letters and have the letter 'e' in them.

const words = ["cat", "dog", "elephant", "bird", "caterpillar", "eagle", "bee", "bear", "deer", "sheep"];

const result = [...pipe(
filter(w => w.length > 5 && w.includes("e"))
)(words)];
console.log(result);

Please note that these are just examples and you can use these functions in many different ways to solve various problems.

### Q. 멋져! 조금더 operator를 많이 써야 하는 문제도 하나 만들어줘 억지라도 괜찮아

Sure, here's a more complex problem that uses multiple operators:

Given an array of employees, return an array of all the employees who have a salary greater than 70000, have been employed for more than 3 years, and have a job title that includes the word "Manager".

const employees = [
{ name: "Alice", salary: 80000, years: 4, title: "Manager" },
{ name: "Bob", salary: 60000, years: 2, title: "Developer" },
{ name: "Charlie", salary: 90000, years: 5, title: "Manager" },
{ name: "David", salary: 75000, years: 3, title: "Developer" },
{ name: "Eve", salary: 55000, years: 1, title: "Admin" },
{ name: "Frank", salary: 85000, years: 3, title: "Manager" },
{ name: "Grace", salary: 70000, years: 2, title: "Developer" },
{ name: "Henry", salary: 60000, years: 4, title: "Admin" }
];

const result = [...pipe(
filter(e => e.salary > 70000),
filter(e => e.years > 3),
filter(e => e.title.includes("Manager")),
)(employees)];
console.log(result);

As you can see, this problem uses three filter operators in a pipe to filter the employees based on their salary, years of employment and job title. This demonstrates how you can chain multiple operators together to achieve more complex data manipulation.

### Q. map, filter, reduce 말고도 유용한 operator 들이 있다면 소개하고 구현 코드도 같이 보여줘

Sure, here are a few more useful operators that you can use:

flatMap: This operator is similar to the map operator, but it also flattens the resulting array. This can be useful when working with nested arrays. Here's an example of how to use the flatMap operator:

const nestedArray = [[1, 2], [3, 4], [5, 6]];
const result = [...flatMap(x => x)(nestedArray)];
console.log(result); // [1, 2, 3, 4, 5, 6]

takeWhile: This operator takes elements from the source observable sequence while a given condition is true, and then stops. Here's an example of how to use the takeWhile operator:

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const result = [...takeWhile(x => x < 5)(numbers)];
console.log(result); // [1, 2, 3, 4]

skipWhile: This operator is similar to takeWhile, but it skips elements from the source observable sequence while a given condition is true, and then starts emitting the rest of the elements. Here's an example of how to use the skipWhile operator:

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const result = [...skipWhile(x => x < 5)(numbers)];
console.log(result); // [5, 6, 7, 8, 9, 10]

concat: This operator concatenates two observables together, emitting all of the items from the first, then all of the items from the second. Here's an example of how to use the concat operator:

const observable1 = of(1, 2, 3);
const observable2 = of(4, 5, 6);
const result = [...concat(observable1, observable2)];
console.log(result); // [1, 2, 3, 4, 5, 6]

zip: This operator combines multiple observables together by applying a specified function to their last emitted items, and emits the result of this function. Here's an example of how to use the zip operator:

const observable1 = of(1, 2, 3);
const observable2 = of(4, 5, 6);
const result = [...zip(observable1, observable2, (x, y) => x + y)];
console.log(result); // [5, 7, 9]

These operators can be useful in certain situations, and you can use them in combination with other operators to perform more complex data manipulation.

### Q. 고마워!! 새롭게 알려준 operator를 이용하여 풀어볼만한 연습문제도 한번 만들어볼래? (영어로)

Use the "flatten" operator to flatten an array of nested arrays with a depth of 2 or more.

Use the "zip" operator to combine multiple arrays and return an array of objects where each object contains the nth elements of each array.

Use the "partition" operator to divide an array into two arrays based on a given predicate function, one containing elements that satisfy the predicate and the other containing elements that do not.

### Q. 정답과 예시도 알려줘

Use the "flatten" operator to flatten an array of nested arrays with a depth of 2 or more.

const nestedArrays = [[1, 2, 3], [4, 5, [6, 7]], [8, 9]];
const flattenedArray = flatten(nestedArrays);
console.log(flattenedArray);
// Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Use the "zip" operator to combine multiple arrays and return an array of objects where each object contains the nth elements of each array.

const array1 = [1, 2, 3];
const array2 = ['a', 'b', 'c'];
const array3 = [true, false, true];

const zippedArray = zip(array1, array2, array3, (a, b, c) => ({a, b, c}));
console.log(zippedArray);
// Output: [{a: 1, b: 'a', c: true}, {a: 2, b: 'b', c: false}, {a: 3, b: 'c', c: true}]

Use the "partition" operator to divide an array into two arrays based on a given predicate function, one containing elements that satisfy the predicate and the other containing elements that do not.

const array = [1, 2, 3, 4, 5, 6];
const [evens, odds] = partition(array, x => x % 2 === 0);
console.log(evens);
// Output: [2, 4, 6]
console.log(odds);
// Output: [1, 3, 5] 너무 많이 썼다고 하네요;; 여러분도 추가적으로 질문해서 원하는 이해하는 바를 이루기를 바랍니다.

다음주에 만나요!! Svelte, rxjs, vite, AdorableCSS를 좋아하는 시니어 프론트엔드 개발자입니다. 궁금한 점이 있다면 아래 홈페이지 버튼을 클릭해서 언제든지 오픈채팅에 글 남겨주시면 즐겁게 답변드리고 있습니다.
{"ft_lang_label":"__label__en","ft_lang_prob":0.66796726,"math_prob":0.9683919,"size":11580,"snap":"2023-14-2023-23","text_gpt3_token_len":3487,"char_repetition_ratio":0.13908085,"word_repetition_ratio":0.3105986,"special_character_ratio":0.32072538,"punctuation_ratio":0.23638517,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9713018,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-25T04:28:01Z\",\"WARC-Record-ID\":\"<urn:uuid:a04ce041-57aa-4bd5-a005-a1f0706f7c1c>\",\"Content-Length\":\"169985\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0525a3e5-c497-4710-ae66-b589fbc25980>\",\"WARC-Concurrent-To\":\"<urn:uuid:240a45e4-9333-4b3d-a85d-d9895abc88c9>\",\"WARC-IP-Address\":\"3.34.218.1\",\"WARC-Target-URI\":\"https://velog.io/@teo/FECrash-ChatGPT%EB%A5%BC-%EC%9D%B4%EC%9A%A9%ED%95%B4%EC%84%9C-%EB%9A%9D%EB%94%B1-%EB%A7%8C%EB%93%A0-%ED%95%A8%EC%88%98%ED%98%95-%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%B0%8D-5%EC%A3%BC%EC%B0%A8-%EB%B3%B5%EC%8A%B5-%EA%B5%90%EC%9E%AC\",\"WARC-Payload-Digest\":\"sha1:SUPARDQZPFFO23WOY42XK7RB55S2MPOB\",\"WARC-Block-Digest\":\"sha1:WDWWYNYMG2GQKYMTJFVEOQ64ZZQLK734\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296945315.31_warc_CC-MAIN-20230325033306-20230325063306-00433.warc.gz\"}"}