Skip to content

Instantly share code, notes, and snippets.

@ykarikos
Last active December 18, 2015 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ykarikos/5776874 to your computer and use it in GitHub Desktop.
Save ykarikos/5776874 to your computer and use it in GitHub Desktop.
A function that returns function that accesses a dict
#
# Third iteration, now with reduce!
#
def getFieldAccessor(fields):
return lambda a: reduce(lambda x, y: x[y], fields, a)
@laitinen
Copy link

Maybe something like this: reduce((lambda x, y: x[y]), fields, x)?

reduce((lambda x,y: x[y]), [1], [1,2,3,4])
2
reduce((lambda x,y: x[y]), [1], [[1,2,3,4],[2,3,4]])
[2, 3, 4]
reduce((lambda x,y: x[y]), [1,2], [[1,2,3,4],[2,3,4]])
4
reduce((lambda x,y: x[y]), [], [[1,2,3,4],[2,3,4]])
[[1, 2, 3, 4], [2, 3, 4]]

@ykarikos
Copy link
Author

Thanks, that did the job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment