Skip to content

Commit

Permalink
Clean up stdlib, make slice() polymorphic and deprecate sliceList
Browse files Browse the repository at this point in the history
  • Loading branch information
thesephist committed Mar 5, 2020
1 parent a9412b6 commit 5e6b788
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 54 deletions.
56 changes: 15 additions & 41 deletions samples/std.ink
Original file line number Diff line number Diff line change
Expand Up @@ -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 => (
Expand Down Expand Up @@ -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
Expand All @@ -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 `
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 10 additions & 11 deletions samples/test.ink
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions samples/tictactoe.ink
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5e6b788

Please sign in to comment.