Skip to content

Instantly share code, notes, and snippets.

@chattes
Last active February 2, 2021 02:54
Show Gist options
  • Save chattes/fe8b27337b4dfe7f0fd60da43c9bad41 to your computer and use it in GitHub Desktop.
Save chattes/fe8b27337b4dfe7f0fd60da43c9bad41 to your computer and use it in GitHub Desktop.
// Array.prototype = {
// left: num => x.slice(num),
// divideBy: x => x.map(y => y / num),
// value: x => x
// }
let t1 = [
{name: 'Alice', age: 20},
{name: 'Bob', age: 30},
{name: 'Harry', age: 40},
{name: 'Jerry', age: 50}
]
function Utils(x) {
if(!(this instanceof Utils)){
console.log("Here")
return new Utils(x)
}
this.value = Array.isArray(x) ? x : [x]
return this
}
Utils.prototype.map = function(f){
this.value = this.value.map(y => f(y))
return this
}
Utils.prototype.take = function(n){
this.value = this.value.slice(0, n)
return this
}
Utils.prototype.divideBy = function(n){
this.value = this.value.map(y => y / n)
return this
}
Utils.prototype.fold = function(){
return this.value
}
let res = Utils(t1).map(y => y.age).take(3).divideBy(10).fold()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment