Skip to content

Commit

Permalink
More #14 style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercerenies committed Jan 9, 2018
1 parent 9fdc7a3 commit 4eb2929
Showing 1 changed file with 77 additions and 55 deletions.
132 changes: 77 additions & 55 deletions std/array.lats
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ Cons pretty := {
chain := {
curr := $1.
cond {
when (curr cdr nil?)
do { curr car toString. }.
when (curr cdr is (Cons))
do { (curr car toString) ++ " " ++ (chain: curr cdr). }.
else { (curr car toString) ++ " . " ++ (curr cdr toString). }.
when (curr cdr nil?) do {
curr car toString.
}.
when (curr cdr is (Cons)) do {
(curr car toString) ++ " " ++ (chain: curr cdr).
}.
else {
(curr car toString) ++ " . " ++ (curr cdr toString).
}.
}.
}.
"(" ++ (chain: self) ++ ")".
Expand All @@ -35,9 +39,11 @@ Array lowerBound := 0.
Array upperBound := 0.
Array mapping := {
var := $1.
if { var >= 0. }
then { (var * 2) + 1. }
else { var * -2. }.
if { var >= 0. } then {
(var * 2) + 1.
} else {
var * -2.
}.
}.
Array empty? := { (self lowerBound) >= (self upperBound). }.
Array pushFront := {
Expand All @@ -53,82 +59,98 @@ Array pushBack := {
#'value.
}.
Array popFront := {
if (self empty?)
then { err BoundsError clone throw. }
else {
parent self lowerBound := parent self lowerBound + 1.
parent self slot: (parent self mapping: (parent self lowerBound - 1)) ordinal.
}.
if (self empty?) then {
err BoundsError clone throw.
} else {
parent self lowerBound := parent self lowerBound + 1.
parent self slot: (parent self mapping: (parent self lowerBound - 1)) ordinal.
}.
}.
Array popBack := {
if (self empty?)
then { err BoundsError clone throw. }
else {
parent self upperBound := parent self upperBound - 1.
parent self slot: (parent self mapping: parent self upperBound) ordinal.
}.
if (self empty?) then {
err BoundsError clone throw.
} else {
parent self upperBound := parent self upperBound - 1.
parent self slot: (parent self mapping: parent self upperBound) ordinal.
}.
}.
Array nth := {
arr := self.
index := $1.
pos := if ($1 < 0)
then { (arr upperBound) + (index). }
else { (arr lowerBound) + (index). }.
if ((pos < (self lowerBound)) or (pos >= (self upperBound)))
then { err BoundsError clone throw. }
else { arr slot: (parent self mapping: pos) ordinal. }.
pos := if ($1 < 0) then {
(arr upperBound) + (index).
} else {
(arr lowerBound) + (index).
}.
if ((pos < (self lowerBound)) or (pos >= (self upperBound))) then {
err BoundsError clone throw.
} else {
arr slot: (parent self mapping: pos) ordinal.
}.
}.
Array nth= := {
arr := self.
index := $1.
value := #'$2.
pos := if ($1 < 0)
then { (arr upperBound) + (index). }
else { (arr lowerBound) + (index). }.
if ((pos < (self lowerBound)) or (pos >= (self upperBound)))
then { err BoundsError clone throw. }
else { arr slot ((parent self mapping: pos) ordinal) = #'value. }.
}.
Array size := { (self upperBound) - (self lowerBound). }.
pos := if ($1 < 0) then {
(arr upperBound) + (index).
} else {
(arr lowerBound) + (index).
}.
if ((pos < (self lowerBound)) or (pos >= (self upperBound))) then {
err BoundsError clone throw.
} else {
arr slot ((parent self mapping: pos) ordinal) = #'value.
}.
}.
Array size := {
(self upperBound) - (self lowerBound).
}.
Array join := {
index := 1.
size := self size.
delim := #'$1.
str := if (self empty?)
then { "". }
else { (parent self nth: 0) toString. }.
while { (index) < (size). }
do {
parent str := (str) ++ ((delim) ++ ((parent self nth: index) toString)).
parent index := index + 1.
}.
str := if (self empty?) then {
"".
} else {
(parent self nth: 0) toString.
}.
while { (index) < (size). } do {
parent str := (str) ++ ((delim) ++ ((parent self nth: index) toString)).
parent index := index + 1.
}.
str.
}.
Array joinText := {
index := 1.
size := self size.
delim := #'$1.
str := if (self empty?)
then { "". }
else { (parent self nth: 0) pretty. }.
while { (index) < (size). }
do {
parent str := (str) ++ ((delim) ++ ((parent self nth: index) pretty)).
parent index := index + 1.
}.
str := if (self empty?) then {
"".
} else {
(parent self nth: 0) pretty.
}.
while { (index) < (size). } do {
parent str := (str) ++ ((delim) ++ ((parent self nth: index) pretty)).
parent index := index + 1.
}.
str.
}.
Array concat := {
arr := Array clone.
self visit {
arg := #'$1.
if (#'arg is (Array))
then { #'arg visit { arr pushBack: #'$1. }. }
else { arr pushBack: #'arg. }.
if (#'arg is (Array)) then {
#'arg visit { arr pushBack: #'$1. }.
} else {
arr pushBack: #'arg.
}.
}.
arr.
}.
Array toString := { "[" ++ (self join ", ") ++ "]". }.
Array toString := {
"[" ++ (self join ", ") ++ "]".
}.
Array == := {
localize.
takes '[arr].
Expand All @@ -141,7 +163,7 @@ Array < := {
takes '[arr].
callCC {
escapable.
(arr is: Array) ifFalse { return: False. }.
(arr is: Array) ifFalse { return: False. }. ; TODO Should this be an exception?
(this zip: arr) visit {
(($1 car) < ($1 cdr)) ifTrue: { return: True. }.
(($1 car) > ($1 cdr)) ifTrue: { return: False. }.
Expand Down

0 comments on commit 4eb2929

Please sign in to comment.