Last active
February 15, 2019 15:07
-
-
Save nikitalarionov/6946b6bc3175e4118358ce1797dc6717 to your computer and use it in GitHub Desktop.
DGTF Ch04 Exercises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const compose = (f, g) => x => f(g(x)); // Receives functions | |
const join = x => { | |
return y => { | |
if (typeof y === 'string') { | |
y = y.split(); | |
} | |
return y.join(x); | |
} | |
} | |
const map = fn => x => x.map(fn); // | |
const split = x => y => y.split(x); | |
const toLower = x => x.toLowerCase(); | |
const replace = x => y => y.replace(x); | |
const dasherize = compose( | |
join('-'), | |
map(toLower), | |
split(' '), | |
replace(/\s{2,}/ig, ' '), | |
); | |
console.log(dasherize('The world is a vampire')); | |
// TODO MAKE IT WORK |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Сделаем для начала ссылку на оригинальную функцию с помощью каррирования | |
const split = x => str => String.prototype.split.call(str, x); | |
// Более короткая версия | |
const _split = x => str => str.split(x); | |
// Потом применим частичный вызов | |
const words = split(' '); | |
// Проверим результат | |
console.log(words('Hello world guys!')); | |
// output: [ 'Hello', 'world', 'guys!' ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Разберем более простой пример | |
// Применим каррирование | |
const filter = fn => x => x.filter(fn); | |
// Создадим функцию которая фильтрует массив и возвращает числа больше 5 | |
const someFilterFn = function(number) { | |
return number > 5; | |
}; | |
// Создадим список с числами | |
const numList = [1, 2, 3, 4, 5, 6]; | |
// Создадим нужную функцию которая фильтрует массив | |
const moreThanFive = filter(someFilterFn); | |
// Проверим результат | |
console.log(moreThanFive(numList)); | |
// [6] | |
// 1. Применили каррирование чтобы получить функцию которая фильтрует массив | |
// 2. Создали нужную нам функцию и определили данные. | |
// 3. Мы использовали подход Pointfree при создании функции moreThanFive | |
// Решение задания | |
// 1 Сделаем реф на match, через каррирование | |
const match = re => x => x.match(re); | |
// Ответ | |
// 2 Сделаем новую фильтр функцию принимающую массив, она отбрасывает в массиве все строки без q | |
const filterQs = filter(match(/q/i)); | |
// 3 Проверим результат | |
console.log(filterQs(['q1', 'q2', 'q3', '1', '2', '3', '4']); | |
// ['q1', 'q2', 'q3'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const compose = (f, g) => x => f(g(x)); | |
const addSmile = x => { | |
x = x + ':)'; | |
return x; | |
}; | |
const toUpperCase = (x) => { return x.toUpperCase(); } | |
const joinAndUpperCase = compose(addSmile, toUpperCase); | |
const result = joinAndUpperCase('Hello world!'); | |
console.log(result); | |
// HELLO WORLD!:) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment