Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[06_week] users에서 원하는 값을 도출하기3 #6

Open
joeunha opened this issue Feb 7, 2018 · 5 comments

Comments

@joeunha
Copy link
Owner

commented Feb 7, 2018

users에서 원하는 값을 도출하기!

users

var users = [
  { name: 'ID', age: 32, city: 'seoul', blood: 'A' },
  { name: 'BJ', age: 31, city: 'seoul', blood: 'O' },
  { name: 'JM', age: 32, city: 'busan', blood: 'O' },
  { name: 'PJ', age: 27, city: 'seongnam', blood: 'B' },
  { name: 'HA', age: 27, city: 'seoul', blood: 'O' },
  { name: 'JE', age: 27, city: 'seongnam', blood: 'O' },
  { name: 'JI', age: 32, city: 'incheon', blood: 'A' },
  { name: 'MP', age: 28, city: 'seoul', blood: 'O' },
  { name: 'JY', age: 31, city: 'seoul', blood: 'O' },
  { name: 'TH', age: 27, city: 'busan', blood: 'AB' },
  { name: 'DS', age: 33, city: 'incheon', blood: 'O' },
  { name: 'YJ', age: 24, city: 'busan', blood: 'O' },
  { name: 'MB', age: 37, city: 'incheon', blood: 'B' },
  { name: 'JJ', age: 29, city: 'busan', blood: 'A' },
  { name: 'TU', age: 21, city: 'seoul', blood: 'O' },
  { name: 'UB', age: 23, city: 'seongnam', blood: 'O' },
];

원하는 값

  • 부산 외에 거주하면서 혈액형이 O형인 나이가 가장 많은 세 사람
  • 추천 함수 _.sortBy, _.reject, _.filter, _.take(== _.first)

유의 사항

  • partial.js 혹은 이전에 만들어둔 함수를 적극 활용해주세요!
  • 결과는 금요일 자정 이후에 댓글로 정답을 남겨주세요. (그 전에 완성하셨더라도 혼자봐주세요.)
  • 토요일에 함께 보며 공부해요. 😁

시간이 된다면

function go() {
  var i = 0, fs = arguments, len = fs.length, res = arguments[0];
  while (++i < len) res = res && fs[i](res);
  return res;
}
  • go 함수를 비동기 제어가 가능한 함수로 만들어보세요.
@heongkyun

This comment has been minimized.

Copy link

commented Feb 9, 2018

_.go(users,
    _.filter(u => u.city !== 'busan' && u.blood === 'O'),
    _.sortBy(v => -v.age),
    _.first(3),
    _.pluck('name'),
    _.log
);
@jeemyeong

This comment has been minimized.

Copy link

commented Feb 9, 2018

function reverse(list) {
  return _.reduceRight(list, (prev, cur) => _.append(prev, cur), []);
}
function take(list, n) {
  return _.filter(list, (val, index) => index < n)
}

_.go(users,
  _.reject(__(_.val("city"), _.isEqual("busan"))),
  _.filter(__(_.val("blood"), _.isEqual("O"))),
  _.sortBy("age"),
  reverse,
  _(take, _, 3),
  _.log
)

function go() {
  var i = 0, fs = arguments, len = fs.length, res = arguments[0];
  while (++i < len) {
    res = res instanceof Promise ? res.then(fs[i]) : (fs[i](res))
  }
  return res;
}
@JeewhanR

This comment has been minimized.

Copy link

commented Feb 10, 2018

function mm(func) {
  const cache = new Map();
  return arg => cache.has(arg)
    ? console.log("본체 실행"), cache.set(arg, func(arg)), cache.get(arg)
    : console.log("캐시 실행"), cache[arg];
}

var mult5 = mm(function(a) { return a * 5; });
console.log( mult5(1) ); // 본체 실행, 5
console.log( mult5(2) ); // 본체 실행, 10
console.log( mult5(1) ); // 캐시 실행, 5
@jeemyeong

This comment has been minimized.

Copy link

commented Feb 10, 2018

  function memoize(fn) {
    var cache = new Map();
    return function (...args) {
      var hashed_args = args.join("|");
      if (cache.has(hashed_args)) {
        return cache.get(hashed_args);
      }
      cache.set(hashed_args, fn(...args));
      return cache.get(hashed_args);
    }
  }
@joeunha

This comment has been minimized.

Copy link
Owner Author

commented Feb 10, 2018

  • 부산 외에 거주하면서 혈액형이 O형인 나이가 가장 많은 세 사람
var count = 0;

_.go(users,
  _.sort_by('age'),
  arr => arr.reverse(), 
  _.reject(arr => {
    count++;
    return arr.city === 'busan'}),
  _.filter(arr => {
    count++;
    return arr.blood === 'O'}), 
  _.map(arr => {
    count++;
    return arr.name}), 
  _.take(3),
  _.log)

console.log(count); // 36
  • 부산 외에 거주하면서 혈액형이 O형인 나이가 가장 많은 세 사람 (지연평가)
var count = 0;

_.go(users,
  _.sort_by('age'),
  arr => arr.reverse(), 
  lz.reject(arr => {
    count++;
    return arr.city === 'busan'}),
  lz.filter(arr => {
    count++;
    return arr.blood === 'O'}), 
  lz.map(arr => {
    count++;
    return arr.name}), 
  lz.take(3),
  _.log)

console.log(count); // 16
  • 지연평가 함수 세트
  var lz = {};
  function lz_add(...fns) {
    return list => {
      if (list.is_lazy) return list.push(fns), list;
      var lazy = [fns];
      lazy.data = list;
      lazy.is_lazy = true;
      return lazy;
    }
  }

  lz.map = fn => lz_add(fn, map)
  lz.filter = fn => lz_add(fn, filter)
  lz.reject = fn => lz_add(fn, reject)
  lz.take = limit => {
    return lazy => {
      let i = -1, ll = lazy.length, dl = lazy.data.length, res = [];
      while (++i < dl) {
        let j = 0, v = lazy.data[i], rev = lazy[j][1]([v], lazy[j][0])[0];
        while (++j < ll) {
          if (rev) rev = lazy[j][1]([v], lazy[j][0])[0];
        }
        if (rev) res.push(rev);
        if (res.length === limit) return res;
      }
    }
  }
  • asy_go
  function asy_go() {
    var fs = arguments, i = 0, len = arguments.length;
    return function f(res) {
      if (res && res.then) return res.then(f);
      if (i < len) return f(fs[i++](res));
      return res;
    }(arguments[i++]);
  };
  • memoize
  function memoize(fn, hasher) {
    f.cache = {};
    function f(...args) {
      let key = hasher(...args);
      if (!has(f.cache, key)) return f.cache[key] = fn(...args);
      return f.cache[key];
    }
    return f;
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.