Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,45 @@
MODE DIETITEM = STRUCT (
STRING food, annual quantity, units, REAL cost
);
MODE OBJVALUE = DIETITEM;
# PUSH element to stack #
OP +:= = (REF FLEX[]OBJVALUE stack, OBJVALUE item) VOID:
BEGIN
FLEX[UPB stack + 1]OBJVALUE newstack;
newstack[2:UPB newstack] := stack;
newstack[1] := item;
stack := newstack
END;
OP POP = (REF FLEX[]OBJVALUE stack) OBJVALUE:
IF UPB stack > 0 THEN
OBJVALUE result = stack[1];
stack := stack[2:UPB stack];
result
ELSE
# raise index error; # SKIP
FI;
# Stigler's 1939 Diet ... #
FORMAT diet item fmt = $g": "g" "g" = $"zd.dd$;
[]DIETITEM stigler diet = (
("Cabbage", "111","lb.", 4.11),
("Dried Navy Beans", "285","lb.", 16.80),
("Evaporated Milk", "57","cans", 3.84),
("Spinach", "23","lb.", 1.85),
("Wheat Flour", "370","lb.", 13.33),
("Total Annual Cost", "","", 39.93)
);
FLEX[0]DIETITEM example stack;
FOR i TO UPB stigler diet DO
example stack +:= stigler diet[i]
OD;
printf($"Items popped in reverse:"l$);
WHILE UPB example stack > 0 DO
printf((diet item fmt, POP example stack, $l$))
OD

View file

@ -0,0 +1,12 @@
defmodule Stack do
def new, do: []
def empty?([]), do: true
def empty?(_), do: false
def pop([h|t]), do: {h,t}
def push(h,t), do: [h|t]
def top([h|_]), do: h
end

View file

@ -41,25 +41,15 @@ class Stack
# If passing a number _n_, removes the top _n_ elements, and returns
# an Array of them. If this Stack contains fewer than _n_ elements,
# returns them all. If this Stack is empty, returns an empty Array.
nil
def_delegator :@ary, :pop
if ([].pop(0) rescue false)
# Ruby >= 1.8.7
def_delegator :@ary, :pop
else
# Ruby < 1.8.7
def pop(*args) # :nodoc:
case len = args.length
when 0
@ary.pop
when 1
n = [@ary.length, args.first].min
@ary.slice!(-n, n)
else
raise ArgumentError, "wrong number of arguments (#{len} for 0..1)"
end
end
end
##
# :method: top
# :call-seq:
# top -> obj or nil
# top(n) -> ary
# Returns the topmost element without modifying the stack.
def_delegator :@ary, :last, :top
##
# :method: empty?

View file

@ -1,12 +1,19 @@
s = Stack.new
s.empty? # => true
s.pop # => nil
s.pop(1) # => []
s.push(1) # => Stack[1]
s.push(2, 3) # => Stack[1, 2, 3]
s.pop # => 3
s.pop(1) # => [2]
s.empty? # => false
p s = Stack.new # => Stack[]
p s.empty? # => true
p s.size # => 0
p s.top # => nil
p s.pop # => nil
p s.pop(1) # => []
p s.push(1) # => Stack[1]
p s.push(2, 3) # => Stack[1, 2, 3]
p s.top # => 3
p s.top(2) # => [2, 3]
p s # => Stack[1, 2, 3]
p s.size # => 3
p s.pop # => 3
p s.pop(1) # => [2]
p s.empty? # => false
s = Stack[:a, :b, :c]
s.pop # => :c
p s = Stack[:a, :b, :c] # => Stack[:a, :b, :c]
p s << :d # => Stack[:a, :b, :c, :d]
p s.pop # => :d