make linked lists fully mutable
This commit is contained in:
parent
2e126a6652
commit
a8b0a1bb53
2 changed files with 44 additions and 30 deletions
|
@ -12,16 +12,15 @@ construct LinkedList {
|
||||||
}
|
}
|
||||||
i
|
i
|
||||||
}
|
}
|
||||||
push { this | with item this ;
|
push { | with item this ;
|
||||||
item:unwrap;
|
item:unwrap;
|
||||||
this:value null eq if {
|
this:value null eq if {
|
||||||
item this:=value;
|
item this:=value;
|
||||||
this 2 stop
|
2 stop
|
||||||
}
|
}
|
||||||
LinkedList:new
|
LinkedList:new
|
||||||
dup :=value;<item>
|
dup :=value;<item>
|
||||||
this:last-entry:=next;
|
this:last-entry:=next;
|
||||||
this
|
|
||||||
}
|
}
|
||||||
last-entry { list | with this ;
|
last-entry { list | with this ;
|
||||||
while { this:next null eq not } {
|
while { this:next null eq not } {
|
||||||
|
@ -47,17 +46,20 @@ construct LinkedList {
|
||||||
:=next;<null>
|
:=next;<null>
|
||||||
item:value
|
item:value
|
||||||
}
|
}
|
||||||
push-front { list | with item this ;
|
push-front { | with item this ;
|
||||||
item:unwrap;
|
item:unwrap;
|
||||||
this:value null eq if {
|
this:value null eq if {
|
||||||
item this:=value;
|
item this:=value;
|
||||||
this 2 stop
|
2 stop
|
||||||
}
|
}
|
||||||
LinkedList:new
|
"append new next identical to this, then make this contain new value";
|
||||||
dup :=next;<this>
|
def new LinkedList:new =new
|
||||||
dup :=value;<item>
|
this:next new:=next;
|
||||||
|
this:value new:=value;
|
||||||
|
item this:=value;
|
||||||
|
new this:=next;
|
||||||
}
|
}
|
||||||
insert { list | with item index this ;
|
insert { | with item index this ;
|
||||||
item:unwrap;
|
item:unwrap;
|
||||||
index 0 eq if {
|
index 0 eq if {
|
||||||
item this:push-front 2 stop
|
item this:push-front 2 stop
|
||||||
|
@ -67,13 +69,19 @@ construct LinkedList {
|
||||||
list:next =list
|
list:next =list
|
||||||
index -- =index
|
index -- =index
|
||||||
}
|
}
|
||||||
item list:next:push-front list:=next;
|
item list:next:push-front
|
||||||
this
|
|
||||||
}
|
}
|
||||||
pop-front { item list | with this ;
|
pop-front { item | with this ;
|
||||||
this:value this:next
|
this:value
|
||||||
|
this:next null eq dup if {
|
||||||
|
null this:=value
|
||||||
|
}
|
||||||
|
not if {
|
||||||
|
this:next:value this:=value
|
||||||
|
this:next:next this:=next
|
||||||
|
}
|
||||||
}
|
}
|
||||||
remove { item list | with index this ;
|
remove { item | with index this ;
|
||||||
index 0 eq if {
|
index 0 eq if {
|
||||||
this:pop-front 2 stop
|
this:pop-front 2 stop
|
||||||
}
|
}
|
||||||
|
@ -82,8 +90,7 @@ construct LinkedList {
|
||||||
list:next =list
|
list:next =list
|
||||||
index -- =index
|
index -- =index
|
||||||
}
|
}
|
||||||
list:next:pop-front list:=next
|
list:next:pop-front
|
||||||
this
|
|
||||||
}
|
}
|
||||||
get { item | with index this ;
|
get { item | with index this ;
|
||||||
while { index 0 gt } {
|
while { index 0 gt } {
|
||||||
|
@ -92,6 +99,13 @@ construct LinkedList {
|
||||||
}
|
}
|
||||||
this:value
|
this:value
|
||||||
}
|
}
|
||||||
|
set { item | with value index this ;
|
||||||
|
while { index 0 gt } {
|
||||||
|
this:next =this
|
||||||
|
index -- =index
|
||||||
|
}
|
||||||
|
this:value value this:=value
|
||||||
|
}
|
||||||
foreach { | with callable this ;
|
foreach { | with callable this ;
|
||||||
while { this null eq not } {
|
while { this null eq not } {
|
||||||
this:value dup null eq not if { callable:call; }
|
this:value dup null eq not if { callable:call; }
|
||||||
|
|
28
test.spl
28
test.spl
|
@ -1,11 +1,11 @@
|
||||||
[
|
[
|
||||||
"#stream.spl" import
|
"spl/stream.spl" import
|
||||||
"#http.spl" import
|
"spl/http.spl" import
|
||||||
"#messaging.spl" import
|
"spl/messaging.spl" import
|
||||||
"#server.spl" import
|
"spl/server.spl" import
|
||||||
"#time.spl" import
|
"spl/time.spl" import
|
||||||
"#httpserver/base.spl" import
|
"spl/httpserver/base.spl" import
|
||||||
"#linkedlist.spl" import
|
"spl/linkedlist.spl" import
|
||||||
|
|
||||||
|
|
||||||
"SPL tester" =program-name
|
"SPL tester" =program-name
|
||||||
|
@ -271,31 +271,31 @@ func main { int | with args ;
|
||||||
list:pop;
|
list:pop;
|
||||||
list:iter:join<", "> println;
|
list:iter:join<", "> println;
|
||||||
"=> removing 5: " print
|
"=> removing 5: " print
|
||||||
list:remove<5> =list pop
|
list:remove;<5>
|
||||||
list:iter:join<", "> println;
|
list:iter:join<", "> println;
|
||||||
"=> popping front: " print
|
"=> popping front: " print
|
||||||
list:pop-front =list pop
|
list:pop-front;
|
||||||
list:iter:join<", "> println;
|
list:iter:join<", "> println;
|
||||||
|
|
||||||
"=> inserting 0 back: " print
|
"=> inserting 0 back: " print
|
||||||
0 list:insert<0> =list
|
0 list:insert;<0>
|
||||||
list:iter:join<", "> println;
|
list:iter:join<", "> println;
|
||||||
|
|
||||||
"=> inserting 5 back: " print
|
"=> inserting 5 back: " print
|
||||||
5 list:insert<5> =list
|
5 list:insert;<5>
|
||||||
list:iter:join<", "> println;
|
list:iter:join<", "> println;
|
||||||
|
|
||||||
|
|
||||||
5 :foreach<{ | "" println }>
|
5 :foreach<{ | pop "" println }>
|
||||||
|
|
||||||
"you now have a chance to connect too: localhost :4075 :4076 - stopping in 5 seconds..." println;
|
|
||||||
5000 time:sleep;
|
|
||||||
|
|
||||||
] dup :len 0 eq not if {
|
] dup :len 0 eq not if {
|
||||||
"" println
|
"" println
|
||||||
"!! something went wrong somewhere. the stack is not empty." println
|
"!! something went wrong somewhere. the stack is not empty." println
|
||||||
dyn-__dump
|
dyn-__dump
|
||||||
}
|
}
|
||||||
|
"you now have a chance to connect too: localhost :4075 :4076 - stopping in 5 seconds..." println;
|
||||||
|
5000 time:sleep;
|
||||||
100
|
100
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue