Work with slim instead

This commit is contained in:
2026-06-01 07:01:25 -05:00
parent 0459b0a84e
commit 3e5932b2ad
18 changed files with 464 additions and 36 deletions

View File

@@ -0,0 +1,65 @@
class ConcurrentQueue
{
private mtx = new Mutex();
private queue = new Queue();
public Enqueue(val)
{
mtx.Lock();
queue.Enqueue(val);
mtx.Unlock();
}
public Dequeue()
{
mtx.Lock();
const val = queue.Dequeue();
mtx.Unlock();
return val;
}
public Peek()
{
mtx.Lock();
const val = queue.Peek();
mtx.Unlock();
return val;
}
public getCount()
{
mtx.Lock();
const val = queue.Count;
mtx.Unlock();
return val;
}
public getLength()
{
mtx.Lock();
const val = queue.Length;
mtx.Unlock();
return val;
}
public ToList()
{
mtx.Lock();
const ittr = new Queryable(queue);
const ls = ittr.ToList();
mtx.Unlock();
return ls;
}
public GetEnumerator()
{
return ToList().GetEnumerator();
}
}

View File

@@ -0,0 +1,65 @@
class ConcurrentStack
{
private mtx = new Mutex();
private stack = new Stack();
public Push(val)
{
mtx.Lock();
stack.Push(val);
mtx.Unlock();
}
public Pop()
{
mtx.Lock();
const val = stack.Pop();
mtx.Unlock();
return val;
}
public Top()
{
mtx.Lock();
const val = stack.Top();
mtx.Unlock();
return val;
}
public getCount()
{
mtx.Lock();
const val = stack.Count;
mtx.Unlock();
return val;
}
public getLength()
{
mtx.Lock();
const val = stack.Length;
mtx.Unlock();
return val;
}
public ToList()
{
mtx.Lock();
const ittr = new Queryable(stack);
const ls = ittr.ToList();
mtx.Unlock();
return ls;
}
public GetEnumerator()
{
return ToList().GetEnumerator();
}
}

View File

@@ -0,0 +1,32 @@
class Queue
{
private items = [];
public Enqueue(val)
{
items.Add(val);
}
public Dequeue()
{
if(items.Count == 0) return null;
const val = items[0];
items.RemoveAt(0);
return val;
}
public Peek()
{
if(items.Count == 0) return null;
return items[0];
}
public GetEnumerator()
{
return items.GetEnumerator();
}
public getCount() items.Count;
public getLength() items.Length;
}

View File

@@ -0,0 +1,24 @@
func SequenceEquals(a,b)
{
const ittra = a.GetEnumerator();
const ittrb = b.GetEnumerator();
defer {
ittra.Dispose();
ittrb.Dispose();
}
while(true) {
const hasa = ittra.MoveNext();
const hasb = ittrb.MoveNext();
if(hasa != hasb) return false;
if(hasa)
{
if(!(hasa.Current == hasb.Current)) return false;
}
else break;
}
return true;
}

View File

@@ -0,0 +1,33 @@
class Stack
{
private items = [];
public Push(val)
{
items.Insert(0, val);
}
public Pop()
{
if(items.Count == 0) return null;
const val = items[0];
items.RemoveAt(0);
return val;
}
public Top()
{
if(items.Count == 0) return null;
return items[0];
}
public getCount() items.Count;
public getLength() items.Length;
public GetEnumerator()
{
return items.GetEnumerator();
}
}

View File

@@ -0,0 +1,48 @@
class Event {
private mtx = new Mutex();
private cb = [];
public Event()
{
}
public Invoke(sender,e)
{
mtx.Lock();
each(var item : cb)
{
item(sender,e);
}
mtx.Unlock();
}
public Subscribe(val)
{
mtx.Lock();
cb.Add(val);
mtx.Unlock();
}
public Unsubscribe(val)
{
mtx.Lock();
cb.Remove(val);
mtx.Unlock();
}
public operator+(val)
{
mtx.Lock();
cb.Add(val);
mtx.Unlock();
return this;
}
public operator-(val)
{
mtx.Lock();
cb.Remove(val);
mtx.Unlock();
return this;
}
}

