Skip to content

Instantly share code, notes, and snippets.

@slugbyte
Last active May 9, 2017 06:51
Show Gist options
  • Save slugbyte/5d7a441e1c6926a6426f9bbdc0d1dc49 to your computer and use it in GitHub Desktop.
Save slugbyte/5d7a441e1c6926a6426f9bbdc0d1dc49 to your computer and use it in GitHub Desktop.
factory = (props, methods, proxy) => new Proxy(Object.assign(Object.create(methods), props), proxy)
DLL = (value, next=null) => {
let props = {value, next, prev: null}
let methods = {
map: function (cb) {
let result = DLL(null)
let current = this
let i=0
while(current){
result.last = cb(current.value, i++, this)
current = current.next
}
return result.next
},
}
let proxy = {
set: (obj, prop, value) => {
if(prop == 'next') {
let next = DLL(value)
next.prev = obj
obj.next = next
return
}
if(prop == 'last'){
let current = obj
while(current.next) {
current = current.next
}
if(current == obj) {
let next = DLL(value)
next.prev = obj
current.next = next
return
}
current.next = value
return
}
obj[prop] = value
}
}
return factory(props, methods, proxy)
}
head = DLL('first')
head.last = 'second'
head.last = 'third'
head.last = 'fourth'
doubled = head.map(v => v + '!!!!!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment