diff --git a/samples/std.ink b/samples/std.ink index 10f8cdd..d6e0782 100644 --- a/samples/std.ink +++ b/samples/std.ink @@ -19,12 +19,10 @@ hToN := {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 'a': 10, 'b nToH := '0123456789abcdef' ` take number, return hex string ` -hex := n => ( - (sub := (p, acc) => p < 16 :: { - true -> nToH.(p) + acc - false -> sub(floor(p / 16), nToH.(p % 16) + acc) - })(floor(n), '') -) +hex := n => (sub := (p, acc) => p < 16 :: { + true -> nToH.(p) + acc + false -> sub(floor(p / 16), nToH.(p % 16) + acc) +})(floor(n), '') ` take hex string, return number ` xeh := s => ( @@ -67,7 +65,7 @@ range := (start, end, step) => ( ) ` clamp start and end numbers to ranges, such that - start < end. Utility used in slice/sliceList` + start < end. Utility used in slice ` clamp := (start, end, min, max) => ( start := (start < min :: { true -> min @@ -92,32 +90,20 @@ clamp := (start, end, min, max) => ( } ) -` get a substring of a given string ` -slice := (str, start, end) => ( - ` bounds checks ` - x := clamp(start, end, 0, len(str)) - start := x.start - end := x.end - - max := end - start - (sub := (i, acc) => i :: { - max -> acc - _ -> sub(i + 1, acc.(i) := str.(start + i)) - })(0, '') -) - -` get a sub-list of a given list ` -sliceList := (list, start, end) => ( +` get a substring of a given string, or sublist of a given list ` +slice := (s, start, end) => ( ` bounds checks ` - x := clamp(start, end, 0, len(list)) + x := clamp(start, end, 0, len(s)) start := x.start - end := x.end + max := x.end - start - max := end - start (sub := (i, acc) => i :: { max -> acc - _ -> sub(i + 1, acc.(i) := list.(start + i)) - })(0, []) + _ -> sub(i + 1, acc.(i) := s.(start + i)) + })(0, type(s) :: { + 'string' -> '' + 'composite' -> [] + }) ) ` join one list to the end of another, return the original first list ` @@ -191,19 +177,7 @@ reduceBack := (list, f, acc) => ( ) ` flatten by depth 1 ` -flatten := list => ( - max := len(list) - (sub := (i, count, acc) => i :: { - max -> acc - _ -> ( - cur := list.(i) - each(cur, (item, idx) => ( - acc.(count + idx) := item - )) - sub(i + 1, count + len(cur), acc) - ) - })(0, 0, []) -) +flatten := list => reduce(list, join, []) ` true iff some items in list are true ` some := list => reduce(list, (acc, x) => acc | x, false) diff --git a/samples/test.ink b/samples/test.ink index d95e73e..17a0a68 100755 --- a/samples/test.ink +++ b/samples/test.ink @@ -558,43 +558,42 @@ m('type() builtin function') m('std.range/slice/append/join/cat and stringList') ( stringList := std.stringList - sliceList := std.sliceList range := std.range reverse := std.reverse slice := std.slice join := std.join cat := std.cat - ` slice/sliceList returns copies ` + ` slice returns copies ` ( st := '12345' li := [1, 2, 3, 4, 5] stc := slice(st, 0, len(st)) - lic := sliceList(li, 0, len(li)) + lic := slice(li, 0, len(li)) stc.2 := 'x' lic.2 := 'x' t('slice(string) should make a copy', st, '12345') t('slice(string) should return a copy', stc, '12x45') - t('sliceList(list) should make a copy', li, [1, 2, 3, 4, 5]) - t('sliceList(list) should return a copy', lic, [1, 2, 'x', 4, 5]) + t('slice(list) should make a copy', li, [1, 2, 3, 4, 5]) + t('slice(list) should return a copy', lic, [1, 2, 'x', 4, 5]) ) - sl := (l, s, e) => stringList(sliceList(l, s, e)) + sl := (l, s, e) => stringList(slice(l, s, e)) list := range(10, ~1, ~1) str := 'abracadabra' - t('sliceList(list)', sl(list, 0, 5), '[10, 9, 8, 7, 6]') - t('sliceList with OOB lower bound', sl(list, ~5, 2), '[10, 9]') - t('sliceList with OOB upper bound', sl(list, 7, 20), '[3, 2, 1, 0]') - t('sliceList with OOB both bounds', sl(list, 20, 1), '[]') + t('slice(list)', sl(list, 0, 5), '[10, 9, 8, 7, 6]') + t('slice with OOB lower bound', sl(list, ~5, 2), '[10, 9]') + t('slice with OOB upper bound', sl(list, 7, 20), '[3, 2, 1, 0]') + t('slice with OOB both bounds', sl(list, 20, 1), '[]') ` redefine list using range and reverse, to t those ` list := reverse(range(0, 11, 1)) t('join() homogeneous lists', stringList(join( - sliceList(list, 0, 5), sliceList(list, 5, 200) + slice(list, 0, 5), slice(list, 5, 200) )), '[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]') t('join() heterogeneous lists', stringList(join( [1, 2, 3] diff --git a/samples/tictactoe.ink b/samples/tictactoe.ink index c04554f..523f0bf 100644 --- a/samples/tictactoe.ink +++ b/samples/tictactoe.ink @@ -5,7 +5,7 @@ std := load('std') log := std.log scan := std.scan f := std.format -sliceList := std.sliceList +slice := std.slice map := std.map reduce := std.reduce filter := std.filter @@ -88,7 +88,7 @@ checkBoard := bd => ( true -> Result.O _ -> ( ` check if game ended in a tie ` - takenCells := filter(sliceList(bd, 1, 10), val => ~(val = 0)) + takenCells := filter(slice(bd, 1, 10), val => ~(val = 0)) len(takenCells) :: { 9 -> Result.Tie _ -> Result.None