diff --git a/prelude.ld b/prelude.ld index 05938a1..726c706 100644 --- a/prelude.ld +++ b/prelude.ld @@ -18,7 +18,7 @@ fn join fn neg? fn atan/2 fn mod -fn assoc? +fn assoc & ? fn dict fn get fn unbox @@ -370,14 +370,26 @@ fn ws? { (_) -> false } +fn strip { + "Removes punctuation from a string, removing all instances of ,.;:?!" + ("{x},{y}") -> strip ("{x}{y}") + ("{x}.{y}") -> strip ("{x}{y}") + ("{x};{y}") -> strip ("{x}{y}") + ("{x}:{y}") -> strip ("{x}{y}") + ("{x}?{y}") -> strip ("{x}{y}") + ("{x}!{y}") -> strip ("{x}{y}") + (x) -> x +} + fn words { "Takes a string and returns a list of the words in the string. Strips all whitespace." (str as :string) -> { - let raw_strs = split (str, " ") - fn joiner (list, str) -> if eq? (str, "") + let no_punct = strip (str) + let strs = split (no_punct, " ") + fn worder (list, str) -> if empty? (str) then list else append (list, str) - fold (joiner, raw_strs, []) + fold (worder, strs, []) } } @@ -502,6 +514,11 @@ fn inv/0 { (x as :number) -> div/0 (1, x) } +fn inv/safe { + "Returns the inverse of a number: 1/n or `div/safe (1, n)`. Returns a result tuple." + (x as :number) -> div/safe (1, x) +} + fn abs { "Returns the absolute value of a number." (0) -> 0 @@ -620,16 +637,13 @@ fn max { & additional list operations now that we have comparitors fn at { - "Returns the element at index n of a list or tuple. Zero-indexed: the first element is at index 0." - (xs as :list, n as :number) -> when { - neg? (n) -> nil - gte? (n, count (xs)) -> nil - true -> base :nth (n, xs) - } - (xs as :tuple, n as :number) -> when { - neg? (n) -> nil - gte? (n, count (xs)) -> nil - true -> base :nth (n, xs) + "Returns the element at index n of a list or tuple, or the byte at index n of a string. Zero-indexed: the first element is at index 0. Returns nil if nothing is found in a list or tuple; returns an empty string if nothing is found in a string." + (xs as :list, n as :number) -> base :nth (xs, n) + (xs as :tuple, n as :number) -> base :nth (n, xs) + (str as :string, n as :number) -> when { + neg? (n) -> "" + gte? (n, count (str)) -> "" + true -> base :slice (str, n, inc (n)) } (_) -> nil } @@ -741,21 +755,17 @@ fn update { } fn keys { - "Takes an associative collection and returns a list of keys in that collection. Returns an empty list on anything other than a collection." - (coll) -> if not (assoc? (coll)) - then [] - else do coll > list > map (first, _) + "Takes a dict and returns a list of keys in that dict." + (dict as :dict) -> do dict > list > map (first, _) } fn values { - "Takes an associative collection and returns a list of values in that collection. Returns an empty list on anything other than a collection." - (coll) -> if not (assoc? (coll)) - then [] - else do coll > list > map (second, _) + "Takes a dict and returns a list of values in that dict." + (dict) -> do dict > list > map (second, _) } fn diff { - "Takes two associate data structures and returns a dict describing their differences. Does this shallowly, offering diffs only for keys in the original dict." + "Takes two dicts and returns a dict describing their differences. Does this shallowly, offering diffs only for keys in the original dict." (d1 as :dict, d2 as :dict) -> { let key1 = keys (d1) let key2 = keys (d2) @@ -782,7 +792,7 @@ fn diff { } fn coll? { - "Returns true if a value is a collection: dict, struct, list, tuple, or set." + "Returns true if a value is a collection: dict, list, pkg, tuple, or set." (coll as :dict) -> true (coll as :list) -> true (coll as :tuple) -> true @@ -799,7 +809,7 @@ fn ordered? { } fn assoc? { - "Returns true if a value is an associative collection: a dict, struct, or namespace." + "Returns true if a value is an associative collection: a dict or a pkg." (assoc as :dict) -> true (assoc as :pkg) -> true (_) -> false @@ -807,10 +817,10 @@ fn assoc? { & TODO: consider merging `get` and `at` fn get { - "Takes a dict or struct, key, and optional default value; returns the value at key. If the value is not found, returns nil or the default value. Returns nil or default if the first argument is not a dict or struct." + "Takes a key, dict, and optional default value; returns the value at key. If the value is not found, returns nil or the default value." (key as :keyword) -> get (key, _) - (key as :keyword, coll) -> base :get (key, coll) - (key as :keyword, coll, default) -> base :get (key, coll, default) + (key as :keyword, dict as :dict) -> get (key, dict) + (key as :keyword, dict as :dict, default) -> base :get (key, dict, default) } & TODO: add sets to this? @@ -1338,159 +1348,164 @@ fn penwidth { box state = nil pkg Prelude { - abs - add - and - angle - any? - append - assert! - assoc - assoc? - at - atan/2 - back! - background! - between? - bg! - bgcolor - bk! - bool - bool? - box? - butlast - ceil - clear! - coll? - colors - concat - cos - count - dec - deg/rad - deg/turn - dict - dict? - diff - dissoc - dist - div - div/0 - div/safe - doc! - downcase - each! - empty? - eq? - err - err? - even? - false? - fd! - filter - first - floor - fn? - fold - forward! - get - goto! - gt? - gte? - heading - heading/vector - home! - inc - inv - inv/0 - join - keep - keys - keyword? - last - left! - list - load_turtle_state! - lt! - lt? - lte? - map - max - min - mod - mult - neg - neg? - neq? - nil? - not - odd? - ok - ok? - or - ordered? - p5_calls - pc! - pd! - pencolor - pencolor! - pendown! - pendown? - penup! - penwidth - penwidth! - pi - pos? - position - print! - prn! - pu! - pw! - rad/deg - rad/turn - random - random_int - range - render_turtle! - report! - reset_turtle! - rest - right! - round - rt! - second - set - set? - show - sin - slice - some - some? - split - square - state - store! - string - string? - sub - sum_of_squares - tan - tau - trim - tuple? - turn/deg - turn/rad - turtle_commands - turtle_state - turtle_states - type - unbox - unwrap! - unwrap_or - upcase - update - update! - values - words - zero? + abs & math + add & math + and & bool + angle & math + any? & dicts lists strings sets tuples + append & lists sets + assert! & errors + assoc & dicts + assoc? & dicts + at & lists strings + atan/2 & math + back! & turtles + background! & turtles + between? & math + bg! & turtles + bgcolor & turtles + bk! & turtles + bool & bool + bool? & bool + box? & boxes + butlast & lists strings tuples + ceil & math + clear! & turtles + coll? & dicts lists sets tuples + colors & turtles + concat & string list set + cos & math + count & string list set tuple dict + dec & math + deg/rad & math + deg/turn & math + dict & dict + dict? & dict + diff & dict + dissoc & dict + dist & math + div & math + div/0 & math + div/safe & math + doc! & env + downcase & string + each! & list + empty? & list dict set string tuple + eq? & values + err & result + err? & result + even? & math + false? & bool + fd! & turtles + filter & list + first & list tuple + floor & math + fn? & functions + fold & lists + forward! & turtles + get & dicts + goto! & turtles + gt? & math + gte? & math + heading & turtles + heading/vector & math + home! & turtles + inc & math + inv & math + inv/0 & math + inv/safe & math + join & lists strings + keep & lists + keys & dicts + keyword? & keywords + last & lists tuples + left! & turtles + list & lists + list? & lists + load_turtle_state! & turtles + lt! & turtles + lt? & math + lte? & math + map & lists + max & math + min & math + mod & math + mult & math + neg & math + neg? & math + neq? & values + nil? & nil + not & bool + odd? & math + ok & results + ok? & results + or & bool + ordered? & lists tuples strings + p5_calls & turtles + pc! & turtles + pd! & turtles + pencolor & turtles + pencolor! & turtles + pendown! & turtles + pendown? & turtles + penup! & turtles + penwidth & turtles + penwidth! & turtles + pi & math + pos? & math + position & turtles + print! & environment + & prn! & environment + pu! & turtles + pw! & turtles + rad/deg & math + rad/turn & math + random & math dicts lists tuples sets + random_int & math + range & math lists + render_turtle! & turtles + report! & environment + reset_turtle! & turtles + rest & lists tuples + right! & turtles + round & math + rt! & turtles + second & lists tuples + sentence & lists strings + set & sets + set? & sets + show & strings + sin & math + slice & lists tuples strings + some & values + some? & values + split & strings + square & math + state & environment + store! & boxes + string & strings + string? & strings + strip & strings + sub & math + sum_of_squares & math + tan & math + tau & math + trim & strings + tuple? & tuples + turn/deg & math + turn/rad & math + turtle_commands & turtles + turtle_state & turtles + turtle_states & turtles + type & values + unbox & boxes + unwrap! & results + unwrap_or & results + upcase & strings + update & dicts + update! & boxes + values & dicts + words & strings lists + ws? & strings + zero? & math }