Skip to content

Instantly share code, notes, and snippets.

@starsinmypockets
Created July 14, 2017 18:37
Show Gist options
  • Select an option

  • Save starsinmypockets/92f4f67c19711242f2629e30766fde0a to your computer and use it in GitHub Desktop.

Select an option

Save starsinmypockets/92f4f67c19711242f2629e30766fde0a to your computer and use it in GitHub Desktop.
// Exercise 1
//==============
// Refactor to remove all arguments by partially applying the function.
const words = function(str) {
return _.split(' ', str);
};
const bibleIntro = function(str) {
return words("In the beginiing " + str);
}
const introArr = bibleIntro("God created jam");
const sentences = function (words) {
return words.join(' ');
}
const introStr = sentences(introArr);
console.log("Exc. 1", introArr, "Exc. 1a", introStr);
// Exercise 2
//==============
// Refactor to remove all arguments by partially applying the functions.
const filterQs = function(xs) {
return _.filter(function(x) {
console.log(x);
return !x.match(/q/i);
}, xs);
};
const filterLetter = function (letter, xs) {
return _.filter(function (x) {
const rr=new RegExp(letter, 'i');
return !x.match(rr);
}, xs);
}
const filterQ = function (arr) {
return filterLetter('q', arr);
}
const noqs = filterQ(['a', 'q', 'Q', 'abc']);
// Exercise 3
//==============
// Use the helper function _keepHighest to refactor max to not reference any
// arguments.
// LEAVE BE:
var _keepHighest = function(x, y) {
return x >= y ? x : y;
};
// REFACTOR THIS ONE:
var max = function(xs) {
return _.reduce(function(acc, x) {
return _keepHighest(acc, x);
}, -Infinity, xs);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment