pibo

피보나치

by jinam yu

HTML

<div id="output"></div>

JavaScript

const pipe = (...fns) => x => fns.reduce((y, fn)=>fn(y), x);
const curry = (fn, ...args) => {
	return (fn.length <= args.length) ? fn(...args) : (...more) => curry(fn, ...args, ...more);
}

const selector = curry((selector)=>{
	return document.querySelector(selector);
});
const innerHTML = curry((str, el)=>{
	el.innerHTML = str;
  return el;
});

const pibo = (idx) => {
	const arr = Array(idx).fill(0).reduce((a,b)=>{
  	return [a[1], a[0]+a[1]]; // [1,2], [2, 3], [3, 5], [5, 8]
  }, [1,1]); // [1, 1, 2, 3, 5, 8]
  return arr[0];
}

const log = (msg) => {
	pipe(
  	selector,
    innerHTML(msg),
  )("#output")
}
pipe(
  pibo,
  log,
)(5);