diff --git a/core/collections.stanza b/core/collections.stanza index 7d7568f80..4f6e88c0b 100644 --- a/core/collections.stanza +++ b/core/collections.stanza @@ -37,6 +37,15 @@ defstruct SetItem : defmethod print (o:OutputStream, x:SetItem) : print(o, "(%_) %_" % [hash(x), key(x)]) +defn indented-list (items: Seqable) : + new Printable : + defmethod print (o:OutputStream, this) : + val o2 = IndentedStream(o) + do(lnprint{o2, _}, items) + defmethod write (o:OutputStream, this) : + val o2 = IndentedStream(o) + do(lnwrite{o2, _}, items) + ;Binary search ;Returns n such that the first n numbers in xs < v defn bsearch (less?: (T, S) -> True|False, @@ -229,11 +238,23 @@ public defn to-vector (xs:Seqable) -> Vector : add-all(v, xs) v +public defn to-vector (xs:Seqable) -> Vector : + to-vector(xs) + public defn map (f: T -> R, v:Vector) -> Vector : val ret = Vector(length(v)) add-all(ret, seq(f, v)) ret +;================================== +;======== Printer / Writer ======== +;================================== + +public defmethod print (o: OutputStream, v: Vector) : + print(o, "Vector(%*)" % [v]) + +public defmethod write (o: OutputStream, v: Vector) : + print(o, "to-vector([%@])" % [v]) ;============================================================ ;====================== Queues ============================== @@ -272,6 +293,7 @@ public defn Queue (initial-cap:Int) -> Queue : (begin + i) & (cap - 1) new Queue : + ; Iteration over a queue is tail to head (Last in First iterated upon) instead of the conventional head to tail defmethod get (this, i:Int) : core/ensure-index-in-bounds(this, i) array[wrapped-index(i)] @@ -309,6 +331,25 @@ public defn Queue (initial-cap:Int) -> Queue : public defn Queue () -> Queue : Queue(8) +public defn to-queue (xs:Seqable) -> Queue : + val q = Queue() + do(add{q, _}, xs) + q + +public defn to-queue (xs:Seqable) -> Queue : + to-queue(xs) + +;================================== +;======== Printer / Writer ======== +;================================== + +public defmethod print (o: OutputStream, q: Queue) : + print(o, "Queue(%_)" % [string-join(in-reverse(q), " < ")]) + +public defmethod write (o: OutputStream, q: Queue) : + print(o, "to-queue([%@])" % [in-reverse(q)q]) + + ;============================================================ ;======================== Tables ============================ ;============================================================ @@ -797,11 +838,27 @@ public defn to-hashtable (es:Seqable>) -> HashTable : t[key(e)] = value(e) t +public defn to-hashtable (es:Seqable>) -> HashTable : + to-hashtable(es) + public defn to-hashtable (ks:Seqable, vs:Seqable) -> HashTable : val t = HashTable() set-all(t, ks, vs) t +public defn to-hashtable (ks:Seqable, vs:Seqable) -> HashTable : + to-hashtable(ks, vs) + +;================================== +;======== Printer / Writer ======== +;================================== + +public defmethod print (o: OutputStream, t: HashTable) : + print(o, "HashTable(%_)" % [indented-list(t)]) + +public defmethod write (o: OutputStream, t: HashTable) : + print(o, "to-hashtable([%~])" % [indented-list(t)]) + ;============================================================ ;===================== Int Tables =========================== ;============================================================ @@ -1179,11 +1236,27 @@ public defn to-inttable (es:Seqable>) -> IntTable : t[key(e)] = value(e) t +public defn to-inttable (es:Seqable>) -> IntTable : + to-inttable(es) + public defn to-inttable (ks:Seqable, vs:Seqable) -> IntTable : val t = IntTable() set-all(t, ks, vs) t +public defn to-inttable (ks:Seqable, vs:Seqable) -> IntTable : + to-inttable(ks, vs) + +;================================== +;======== Printer / Writer ======== +;================================== + +public defmethod print (o: OutputStream, t: IntTable) : + print(o, "IntTable(%_)" % [indented-list(t)]) + +public defmethod write (o: OutputStream, t: IntTable) : + print(o, "to-inttable([%~])" % [indented-list(t)]) + ;============================================================ ;======================== Sets ============================== ;============================================================ @@ -1460,6 +1533,16 @@ public defn hashset-intersection (a:Seqable, b:Seqable< val aset = to-hashset(a) to-hashset $ filter({aset[_]}, b) +;================================== +;======== Printer / Writer ======== +;================================== + +public defmethod print (o: OutputStream, s: HashSet) : + print(o, "HashSet(%*)" % [s]) + +public defmethod write (o: OutputStream, s: HashSet) : + print(o, "to-hashset([%@])" % [s]) + ;============================================================ ;====================== IntSets ============================= ;============================================================ @@ -1686,3 +1769,13 @@ public defn to-intset (xs:Seqable) -> IntSet : val s = IntSet() do(add{s, _}, xs) s + +;================================== +;======== Printer / Writer ======== +;================================== + +public defmethod print (o: OutputStream, s: IntSet) : + print(o, "IntSet(%*)" % [s]) + +public defmethod write (o: OutputStream, s: IntSet) : + print(o, "to-intset([%@])" % [s]) diff --git a/core/core.stanza b/core/core.stanza index 3549d0a48..b522f5e43 100644 --- a/core/core.stanza +++ b/core/core.stanza @@ -3156,6 +3156,7 @@ public defn print-all (xs:Seqable) : public defn println-all (xs:Seqable) : println-all(CURRENT-OUTPUT-STREAM, xs) + ;============================================================ ;=================== Input Streams ========================== ;============================================================ @@ -7917,6 +7918,42 @@ public defn write-all (o:OutputStream, xs:Seqable) -> False : print(o, " ") write(o, next(xs-seq)) +; Convenience Functions +; ===================== + + +public defn writeln (o:OutputStream, x) : + write(o, x) + print(o, NL) + +public defn lnwrite (o:OutputStream, x) : + print(o, NL) + write(o, x) + +public defn writeln-all (o:OutputStream, xs:Seqable) : + write-all(o, xs) + print(o, NL) + + +; Write to Current Output Stream +; ============================== + +public defn write (x) : + write(CURRENT-OUTPUT-STREAM, x) + +public defn write-all (xs:Seqable) : + write-all(CURRENT-OUTPUT-STREAM, xs) + +public defn writeln (x) : + writeln(CURRENT-OUTPUT-STREAM, x) + +public defn lnwrite (x) : + lnwrite(CURRENT-OUTPUT-STREAM, x) + +public defn writeln-all (xs:Seqable) : + writeln-all(CURRENT-OUTPUT-STREAM, xs) + + ; Escape Sequences ; ================ diff --git a/tests/test-collections-printers.stanza b/tests/test-collections-printers.stanza new file mode 100644 index 000000000..72d5d2fb2 --- /dev/null +++ b/tests/test-collections-printers.stanza @@ -0,0 +1,24 @@ +#use-added-syntax(tests) +defpackage test-collections-printers : + import core + import collections + + +deftest test-hashtable : + val h = to-hashtable([1 => "a" 2 => "b"]) + val h-write = trim $ \ +to-hashtable([ + 1 => "a" + 2 => "b"]) + + val h-print = trim $ \ +HashTable( + 1 => "a" + 2 => "b") + + + #ASSERT(to-string("%~" % [h]) == h-write) + #ASSERT(to-string(h) == h-print) + +; TODO: inttable, hashset, intset, vector, queue, big nested example of the previous elements +; TODO: basic writeln, lnwrite... calls