Skip to content

Instantly share code, notes, and snippets.

@Villane
Created January 26, 2012 02:04
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 Villane/1680464 to your computer and use it in GitHub Desktop.
Save Villane/1680464 to your computer and use it in GitHub Desktop.
foldLeft for int arrays in Slang (there are no generics yet) (syntax highlight set to Scala because it's most similar)
def foldLeft(z: Int, a: [Int], f: (Int, Int) -> Int) =
loop(i := 0, acc := z) while i < #a do
(i + 1, f(acc, aᵢ))
yield acc
def ∑(a: [Int]) = foldLeft(0, a, (x, y) -> x + y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment