Skip to content

Instantly share code, notes, and snippets.

@themalkolm
Created February 11, 2015 12:13
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 themalkolm/ea277d47cf0b2292ff5a to your computer and use it in GitHub Desktop.
Save themalkolm/ea277d47cf0b2292ff5a to your computer and use it in GitHub Desktop.
@funcy.collecting
def unpack(*args):
d = args[-1]
for k in args[:-1]
yield d[k]
a, b = unpack('a', 'b', {'a': 1, 'b': 2, 'c': 3})
@themalkolm
Copy link
Author

a, b = [d[k] for k in ('a', 'b')]

@themalkolm
Copy link
Author

def unpack(*keys):
  @funcy.collecting
  def _unpack(d):
    for k in keys:
      yield d[k]
  return _unpack

a, b = unpack('a', 'b')({'a': 1, 'b': 2, 'c': 3})

@themalkolm
Copy link
Author

def unpack(*keys):
  def _unpack(d):
    return [d[k] for k in keys]
  return _unpack

a, b = unpack('a', 'b')({'a': 1, 'b': 2, 'c': 3})

@themalkolm
Copy link
Author

def unpack(*keys):
  return lambda d: [d[k] for k in keys]

a, b = unpack('a', 'b')({'a': 1, 'b': 2, 'c': 3})

@themalkolm
Copy link
Author

def dict_unpack(*keys):
    """Return dict unpacker for the provided keys in the exact order."""
    return lambda d: [d[k] for k in keys]

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