Skip to content

Instantly share code, notes, and snippets.

@snagy
Created December 4, 2019 23:27
Show Gist options
  • Save snagy/27a8ceed627b220793d43680912061a1 to your computer and use it in GitHub Desktop.
Save snagy/27a8ceed627b220793d43680912061a1 to your computer and use it in GitHub Desktop.
Scopes Closure problem
using import Array
let d2_test = """"1,9,10,3,2,3,11,0,99,30,40,50
fn parse_int (s) #parse an int, assuming nothings wrong because error checking is annoying
local v = 0
print "parsing" s
for c in s
print "char" c
if (c > (@ "9" 0))
break
elseif (c < (@ "0" 0))
break;
v = (* v 10) + c - (@ "0" 0)
print "parsed as" v
return v
fn parse_str_arr_with_closure (a d c) #parse an array of strings to a destination array with a closure
loop (i = 0)
if (i >= (countof a))
break i
'append d (c (@ a i))
repeat (i + 1)
fn split_str (str char) #split string str with char c as the token
global split_arr : (GrowingArray string)
local p = 0
loop (c = 0)
if (c >= (countof str))
'append split_arr (rslice str p)
break c
elseif ((@ str c) == char)
'append split_arr (slice str p c)
p = (c + 1)
repeat (c + 1)
return split_arr
fn print_arr (a)
for c in a
print c
let test_arr = (split_str d2_test (@ "," 0))
print "test set" (print_arr test_arr)
global d : (GrowingArray i32)
(parse_str_arr_with_closure test_arr d parse_int)
print "parsed test set" (print_arr d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment