Skip to content

Instantly share code, notes, and snippets.

View suissa's full-sized avatar
🏠
Working from home

Jean Carlo Nascimento suissa

🏠
Working from home
  • Oncorithms Institute
  • Brasil
View GitHub Profile
/**
* Created in 21/06/2017.
* Author: William Dias Vargas
* Github: @wdiasvargas
*/
'use strict';
import yCombFact from './y-combinator-factorial'
export default (number) => ((fn) => fn(fn, number))((f, n) => (n <= 1)? 1: n * f(f, (n - 1)))
console.info(yCombFact(5))
@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]
@XerxesNoble
XerxesNoble / animate_values.js
Last active March 9, 2024 13:07
Basic javascript linear value animation that can accept easing functions and provides update & complete callbacks
/**
* @desc Basic linear value animation that can accept simple easing functions and provides update & complete callbacks
* @param {Object} values - Object with numerical values. eg. { value1: 0, value2: 20, someKey: 55 }
* @param {Number} duration - How long (in milliseconds) the animation will be
* @param {Object} options - target values, update callback & complete callback
* @param {Function} [options.onComplete=(values) => values] - Callback that fires once animation is complete
* @param {Function} [options.onUpdate=(values) => values] - Callback that fires when animation frame updates
* @param {Function} [options.ease=(t) => t] - easing method eg. https://gist.github.com/gre/1650294
* @example
*
@Woodsphreaker
Woodsphreaker / objectManipulate.js
Created April 21, 2017 22:07
objectManipulate
const myItens = {
itens: []
}
const addItens = (obj, value) => obj.itens.push(value)
const deleteItem = (obj, toseek) => obj.itens.map(remove(obj, toseek))
const updateItem = (obj, toseek, value) => obj.itens.map(update(obj, toseek, value))
const remove = (obj, toseek) => (curr, index) => curr.nome === toseek ? obj.itens.splice(index, 1) : null
const update = (obj, toseek, value) => (curr, index) => curr.nome === toseek ? obj.itens.splice(index, 1, value) : null

Recuperar todos os valores do objeto

Tive uma situação em que precisei recuperar todos os valores de um objeto, mas esse objeto tinha vários níveis.

O que fiz então foi, criar uma recursividade, verificando cada nó do objeto, se o nó fosse o tipo Object, como quase tudo no JS é, a função é novamente chamada entrando em um loop até que todos os nós fossem processados e seus valores armazenados.

O reduce permite um valor inicial, então usando o concat e o valor inicial como um array vazio

@Woodsphreaker
Woodsphreaker / even_odd.js
Last active March 18, 2017 23:33
Show Me the Evens - Show me the Odds
const numbersGenerator = (numbers) => [].concat(...Array(numbers).keys());
const props = [
(num) => num % 2 === 0,
(num) => !props[0](num)
]
const result = (range, type) => numbersGenerator(range).filter(_a => props[type](_a))
console.log(result(20,0));
console.log(result(20,1));
// caso de uso
@anabastos
anabastos / criaArrayPrimos.js
Last active January 7, 2020 00:16
Dado um número aleatório, retornar todos os números PRIMOS entre 0 e o número escolhido
const criaArrayPrimos = x =>
criaArray(x)
.slice(2)
.filter(checaFatores)
const criaArray = tamanho => Array.from({ length: tamanho }, (el, index) => index)
const checaFatores = n =>
criaArray(maiorDivisor(n))
.slice(2)
@suissa
suissa / promises_reduce.js
Created March 13, 2017 19:33 — forked from anvk/promises_reduce.js
Sequential execution of Promises using reduce()
function asyncFunc(e) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(e), e * 1000);
});
}
const arr = [1, 2, 3];
let final = [];
function workMyCollection(arr) {
@lubien
lubien / desafio.js
Last active August 4, 2019 03:23
Dado o array de dictionary abaixo some o total dos valores (https://gist.github.com/anabastos/fbdfef7fcc64105e76e5e26218ebf7e6)
const
composeN = (...fs) => x =>
fs.reduceRight((x, f) => f(x), x)
, map = f => xs =>
xs.map(f)
, reduce = f => xs =>
xs.reduce(f)
@reborg
reborg / rich-already-answered-that.md
Last active August 5, 2025 03:46
A curated collection of answers that Rich gave throughout the history of Clojure

Rich Already Answered That!

A list of commonly asked questions, design decisions, reasons why Clojure is the way it is as they were answered directly by Rich (even when from many years ago, those answers are pretty much valid today!). Feel free to point friends and colleagues here next time they ask (again). Answers are pasted verbatim (I've made small adjustments for readibility, but never changed a sentence) from mailing lists, articles, chats.

How to use:

  • The link in the table of content jumps at the copy of the answer on this page.
  • The link on the answer itself points back at the original post.

Table of Content