Skip to content

Instantly share code, notes, and snippets.

@leogdion
Last active March 17, 2020 21:00
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 leogdion/ca917ddb5ade32ee4200475cd743b7bf to your computer and use it in GitHub Desktop.
Save leogdion/ca917ddb5ade32ee4200475cd743b7bf to your computer and use it in GitHub Desktop.
"Multiply" and Modify Sequences
func product<Multiplier, Factor>(
_ sequence: [Factor],
_ other: [Multiplier]
) -> [(Factor, Multiplier)] {
return other.flatMap { value in
sequence.map { element in
(element, value)
}
}
}
func product<Multiplier, Factor, Product>(
_ sequence: [Factor],
_ other: [Multiplier],
using closure: (Factor, Multiplier) -> Product
) -> [Product] {
product(sequence, other).map(closure)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment