Skip to content

Instantly share code, notes, and snippets.

@tapi
Created June 4, 2014 05:41
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 tapi/c88175a020a232eb8a54 to your computer and use it in GitHub Desktop.
Save tapi/c88175a020a232eb8a54 to your computer and use it in GitHub Desktop.
Generic types broken into separate lines
func anyCommonElements <T, U where T: Sequence, U: Sequence, T.GeneratorType.Element: Equatable, T.GeneratorType.Element == U.GeneratorType.Element> (lhs: T, rhs: U) -> Bool {
for lhsItem in lhs {
for rhsItem in rhs {
if lhsItem == rhsItem {
return true
}
}
}
return false
}
anyCommonElements([1, 2, 3], [3])
func anyCommonElements
<T, U where T: Sequence, U: Sequence, T.GeneratorType.Element: Equatable, T.GeneratorType.Element == U.GeneratorType.Element>
(lhs: T, rhs: U) -> Bool
{
for lhsItem in lhs {
for rhsItem in rhs {
if lhsItem == rhsItem {
return true
}
}
}
return false
}
anyCommonElements([1, 2, 3], [3])
@tapi
Copy link
Author

tapi commented Jun 4, 2014

Because the type declarations can get pretty hairy I was wondering what people thought about breaking them up. It allows you to easily scan the function name, the types being used, the parameters that can be passed all more or less at a glance.

@angusiguess
Copy link

If you wanted to go buck wild you could decompose it even further to roughly one constraint per line (assuming the whitespace doesn't make swift sad)

func anyCommonElements
    <T, U where
       T: Sequence, U: Sequence, 
       T.GeneratorType.Element: Equatable, 
       T.GeneratorType.Element == U.GeneratorType.Element>
    (lhs: T, rhs: U) -> Bool 
    {
    for lhsItem in lhs {
        for rhsItem in rhs {
            if lhsItem == rhsItem {
                return true
            }
        }
    }
    return false
}

@tapi
Copy link
Author

tapi commented Dec 3, 2015

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