Skip to content

Instantly share code, notes, and snippets.

View saginadir's full-sized avatar
:octocat:

Sagi saginadir

:octocat:
View GitHub Profile
@saginadir
saginadir / myFunction.js
Last active February 17, 2017 11:09
regular JS function
function updateUserNameSaveToDb(user, newName, db) {
user.name = newName.charAt(0).toUpperCase() + newName.slice(1).toLowerCase();
db.save(user);
$('.user-greet').html(`Hello ${user.name}`);
}
@saginadir
saginadir / sigma.js
Created February 7, 2017 07:23
sum function
// https://jsbin.com/guhumaq/4/edit?html,js,console
// -- Helpers ------------------- //
const reduce = (fn, arr) => arr.reduce(fn, 0)
const add = (x, y) => x + y;
// -- Sum Func ------------------ //
@saginadir
saginadir / gist.js
Last active January 17, 2017 07:52
const pipe = (fn,...fns) => (...args) => !fns.length ? fn(...args) : pipe(...fns)(fn(...args));
const getKittens =
cats => cats.filter(cat => cat.months < 7);
const namesToList =
listOfObjects => listOfObjects.map(ob => ob.name);
const getKittenNames = pipe(getKittens, namesToList);
class Student {
constructor (student) {
this.student = student;
}
map(f) {
const student = Object.assign({}, this.student);
return new Student(f(student))
}
const student = newStudent('Josh');
student
.logMyState()
.giveHomework(2)
.doHomework() // "Missing pencil to do homework"
.take('ruler')
.take('coffee')
.logMyState()
.doHomework() // "Missing pencil to do homework"
const wrap = (student) => ({
take: (item) => {
student.items.push(item);
return wrap(student);
},
giveHomework: (homeworkQuantity) => {
student.homeworkQuantity += homeworkQuantity;
return wrap(student);
},
doHomework: () => {
const wrap = (el) => ({
css: (attribute, value) => {
el.style[attribute] = value;
return wrap(el);
},
hide: () => {
return wrap(el).css('display', 'none');
},
show: () => {
return wrap(el).css('display', 'block');
const toArray = (...args) => args; // turns all args to array.
const res1 = toArray(1, 'f', 'Apple', {hello: 'world'})[2].toUpperCase();
console.log(res1); // Will outut APPLE;
const res2 = toArray(1, 'f', 'Apple', {hello: 'world'})[2].toUpperCase().substr(0,3);
console.log(res2); // Will outut APP;
const toArray = (...args) => args; // turns all args to array.
// [2] position hold 'Apple', allowing us to directly access it after the function.
toArray(1, 'f', 'Apple', {hello: 'world'})[2];
const add = (a, b) => a + b;
console.log(add(3, 4)); // will output 7
add(1, 2) + add(3 + 3); // Add the result of 2 functions together