add chars
to prelude
This commit is contained in:
parent
32b42e0242
commit
5c32d32f24
14
prelude.ld
14
prelude.ld
|
@ -386,6 +386,19 @@ fn downcase {
|
||||||
(str as :string) -> base :downcase (str)
|
(str as :string) -> base :downcase (str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn chars {
|
||||||
|
"Takes a string and returns its characters as a list. Works only for strings with only ascii characters. Panics on any non-ascii characters."
|
||||||
|
(str as :string) -> match base :chars (str) {
|
||||||
|
(:ok, chrs) -> chrs
|
||||||
|
(:err, msg) -> panic! msg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn chars/safe {
|
||||||
|
"Takes a string and returns its characters as a list, wrapped in a result tuple. Works only for strings with only ascii characters. Returns an error tuple on any non-ascii characters."
|
||||||
|
(str as :string) -> base :chars (str)
|
||||||
|
}
|
||||||
|
|
||||||
fn ws? {
|
fn ws? {
|
||||||
"Tells if a string is a whitespace character."
|
"Tells if a string is a whitespace character."
|
||||||
(" ") -> true
|
(" ") -> true
|
||||||
|
@ -1386,6 +1399,7 @@ pkg Prelude {
|
||||||
box? & boxes
|
box? & boxes
|
||||||
butlast & lists strings tuples
|
butlast & lists strings tuples
|
||||||
ceil & math
|
ceil & math
|
||||||
|
chars & strings
|
||||||
clear! & turtles
|
clear! & turtles
|
||||||
coll? & dicts lists sets tuples
|
coll? & dicts lists sets tuples
|
||||||
colors & turtles
|
colors & turtles
|
||||||
|
|
|
@ -235,6 +235,19 @@
|
||||||
(defn mod [x y]
|
(defn mod [x y]
|
||||||
(% x y))
|
(% x y))
|
||||||
|
|
||||||
|
(defn- byte->ascii [c i]
|
||||||
|
(if (< c 128)
|
||||||
|
(string/from-bytes c)
|
||||||
|
(error (string "non-ASCII character at index" i))))
|
||||||
|
|
||||||
|
(defn chars [str]
|
||||||
|
(def out @[])
|
||||||
|
(try
|
||||||
|
(for i 0 (length str)
|
||||||
|
(array/push out (byte->ascii (str i) i)))
|
||||||
|
([e] (break [:err e])))
|
||||||
|
[:ok out])
|
||||||
|
|
||||||
(def ctx {
|
(def ctx {
|
||||||
"add" +
|
"add" +
|
||||||
"and" ludus/and
|
"and" ludus/and
|
||||||
|
@ -243,6 +256,7 @@
|
||||||
"atan_2" math/atan2
|
"atan_2" math/atan2
|
||||||
"bool" bool
|
"bool" bool
|
||||||
"ceil" math/ceil
|
"ceil" math/ceil
|
||||||
|
"chars" chars
|
||||||
"concat" concat
|
"concat" concat
|
||||||
"conj!" conj!
|
"conj!" conj!
|
||||||
"conj" conj
|
"conj" conj
|
||||||
|
@ -298,10 +312,9 @@
|
||||||
"upcase" string/ascii-upper
|
"upcase" string/ascii-upper
|
||||||
})
|
})
|
||||||
|
|
||||||
(def base (let [b @{}]
|
(def base (let [b @{:^type :dict}]
|
||||||
(each [k v] (pairs ctx)
|
(each [k v] (pairs ctx)
|
||||||
(set (b (keyword k)) v))
|
(set (b (keyword k)) v))
|
||||||
b))
|
b))
|
||||||
|
|
||||||
(set (base :^type) :dict)
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user