View File

@@ -10,7 +10,7 @@ class Exception
public ToString()
{
var messagePart = $"{Class.Name(this)}: {Message}";
var messagePart = $"{Class.Name(this)}: {this.Message}";
if(InnerException != undefined && InnerException != null)
{
var innerEx = InnerException.ToString().Replace("\n","\n\t");
@@ -18,12 +18,4 @@ class Exception
}
return messagePart;
}
}
class OutOfRangeException : Exception
{
public OutOfRangeException(varName,$inner)
{
Exception($"{varName} is out of range.",inner);
}
}

View File

@@ -0,0 +1,7 @@
class OutOfRangeException : Exception
{
public OutOfRangeException(varName,$inner)
{
Exception($"{varName} is out of range.",inner);
}
}

View File

@@ -0,0 +1,42 @@
class ExecutorQueue {
private mtx = new Mutex();
private items = [];
private thread;
private running=true;
public ExecutorQueue()
{
thread = new Thread(()=>{
while(running)
{
mtx.Lock();
if(items.Count == 0)
{
mtx.Unlock();
continue;
}
const item = items[0];
items.RemoveAt(0);
mtx.Unlock();
if(TypeIsCallable(item))
item();
}
});
}
public EnqueueJob(cb)
{
mtx.Lock();
items.Add(cb);
mtx.Unlock();
}
public Dispose()
{
mtx.Lock();
running=false;
mtx.Unlock();
thread.Join();
}
}

View File

@@ -0,0 +1,12 @@
func FuncWithThis(
function,
thisVal
)
{
return ($$args)=>{
const newArgs = [thisVal];
each(var item : args) newArgs.Add(item);
return function.Call(newArgs);
};
}

View File

@@ -0,0 +1,19 @@
class MutexLock
{
private mtx;
public MutexLock(mtx)
{
this.mtx = mtx;
mtx.Lock();
}
public Dispose()
{
mtx.Unlock();
}
}
func Lock(mtx)
{
return new MutexLock(mtx);
}

View File

@@ -0,0 +1,39 @@
class Result {
private isError=false;
private value=null;
public Result(value, isError)
{
this.value = value;
this.isError = isError;
}
static Error(value)
{
return new Result(value,true);
}
static OK(value)
{
return new Result(value,false);
}
public getIsError()
{
return this.isError;
}
public getValue()
{
return this.value;
}
public Unwrap()
{
if(isError) throw value;
return value;
}
}

View File

@@ -0,0 +1,48 @@
Test("Collections",()=>{
Spec("SequenceEquals",()=>{
enumerable func myenum()
{
yield 42;
yield 69;
yield 128;
yield "Hello";
}
Assert(SequenceEquals("MyString",['M','y','S','t','r','i','n','g']), "SequenceEquals is not functioning correctly, String MyString and array with same chars should return true");
Assert(SequenceEquals(myenum(), [42,69,128,"Hello"]), "SequenceEquals is not functioning correctly, enumerable and list should return same result");
});
Spec("Queue", ()=>{
const queue = new Queue();
queue.Enqueue("Apple");
queue.Enqueue(37);
Assert(!TypeIsDefined(queue.Enqueue("My String")), "Queue.Enqueue should not return value, but it does");
Expect(queue.Dequeue()).ToBe("Apple");
Expect(queue.Peek()).LessThan(42);
Expect(queue.Count).ToBe(2);
queue.Dequeue();
Expect(queue.Count).ToBe(1);
queue.Enqueue(4);
queue.Enqueue(5);
queue.Enqueue(true);
Assert(SequenceEquals(queue,["My String",4,5,true]), "Queue isn't working correctly");
});
Spec("Stack", ()=>{
const stack = new Stack();
Assert(!TypeIsDefined(stack.Push(42)),"Stack.Push should not return a value, but it does");
stack.Push(59);
Expect(stack.Count).ToBe(2);
Expect(stack.Pop()).ToBe(59);
Expect(stack.Top()).ToBe(42);
Expect(stack.Pop()).ToBe(42);
});
});