Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions core/collections.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ defstruct SetItem<K> :
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<?T,?S> (less?: (T, S) -> True|False,
Expand Down Expand Up @@ -229,11 +238,23 @@ public defn to-vector<T> (xs:Seqable<T>) -> Vector<T> :
add-all(v, xs)
v

public defn to-vector<?T> (xs:Seqable<?T>) -> Vector<T> :
to-vector<T>(xs)

public defn map<R,?T> (f: T -> R, v:Vector<?T>) -> Vector<R> :
val ret = Vector<R>(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 ==============================
Expand Down Expand Up @@ -272,6 +293,7 @@ public defn Queue<T> (initial-cap:Int) -> Queue<T> :
(begin + i) & (cap - 1)

new Queue<T> :
; 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)]
Expand Down Expand Up @@ -309,6 +331,25 @@ public defn Queue<T> (initial-cap:Int) -> Queue<T> :
public defn Queue<T> () -> Queue<T> :
Queue<T>(8)

public defn to-queue<T> (xs:Seqable<T>) -> Queue<T> :
val q = Queue<T>()
do(add{q, _}, xs)
q

public defn to-queue<?T> (xs:Seqable<?T>) -> Queue<T> :
to-queue<T>(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 ============================
;============================================================
Expand Down Expand Up @@ -797,11 +838,27 @@ public defn to-hashtable<K,V> (es:Seqable<KeyValue<K,V>>) -> HashTable<K,V> :
t[key(e)] = value(e)
t

public defn to-hashtable<?K,?V> (es:Seqable<KeyValue<?K,?V>>) -> HashTable<K,V> :
to-hashtable<K,V>(es)
Copy link
Copy Markdown
Contributor Author

@PhilippeFerreiraDeSousa PhilippeFerreiraDeSousa Aug 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to define those but i don't know how to print the types in the writer; "to-hashtable<String, Int>(...)" for example instead of "to-hashtable(...)"


public defn to-hashtable<K,V> (ks:Seqable<K>, vs:Seqable<V>) -> HashTable<K,V> :
val t = HashTable<K,V>()
set-all(t, ks, vs)
t

public defn to-hashtable<?K,?V> (ks:Seqable<?K>, vs:Seqable<?V>) -> HashTable<K,V> :
to-hashtable<K,V>(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 ===========================
;============================================================
Expand Down Expand Up @@ -1179,11 +1236,27 @@ public defn to-inttable<V> (es:Seqable<KeyValue<Int,V>>) -> IntTable<V> :
t[key(e)] = value(e)
t

public defn to-inttable<?V> (es:Seqable<KeyValue<Int,?V>>) -> IntTable<V> :
to-inttable<V>(es)

public defn to-inttable<V> (ks:Seqable<Int>, vs:Seqable<V>) -> IntTable<V> :
val t = IntTable<V>()
set-all(t, ks, vs)
t

public defn to-inttable<?V> (ks:Seqable<Int>, vs:Seqable<?V>) -> IntTable<V> :
to-inttable<V>(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 ==============================
;============================================================
Expand Down Expand Up @@ -1460,6 +1533,16 @@ public defn hashset-intersection<T> (a:Seqable<T&Equalable&Hashable>, b:Seqable<
val aset = to-hashset<T>(a)
to-hashset<T> $ 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 =============================
;============================================================
Expand Down Expand Up @@ -1686,3 +1769,13 @@ public defn to-intset (xs:Seqable<Int>) -> 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])
37 changes: 37 additions & 0 deletions core/core.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -3156,6 +3156,7 @@ public defn print-all (xs:Seqable) :
public defn println-all (xs:Seqable) :
println-all(CURRENT-OUTPUT-STREAM, xs)


;============================================================
;=================== Input Streams ==========================
;============================================================
Expand Down Expand Up @@ -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
; ================

Expand Down
24 changes: 24 additions & 0 deletions tests/test-collections-printers.stanza
Original file line number Diff line number Diff line change
@@ -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 $ \<S>
to-hashtable([
1 => "a"
2 => "b"])
<S>
val h-print = trim $ \<S>
HashTable(
1 => "a"
2 => "b")
<S>

#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