Created
January 9, 2015 15:00
-
-
Save rob-smallshire/8c31b4a302319389803d to your computer and use it in GitHub Desktop.
A Python 3.4 conj function for mutable and immutable sequences and strings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import singledispatch | |
from collections import Sequence, MutableSequence | |
@singledispatch | |
def conj(sequence, item): | |
raise TypeError("conj() not supported for type '{}'".format(type(sequence))) | |
@conj.register(MutableSequence) | |
def _(sequence, item): | |
sequence.append(item) | |
return sequence | |
@conj.register(Sequence) | |
def _(sequence, item): | |
return sequence + type(sequence)((item,)) | |
@conj.register(str) | |
@conj.register(bytes) | |
def _(sequence, item): | |
return sequence + item |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment