Skip to content

Instantly share code, notes, and snippets.

@saginadir
Last active January 13, 2017 12:40
Show Gist options
  • Save saginadir/dd2aee798f2f34dd3663bb53a7027486 to your computer and use it in GitHub Desktop.
Save saginadir/dd2aee798f2f34dd3663bb53a7027486 to your computer and use it in GitHub Desktop.
const wrap = (student) => ({
take: (item) => {
student.items.push(item);
return wrap(student);
},
giveHomework: (homeworkQuantity) => {
student.homeworkQuantity += homeworkQuantity;
return wrap(student);
},
doHomework: () => {
const doneHomework = student.items.reduce((done, item) => {
if(item === 'pencil') {
student.homeworkQuantity -= 1;
return true;
}
return doneHomework;
}, false)
if(!doneHomework) console.log('Missing pencil to do homework');
return wrap(student);
},
logMyState: () => {
console.log(student);
return wrap(student);
}
});
const newStudent = (name) => wrap({
name,
items: [],
homeworkQuantity: 0,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment