From f73f501642619adddd4b68c00eaab902e0bc10ca Mon Sep 17 00:00:00 2001 From: redxef Date: Tue, 17 Dec 2024 22:19:44 +0100 Subject: [PATCH] feat: add random and shuffle. --- .../Lisp/Interpreter.cs | 27 ++++++++++++++++++- Tests/Tests.cs | 3 +++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Jellyfin.Plugin.SmartPlaylist/Lisp/Interpreter.cs b/Jellyfin.Plugin.SmartPlaylist/Lisp/Interpreter.cs index 265ecd5..5ca8df6 100644 --- a/Jellyfin.Plugin.SmartPlaylist/Lisp/Interpreter.cs +++ b/Jellyfin.Plugin.SmartPlaylist/Lisp/Interpreter.cs @@ -78,6 +78,23 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp { (sort fc list00))) """ ); + this["rand"] = e.eval( + """ + (lambda + (. a) + (cond + ((null a) (random)) + ((null (cdr a)) (% (random) (car a))) + (t (+ + (car a) + (% + (random) + (- + (car (cdr a)) + (car a))))))) + """ + ); + this["shuf"] = new Symbol("shuffle"); } } @@ -114,8 +131,10 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp { public class Builtins : Dictionary { private static Dictionary ResolvedTypes = new Dictionary(); + private Random Random; public Builtins() : base() { + Random = new Random(); this["atom"] = _atom; this["eq"] = _eq; this["car"] = _car; @@ -146,11 +165,17 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp { this["string<"] = (x) => _cmp((String a, String b) => a < b, x); this["string<="] = (x) => _cmp((String a, String b) => a <= b, x); - this["haskeys"] = _haskeys; this["getitems"] = _getitems; this["invoke"] = _invoke; this["invoke-generic"] = _invoke_generic; + + this["random"] = (x) => new Lisp.Integer(Random.Next()); + this["shuffle"] = (x) => { + var newx = ((Lisp.Cons) x.First()).ToList().ToArray(); + Random.Shuffle(newx); + return Lisp.Cons.FromList(newx); + }; } private static T _agg(Func op, IEnumerable args) where T : Expression { T agg = (T) args.First(); diff --git a/Tests/Tests.cs b/Tests/Tests.cs index 826456d..a48b4ad 100644 --- a/Tests/Tests.cs +++ b/Tests/Tests.cs @@ -233,6 +233,9 @@ namespace Tests Assert.Equal("10", e.eval("(fold (lambda (a b) (+ a b)) 0 (list 1 2 3 4))").ToString()); Assert.Equal("(2 3 4 5 6 7)", e.eval("(append (list 2 3 4) (list 5 6 7))").ToString()); Assert.Equal("(1 2 3 4 5 6 7)", e.eval("(qsort (lambda (a b) (> a b)) (list 5 4 7 3 2 6 1))").ToString()); + + //Assert.Equal("", e.eval("(rand)").ToString()); + //Assert.Equal("", e.eval("(shuf (list 0 1 2 3 4 5 6))").ToString()); } } }