Skip to content

Instantly share code, notes, and snippets.

@abiodun0
Last active May 28, 2017 22:39
Show Gist options
  • Select an option

  • Save abiodun0/4d46553565d2519881a03097e358b3ae to your computer and use it in GitHub Desktop.

Select an option

Save abiodun0/4d46553565d2519881a03097e358b3ae to your computer and use it in GitHub Desktop.
pairs [1,2,3,4,5] => [[1,2], [2,3], [3,4], [4,5]]
(partition 2 1 [1, 2, 3, 4, 5]) ;;=> ((1 2) (2 3) (3 4) (4 5))
pairs : List x -> List (x, x)
pairs ls =
case ls of
[] -> []
_ :: [] -> []
(a::b::xs) -> (a, b) :: pairs (b::xs)
function pairs (coll) {
if (coll.length < 2) {
return [];
}
const [first, second, ...rest] = coll;
return [[first, second]].concat(pairs([second, ...rest]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment