def pair(x, y): """Return a function that represents a pair.""" def get(index): if index == 0: return x elif index == 1: return y return get def make_mutable_pair(key, value): """Return a functional implementation of a mutable pair.""" records = [] def get_item(key): for k, v in records: if k == key: return v def set_item(key, value): for item in records: if item[0] == key: item[1] = value return records.append([key, value]) def dispatch(message, key=None, value=None): if message == "get_item": return get_item(key) elif message == "set_item": set_item(key, value) return dispatch p = make_mutable_pair(1,2) p(‘get_item’, 0)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question