Skip to content

Instantly share code, notes, and snippets.

@passcod
Created February 28, 2013 11:19
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 passcod/5056048 to your computer and use it in GitHub Desktop.
Save passcod/5056048 to your computer and use it in GitHub Desktop.
Seq
seq = (a, b = a) ->
if typeof a == "number" and (a == b or typeof b == "number")
if a != b
"(#{a}..#{b})"
else
"(0..#{a})"
else if typeof a != "string" or !rgx.test a or a == ""
"(0..1)"
else
[ok, start, end] = a.match rgx
end = (+end) + 1
"(#{start}..#{end})"
com = (s) ->
if rgx.test s
[ok, start, end] = s.match rgx
start = +start
end = +end
(end - start + 1) * (start + end) / 2
>>> seq()
"(0..1)"
>>> seq(seq())
"(0..2)"
>>> seq(seq(seq(2, 3)))
"(2..5)"
>>> com(seq(15));
120
>>> com(seq(seq(15)));
136
>>> com(seq(seq(15, 33)));
490
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment