Skip to content

Instantly share code, notes, and snippets.

View kutyel's full-sized avatar
🌊
数学者に俺は成る!

Flavio Corpa kutyel

🌊
数学者に俺は成る!
View GitHub Profile
@kutyel
kutyel / curry.js
Created July 29, 2016 08:57
Some useful currying examples!
const get = prop => obj => obj[prop];
array.map(get('key'));
const match = req => obj => Object.keys(req).every(key => Object.keys(obj).includes(key) && obj[key] === req[key]);
array.filter(match({'key': value}));
@kutyel
kutyel / dateDiff.js
Created August 1, 2016 09:20
Diff between dates (in days)
(new Date(2016, 7, 17).getTime() - new Date(2016, 2, 17).getTime()) / 1000 / 3600 / 24
@kutyel
kutyel / fish_greeting.fish
Created August 4, 2016 07:51 — forked from dan-c-underwood/fish_greeting.fish
Custom fish greeting (for fish shell)
function fish_greeting
echo ' '(set_color F00)'___
___======____='(set_color FF7F00)'-'(set_color FF0)'-'(set_color FF7F00)'-='(set_color F00)')
/T \_'(set_color FF0)'--='(set_color FF7F00)'=='(set_color F00)') '(set_color red)(whoami)'@'(hostname)'
[ \ '(set_color FF7F00)'('(set_color FF0)'0'(set_color FF7F00)') '(set_color F00)'\~ \_'(set_color FF0)'-='(set_color FF7F00)'='(set_color F00)')'(set_color yellow)' Uptime:'(set_color white)(uptime | sed 's/.*up \([^,]*\), .*/\1/')(set_color red)'
\ / )J'(set_color FF7F00)'~~ \\'(set_color FF0)'-='(set_color F00)') Theme: '(set_color white)(echo $fish_theme)(set_color red)'
\\\\___/ )JJ'(set_color FF7F00)'~'(set_color FF0)'~~ '(set_color F00)'\) '(set_color yellow)'Version: '(set_color white)(echo $FISH_VERSION)(set_color red)'
\_____/JJJ'(set_color FF7F00)'~~'(set_color FF0)'~~ '(set_color F00)'\\
'(set_color FF7F00)'/ '(set_color FF0)'\ '(set_color FF0)', \\'(set_color F00)'J'(set_color
@kutyel
kutyel / slim-redux.js
Created August 4, 2016 08:31 — forked from gaearon/slim-redux.js
Redux without the sanity checks in a single file. Don't use this, use normal Redux. :-)
function mapValues(obj, fn) {
return Object.keys(obj).reduce((result, key) => {
result[key] = fn(obj[key], key);
return result;
}, {});
}
function pick(obj, fn) {
return Object.keys(obj).reduce((result, key) => {
if (fn(obj[key])) {
@kutyel
kutyel / cat.js
Created August 5, 2016 07:30
Outputs a cat to the console!
((f, t, p) => {
/**
* Returns cat's face
*/
const face = (type) => {
switch (type) {
case 0:
return '( x .x)';
case 1:
return '( o .o)';
@kutyel
kutyel / average.js
Created September 20, 2016 17:08
Average of numbers (JavaScript)
averages = n => n ? n.reduce((x,y,i,z) => i < z.length - 1 ? x.concat((y + z[i + 1] || 0) / 2) : x, []) : [];
{ shouldFlag: selectedIds => this.messages.filter(m => selectedIds.includes(m.id)).some(m => !m.flagged) }
{
shouldFlag: function(selectedIds) {
// Assume don't flag
var shouldFlagMessages = false;
// Keep track of how many are flagged and not flagged
var flaggedCount = 0;
var unFlaggedCount = 0;
// Get only the messages we care about
var messagesInQuestion = [];
const addNumers = (a, b) => a + b;
// Takes the values of an array and returns the total. Demonstrates simple
// recursion.
const totalForArray = (arr, currentTotal) => {
currentTotal = addNumers(currentTotal + arr.shift());
if (arr.length) {
return totalForArray(currentTotal, arr);
} else {
@kutyel
kutyel / sortFromArray.js
Last active October 31, 2016 18:35
Order an array given another array with its indices
const A = ["d", "a", "c", "b"];
const I = [3, 0, 2, 1];
A.reduce((x, y, i, arr) => x.concat(arr[I.indexOf(i)]), []);
// > ["a", "b", "c", "d"]
// Functional Programming Power!
const sortExternalFromArray = z => (x, y, i, arr) => x.concat(arr[z.indexOf(i)]);
// A more elegant/reusable approach
A.reduce(sortExternalFromArray(I), []);