르베르크
663
2021-12-31 15:11:21 작성 2021-12-31 15:13:41 수정됨
0
653

[자바스크립트] - 함수형 프로그래밍 예제2 비동기 -> 동기 처리


라이브러리는 마플에 유인동님이 올려주신 

FxJS  참고하였습니다.

비동기 처리는 소스가 완벽하지 않을수 있으며 개인적으로 공부하며 적용했던 예제 공유드립니다
개선점이 보이시면 답글 부탁드립니다.

<script src="https://unpkg.com/fxjs/dist/fx.es5.js"></script>
<script>
    const log = console.log;
    const {L}= _;
    log(_)

    // 데이터 리스트 Promise 로 구성된 데이터
    const list = [
        Promise.resolve({id: 1, name : "강성재", age : 30, gender : '남자', height: 180, status : "사용",
            family : [
                Promise.resolve({id: 2, name : "강성화", age : 37, gender : '여자', height: 145, status : "미사용"}),
                Promise.resolve({id: 2, name : "강성진", age : 35, gender : '남자', height: 185, status : "사용"}),
        ]}),
        Promise.resolve({id: 2, name : "김나은", age : 31, gender : '여자', height: 145, status : "미사용",
            family : [
                Promise.resolve({id: 2, name : "김나우", age : 18, gender : '남자', height: 195, status : "미사용"}),
                Promise.resolve({id: 2, name : "김나나", age : 22, gender : '여자', height: 175, status : "사용"}),
                Promise.resolve({id: 2, name : "김나자", age : 25, gender : '여자', height: 165, status : "미사용"}),
        ]}),
        Promise.resolve({id: 3, name : "이명우", age : 33, gender : '남자', height: 170, status : "사용",
            family : [
                Promise.resolve({id: 2, name : "이명상", age : 38, gender : '남자', height: 145, status : "사용"}),
        ]}),
        Promise.resolve({id: 4, name : "김동히", age : 35, gender : '남자', height: 180, status : "사용",
            family : [
        ]}),
        Promise.resolve({id: 5, name : "강하윤", age : 44, gender : '여자', height: 155, status : "미사용",
            family : [
                Promise.resolve({id: 2, name : "강성재", age : 31, gender : '여자', height: 145, status : "미사용"}),
                Promise.resolve({id: 2, name : "강성재", age : 31, gender : '여자', height: 145, status : "미사용"}),
        ]}),
        Promise.resolve({id: 6, name : "조자룡", age : 55, gender : '남자', height: 185, status : "사용",
            family : [
                Promise.resolve({id: 2, name : "조자상", age : 31, gender : '여자', height: 145, status : "미사용"}),
                Promise.resolve({id: 2, name : "조자하", age : 31, gender : '여자', height: 145, status : "미사용"}),
        ]}),
        Promise.resolve({id: 7, name : "강하은", age : 77, gender : '여자', height: 175, status : "사용",
            family : [
            Promise.resolve({id: 2, name : "강하윤", age : 38, gender : '여자', height: 145, status : "미사용"}),
            Promise.resolve({id: 2, name : "강하여", age : 40, gender : '여자', height: 145, status : "미사용"}),
        ]}),
        Promise.resolve({id: 8, name : "하우돈", age : 99, gender : '남자', height: 160, status : "사용",
            family : [
            Promise.resolve({id: 2, name : "하우상", age : 31, gender : '남자', height: 185, status : "미사용"}),
            Promise.resolve({id: 2, name : "하우진", age : 32, gender : '남자', height: 185, status : "사용"}),
        ]}),
        Promise.resolve({id: 9, name : "심성화", age : 10, gender : '여자', height: 145, status : "미사용",
            family : [
        ]}),
        Promise.resolve({id: 10, name : "임성유", age : 15, gender : '남자', height: 150, status : "사용",
            family : [
            Promise.resolve({id: 2, name : "임성진", age : 31, gender : '남자', height: 145, status : "미사용"}),
            Promise.resolve({id: 2, name : "임성윤", age : 31, gender : '남자', height: 145, status : "미사용"}),
        ]}),
        Promise.resolve({id: 11, name : "강진우", age : 55, gender : '남자', height: 170, status : "사용",
            family : [
            Promise.resolve({id: 2, name : "강진히", age : 12, gender : '여자', height: 145, status : "사용"}),
            Promise.resolve({id: 2, name : "강진오", age : 16, gender : '여자', height: 145, status : "사용"}),
        ]}),
        Promise.resolve({id: 12, name : "홍윤화", age : 77, gender : '여자', height: 160, status : "미사용",
            family : [
            Promise.resolve({id: 2, name : "홍구", age : 31, gender : '남자', height: 175, status : "미사용"}),
        ]}),
        Promise.resolve({id: 13, name : "유재석", age : 99, gender : '남자', height: 174, status : "미사용",
            family : [
        ]}),
        Promise.resolve({id: 14, name : "김설", age : 10, gender : '여자', height: 158, status : "사용",
            family : [
            Promise.resolve({id: 2, name : "김설희", age : 19, gender : '여자', height: 155, status : "사용"}),
            Promise.resolve({id: 2, name : "김설화", age : 20, gender : '여자', height: 145, status : "사용"}),
        ]}),
        Promise.resolve({id: 15, name : "양준", age : 15, gender : '남자', height: 167, status : "미사용",
            family : [
            Promise.resolve({id: 2, name : "양준혁", age : 50, gender : '남자', height: 195, status : "사용"}),
            Promise.resolve({id: 2, name : "양준희", age : 60, gender : '여자', height: 145, status : "미사용"}),
        ]}),
        Promise.resolve({id: 16, name : "일윤", age : 55, gender : '여자', height: 163, status : "사용",
            family : [
            Promise.resolve({id: 2, name : "일광", age : 36, gender : '남자', height: 165, status : "사용"}),
            Promise.resolve({id: 2, name : "일남", age : 38, gender : '남자', height: 169, status : "사용"}),
        ]}),
        Promise.resolve({id: 17, name : "하준석", age : 10, gender : '남자', height: 168, status : "사용",
            family : [
            Promise.resolve({id: 2, name : "하태석", age : 36, gender : '남자', height: 175, status : "사용"}),
            Promise.resolve({id: 2, name : "하용석", age : 37, gender : '남자', height: 178, status : "사용"}),
            Promise.resolve({id: 2, name : "하만석", age : 35, gender : '남자', height: 169, status : "사용"}),
            Promise.resolve({id: 2, name : "하민석", age : 34, gender : '남자', height: 160, status : "사용"}),
        ]}),
        Promise.resolve({id: 18, name : "양성일", age : 40, gender : '남자', height: 178, status : "미사용",
            family : [
            Promise.resolve({id: 2, name : "양선만", age : 45, gender : '남자', height: 170, status : "사용"}),
            Promise.resolve({id: 2, name : "양선자", age : 43, gender : '여자', height: 162, status : "사용"}),
            Promise.resolve({id: 2, name : "양선녀", age : 42, gender : '여자', height: 165, status : "사용"}),
        ]}),
        Promise.resolve({id: 19, name : "강치승", age : 55, gender : '남자', height: 173, status : "사용",
            family : [
        ]}),
        Promise.resolve({id: 21, name : "강치승", age : 40, gender : '남자', height: 178, status : "미사용",
            family : [
            ]}),
        Promise.resolve({id: 20, name : "이은비", age : 55, gender : '여자', height: 163, status : "사용",
            family : [
            Promise.resolve({id: 2, name : "이은결", age : 59, gender : '남자', height: 165, status : "사용"}),
        ]}),
    ];

    // 1. 상태가 [사용]인 유저의 [평균키] 계산
    async function f1(){
        await _.go(
            list,
            L.filter(({status}) => status  == "사용"),
            _.map(({height}) => height),
            _.reduce(_.add),
            (a) => log("1. 결과 : ",  a / list.length)
        );
    }

    // 2. 상태가 [미사용]인 유저의 [이름]을 [조인]해서 출력
    async function f2(){
        await _.go(
            list,
            L.filter(({status}) => status == "미사용"),
            _.map(({name}) => name),
            (res) => log("2. 결과 : ", _.join(",", res))
        );
    }

    // 3. 전체 리스트의 상태 확인하여 [Object]로 출력
    async function f3(){
        await _.go(
            list,
            L.map(({status}) => status),
            _.countBy((a) => a),
            (res) => log("3. 결과 : ", res)
        );
    }

    // 4. 키가 160 이상인 여성의 [이름] 출력
    async function f4(){
        await _.go(
            list,
            _.filter((obj) => obj.height >= 160 && obj.gender === "여자"),
            _.map(({name}) => name),
            (res) => log("4. 결과 : ", res)
        );
    }

    // 5. 리스트에서 이름이 [이은비]인 데이터 찾기
    async function f5(){
        await _.go(
            list,
            _.find(({name}) => name === "이은비"),
            (res) => log("5. 결과 : ", res)
        );
    }
    
    //6. [성별] 별로 그룹핑하기
    async function f6(){
        await _.go(
            list, 
            _.groupBy(({status}) => status),
            (res) => log("6. 결과 : ", res)
        );
    }

    // 7. 리스트 분해하여 [아이디] 값을 키로 가지는 오브젝트 만들기
    async function f7(){
        await _.go(
            list,
            _.indexBy(({id}) => id),
            (res) => log("7. 결과 : ", res)
        );
    }

    // 8. 유저의 가족들 의 [키] 리스트 추출
    async function f8(){
        await _.go(
                _.map(u => _.go(
                u.family,
                _.map(fu => `이름 : ${fu.name} | 키 : ${fu.height}cm `),
            )),
            (res) => log("8. 결과 : ", _.deepFlat(res))
        );
    }

    // 9. 유저상태가 [사용] 이며 가족의 상태도 [사용] 인 리스트 출력
    async function f9(){
        const statusFilter = (status) => _.filter(u => u.status === status);
        await _.go(
            list,
            statusFilter("사용"),
            _.map(u => _.go(
                u.family,
                statusFilter("사용"),
            )),
            (res) => log("9. 결과 : ", _.deepFlat(res))
        );
    }

    // 10. [5. 응용] 사용자 입력 값으로 찾기
    async function f10(){
        function nameFiendList(name, iter) {
            return _.go(
                _.findWhere({name : name}, iter),
                (res) => log(`10. 결과 : ${name} : `, (res))
            );
        }

        await nameFiendList('양선자', list);
        await nameFiendList('이은비', list);
    }

    // 11. 중복제거 예제 동명이인 나오면 없애기
    async function f11(){
        const uniqueBy = _.uniqueBy(({name}) => name.toUpperCase());
        await _.go(
            list,
            uniqueBy,
            _.map(({name}) => name),
            (res) => log("11. 결과 : ", (res))
        );
    }

    // 12. 성이 강씨인 유저, 가족 검색
    async function f12(){
        const myRe  = /^강/;
        const filterUser = _.filter(({name}) => myRe.test(name));
        const iter = list;
        const familyList = await _.go(
            iter,
            L.map((u) => u.family),
            _.deepFlat,
            filterUser,
        );
        
        const userList = await _.go(
            iter,
            filterUser,
        );

        log("12. 결과 : ", [...await familyList, ...await userList]);
    }


    // 비동기 순차실행
    (async ()=>{
        console.time("A");
        Promise.all(
            [
                await f1(),
                await f2(),
                await f3(),
                await f4(),
                await f5(),
                await f6(),
                await f7(),
                await f8(),
                await f9(),
                await f10(),
                await f11(),
                await f12(),
            ]
        )
        .then(e => {
            log("동기 가 완료되었습니다.");
            console.timeEnd("A");
        })
        .catch(e => log(e));
    })();
</script>

0
  • 댓글 0

  • 로그인을 하시면 댓글을 등록할 수 있습니다.