Skip to content

Commit

Permalink
Collection file updated for #14
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercerenies committed Jan 9, 2018
1 parent 4eb2929 commit 8d1f884
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions std/collection.lats
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Iterator end? := True.
Iterator next := { }.
Iterator element := Nil.
Iterator element= := {
err ReadOnlyError clone tap { self message := "Immutable iterator". } throw.
err ReadOnlyError clone tap {
self message := "Immutable iterator".
} throw.
}.

;; Collection implementations are expected to have `iterator`, as well as a sane `clone`.
Expand All @@ -22,29 +24,27 @@ Iterator element= := {
;; * `clone` - Iterators must be cloneable at any intermediate point in their iteration
;; () `toString` - Although not strictly required, it is recommended that this exist for convenience
global Collection ::= Mixin clone.
Collection interface := '[map!, visit, map, foldl, foldr, size, length, toArray, all, any, notall, notany,
detect, countIf, count, find, containsIf, contains, zip, zip!, take, maximum,
minimum, <>, sum, product, append].
Collection interface := '[map!, visit, map, foldl, foldr, size, length, toArray, all, any, notall,
notany, detect, countIf, count, find, containsIf, contains, zip, zip!,
take, maximum, minimum, <>, sum, product, append].
Collection map! := {
func0 := #'$1.
func := { #'func0 call. }.
iter := self iterator.
while { iter end? not. }
do {
iter element=: (func: iter element).
iter next.
}.
while { iter end? not. } do {
iter element=: (func: iter element).
iter next.
}.
self.
}.
Collection visit := {
func0 := #'$1.
func := { #'func0 call. }.
iter := self iterator.
while { iter end? not. }
do {
func: iter element.
iter next.
}.
while { iter end? not. } do {
func: iter element.
iter next.
}.
self.
}.
Collection map := {
Expand Down Expand Up @@ -145,9 +145,11 @@ Collection countIf := {
func0 := #'$1.
func := proc { #'func0 call. }.
self foldl: 0, {
if (func call #'$2)
then (#'$1 + 1)
else (#'$1).
if (func call #'$2) then {
#'$1 + 1.
} else {
#'$1
}.
}.
}.
Collection count := {
Expand Down Expand Up @@ -182,25 +184,24 @@ Collection zip := {
iter0 := self iterator.
iter1 := $1 iterator.
arr := Array clone.
while { (iter0 end? not) and (iter1 end? not). }
do {
arr pushBack: (cons: iter0 element, iter1 element).
iter0 next.
iter1 next.
}.
while { (iter0 end? not) and (iter1 end? not). } do {
arr pushBack: (cons: iter0 element, iter1 element).
iter0 next.
iter1 next.
}.
arr.
}.
Collection zip! := {
iter1 := $1 iterator.
self map! {
curr := $1.
if (iter1 end?)
then { cons: curr, Nil. }
else {
(cons: curr, iter1 element) tap {
iter1 next.
}.
if (iter1 end?) then {
cons: curr, Nil.
} else {
(cons: curr, iter1 element) tap {
iter1 next.
}.
}.
}.
}.
Collection take := {
Expand All @@ -213,13 +214,12 @@ Collection take := {
escapable.
parent self visit {
curr := #'$1.
if (remaining > 0)
then {
arr pushBack: #'curr.
decrement.
} else {
return: Nil.
}.
if (remaining > 0) then {
arr pushBack: #'curr.
decrement.
} else {
return: Nil.
}.
}.
}.
arr.
Expand Down

0 comments on commit 8d1f884

Please sign in to comment.