From 1b0c5455ddec96ed8bc23c1b894c4847db2bd348 Mon Sep 17 00:00:00 2001 From: redxef Date: Tue, 19 Nov 2024 21:15:41 +0100 Subject: [PATCH] feat(lisp): add quicksort implementation. --- .../Lisp/Interpreter.cs | 32 +++++++++++++++++++ Tests/Tests.cs | 2 ++ 2 files changed, 34 insertions(+) diff --git a/Jellyfin.Plugin.SmartPlaylist/Lisp/Interpreter.cs b/Jellyfin.Plugin.SmartPlaylist/Lisp/Interpreter.cs index 7d68941..8cb7631 100644 --- a/Jellyfin.Plugin.SmartPlaylist/Lisp/Interpreter.cs +++ b/Jellyfin.Plugin.SmartPlaylist/Lisp/Interpreter.cs @@ -46,6 +46,38 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp { this["fold"] = e.eval("(lambda (fc i l) (if (null l) i (fold fc (fc i (car l)) (cdr l))))"); this["any"] = e.eval("(lambda (fc l) (apply or (map fc l)))"); this["all"] = e.eval("(lambda (fc l) (apply and (map fc l)))"); + this["append"] = e.eval("(lambda (l i) (if (null l) i (cons (car l) (append (cdr l) i))))"); + this["qsort"] = e.eval( + """ + (lambda + (fc list00) + (let + (getpivot + (lambda + (list0) + (car list0))) + (split + (lambda + (list0 pivot fc h0 h1) + (cond + ((null list0) (list list0 pivot fc h0 h1)) + ((fc (car list0) pivot) (split (cdr list0) pivot fc h0 (cons (car list0) h1))) + (t (split (cdr list0) pivot fc (cons (car list0) h0) h1))))) + (sort + (lambda + (fc list0) + (cond + ((null list0) nil) + ((null (cdr list0)) list0) + (t + (let* + (halves (split list0 (getpivot list0) fc nil nil)) + (h0 (car (cdr (cdr (cdr halves))))) + (h1 (car (cdr (cdr (cdr (cdr halves)))))) + (append (sort fc h0) (sort fc h1))))))) + (sort fc list00))) + """ + ); } } diff --git a/Tests/Tests.cs b/Tests/Tests.cs index 4052b24..9c1b598 100644 --- a/Tests/Tests.cs +++ b/Tests/Tests.cs @@ -226,6 +226,8 @@ namespace Tests Assert.Equal("nil", e.eval("(all (lambda (x) (= 1 (% x 2))) (list 1 3 4 5))").ToString()); Assert.Equal("nil", e.eval("(all (lambda (x) (= x 2)) nil)").ToString()); 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()); } } }