Skip to content

Instantly share code, notes, and snippets.

View Woodsphreaker's full-sized avatar
🎯
Focusing

Carlo Enrico Woodsphreaker

🎯
Focusing
  • São Paulo
View GitHub Profile
@Woodsphreaker
Woodsphreaker / findOddAndEvenNumbers.js
Created June 6, 2017 22:51
Find odd and even numbers
const findOddEvenNumbers = (toNumber = 50) => [...Array.from({length: toNumber + 1}).keys()]
.reduce((acc, cur) => {
cur % 2 === 0 ? acc[0].push(cur) : acc[1].push(cur)
return acc
}, [[], []])
const evenNubers = findOddEvenNumbers(100)[0]
@Woodsphreaker
Woodsphreaker / mergeStringsMatrix.js
Last active June 9, 2017 11:17
Merging Strings with Matrix
const plus = (a = '') => (b = '') => a + b
const applyOperation = (fn) => (acc, cur) => acc.map((value, index) => fn(cur[index])(value))
const matrix = (list) => (fn) => list.reduceRight(applyOperation(fn))
const solve = (list) => (fn) => matrix(list)(fn).join('')
const str1 = "Testando".split('')
const str2 = "String".split('')
const str3 = "de".split('')
const str4 = "Varios".split('')
@Woodsphreaker
Woodsphreaker / operationsWithMatrix.js
Last active June 8, 2017 03:25
Operations with matrix
const plus = (a = 0) => (b = 0) => a + b
const times = (a = 0) => (b = 0) => a * b
const minus = (a = 0) => (b = 0) => a - b
const obelus = (a = 1) => (b = 1) => a / b
const applyOperation = (fn) => (acc, cur) => acc.map((value, index) => fn(cur[index])(value))
const matrix = (list) => (fn) => list.reduceRight(applyOperation(fn))
const solve = (list) => (fn) => matrix(list)(fn)
@Woodsphreaker
Woodsphreaker / operationsWithMatrixV2.js
Last active June 10, 2017 08:16
Operations with Matrix V2
const plus = (a = 0) => (b = 0) => a + b
const times = (a = 0) => (b = 0) => a * b
const minus = (a = 0) => (b = 0) => a - b
const obelus = (a = 1) => (b = 1) => a / b
const applyVerticalOperation = (fn) => (acc, cur) => acc.map((value, index) => fn(cur[index])(value))
const applyHorizontalOrDiagonalOperation = (fn) => (list) => list.reduce((acc, cur) => fn(acc)(cur))
const solveVerticalMatrix = (list) => (fn) => list.reduce(applyVerticalOperation(fn))
const solveHorizontalMatrix = (list) => (fn) => list.map(applyHorizontalOrDiagonalOperation(fn))
@Woodsphreaker
Woodsphreaker / fibonacci.js
Last active June 9, 2017 00:51
Fibonacci
const arrNumbers = (base = 10) => [...Array.from({"length" : base}).keys()]
const plus = (acc, cur, index) => acc.concat(index < 2 ? cur : acc[index - 2] + acc[index - 1])
const fib = (base) => arrNumbers(base).reduce(plus, [])
console.log(fib(40))
@Woodsphreaker
Woodsphreaker / rot13.js
Last active June 20, 2017 11:09
Rot 13
const ALPHABET = Array.from({"length": 26} , (_ , char) => String.fromCharCode(65 + char))
const getCharIndex = (letter) => ALPHABET.indexOf(letter)
const findNextChar = (index) => (changeTo) => {
return index >= 13
? ALPHABET[index - changeTo]
: ALPHABET[index + changeTo]
}
const getChar = (char) => (changeTo = 0) => {
return getCharIndex(char) >= 0
? findNextChar(getCharIndex(char))(changeTo)
@Woodsphreaker
Woodsphreaker / fizzbuzz.js
Last active June 19, 2017 23:58
Fizzbuzz
const generateListOfNumbers = (toMax = 50) => [...Array(toMax + 1).keys()].slice(1)
const isDivisibleBy = (number) => {
    return {
        "3" : "fizz",
        "5" : "buzz",
@Woodsphreaker
Woodsphreaker / clamp.js
Last active June 20, 2017 15:52
clamp.js
const testCondition = (min, max, x) => {
if (x < min) return min
if (x > max) return max
return x
}
const clamp = (min, max, x) =>
testCondition(min, max, x)
// Challenged by lubien ;-)
@Woodsphreaker
Woodsphreaker / pairwise.js
Last active June 30, 2017 20:59
Pairwise
const pairwise = (arr, arg) =>
/*
Primeiro laço (reduce) com os numeros recebidos no array
Inicio o reduce com um array contendo 2 posições, um número inteiro que vai armazenar as somas dos indices e um
objeto que vai armazenar os indices já usados no processo
*/
arr.reduce((acc, cur, indexR, arr) => {
/*
Segundo laço (forEach) para testar o valor recebido do reduce com cada valor do array com a finalizade de
satisfazer a condição do desafio
const dateDiff = (d1, d2, type = "days") => {
const date1 = d1 instanceof Date ? d1 : new Date(`${d1.slice(6)}, ${d1.slice(3, 5)}, ${d1.slice(0, 2)}`)
const date2 = d2 instanceof Date ? d2 : new Date(`${d2.slice(6)}, ${d2.slice(3, 5)}, ${d2.slice(0, 2)}`)
return {
days: () => {