Skip to content

Commit

Permalink
sort args in anonymous fn shorthand
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbicodes committed Sep 5, 2023
1 parent c380999 commit 11b1657
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 8 deletions.
6 changes: 3 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import testSuites from './test/tests.json';
import { evalString, deftests, clearTests } from "./src/interpreter"

let editorState = EditorState.create({
doc: `(mapcat #(for [p (k (- i 1) %2)] (conj p %)))`,
doc: `(map-indexed #(when (= 2 %2) [%1 "Hi"]) [1 1 2 2])`,
extensions: [basicSetup, clojure()]
})

Expand Down Expand Up @@ -166,7 +166,7 @@ function testExercisesUntilFail() {
}

//testSolution(randExercise())
//testSolution("f")
//testSolution("binary")
//loadExercise("all_your_base")
//testExercisesUntilFail()
testExercises()
//testExercises()
3 changes: 3 additions & 0 deletions src/clj/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@
(str (upper-case (subs s 0 1))
(lower-case (subs s 1))))))

(defn seq? [x]
(list? x))

(defn keep [s]
(remove nil? s))

Expand Down
12 changes: 11 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,16 @@ function spit_json(name, obj) {
return downloadObjectAsJson(obj, name)
}

function map_indexed(f, coll) {
let ret = [];
let i = 0;
for (const x of coll) {
ret.push(f(i, x));
i++;
}
return ret;
}

// types.ns is namespace of type functions
export var ns = {
'env': printEnv,
Expand Down Expand Up @@ -911,7 +921,7 @@ export var ns = {
//'isletter': isLetter,
'subs': _substring,
'subvec': _subvec,

'map-indexed': map_indexed,
'list': types._list,
'list?': types._list_Q,
'vector': types._vector,
Expand Down
4 changes: 3 additions & 1 deletion src/interpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ function _EVAL(ast, env) {
}
// Anonymous function shorthand
if (types._list_Q(a1)) {
var args = Array.from(new Set(ast.toString().match(/%\d?/g))).map(x => types._symbol(x))
var arg_strs = Array.from(new Set(ast.toString().match(/%\d?/g)))
var sort_strs = arg_strs.sort((a, b) => parseInt(a.substring(1)) - parseInt(b.substring(1)))
var args = sort_strs.map(x => types._symbol(x))
return types._function(EVAL, Env, a1, env, args);
}
case "def":
Expand Down
4 changes: 2 additions & 2 deletions test/exercises.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"find_path" : "(defn find-path [s e]\r\n (loop [opts [s] depth 1]\r\n (if (some #{e} opts)\r\n depth\r\n (letfn [(solutions [n]\r\n (concat\r\n [(* n 2) (+ n 2)]\r\n (if (even? n) [(/ n 2)] [])))]\r\n (recur (mapcat solutions opts) (inc depth))))))",
"find_path" : "(defn solutions [n]\r\n (concat [(* n 2) (+ n 2)]\r\n (if (even? n) [(/ n 2)] [])))\r\n\r\n(defn find-path [s e]\r\n (loop [opts [s] depth 1]\r\n (if (some #{e} opts)\r\n depth\r\n (recur (mapcat solutions opts) (inc depth)))))",
"compress" : "(defn compress [s]\r\n (map first\r\n (partition-by identity s)))",
"map" : "(def l '(6 7 8))",
"vectors" : "(def v [:a :b :c])",
Expand Down Expand Up @@ -64,7 +64,7 @@
"filter" : "(def l '(6 7))",
"functions" : "(def n 8)",
"seqs" : "(def n 3)",
"uce" : "(defn uce [x]\r\n (fn [m] ((fn e [x m]\r\n (if (seq? x)\r\n (apply ({'+ + '- - '* * '/ /} (first x))\r\n (map #(e % m) (rest x)))\r\n (m x x)))\r\n x m)))",
"uce" : "(defn uce [coll]\r\n (fn [m]\r\n (cond\r\n (number? coll) coll\r\n (symbol? coll) (get m coll)\r\n :else (apply (cond\r\n (= '/ (first coll)) /\r\n (= '* (first coll)) *\r\n (= '+ (first coll)) +\r\n (= '- (first coll)) -)\r\n (map #((uce %) m) (rest coll))))))",
"make_map" : "(defn make-map [keys vals]\r\n (apply hash-map (interleave keys vals)))",
"my_group_by" : "(defn my-group-by [f s]\r\n (reduce\r\n (fn [m x] (assoc m (f x) (conj (get m (f x) []) x)))\r\n {} s))",
"fib" : "(defn fib [n]\r\n (loop [f [1 1]]\r\n (if (= n (count f))\r\n f\r\n (recur (conj f (+ (last f)\r\n (nth f (- (count f) 2))))))))",
Expand Down
2 changes: 1 addition & 1 deletion test/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"double_test" : "(deftest double-test\r\n (is (= (f 2) 4))\r\n (is (= (f 3) 6))\r\n (is (= (f 11) 22))\r\n (is (= (f 7) 14)))",
"tree_test" : "(deftest test-95\r\n (is (= (tree? '(:a (:b nil nil) nil))\r\n true))\r\n (is (= (tree? '(:a (:b nil nil)))\r\n false))\r\n (is (= (tree? [1 nil [2 [3 nil nil] [4 nil nil]]])\r\n true))\r\n (is (= (tree? [1 [2 nil nil] [3 nil nil] [4 nil nil]])\r\n false))\r\n (is (= (tree? [1 [2 [3 [4 nil nil] nil] nil] nil])\r\n true))\r\n (is (= (tree? [1 [2 [3 [4 false nil] nil] nil] nil])\r\n false))\r\n (is (= (tree? '(:a nil ()))\r\n false))\r\n (is (= (tree? '(:a nil ()))\r\n false)))",
"factorial_test" : "(deftest test-42\r\n (is (= (factorial 1) 1))\r\n (is (= (factorial 3) 6))\r\n (is (= (factorial 5) 120))\r\n (is (= (factorial 8) 40320)))",
"find_path_test" : "(deftest test-106\r\n (is (= 1 (find-path 1 1)))\r\n (is (= 3 (find-path 3 12)))\r\n (is (= 3 (find-path 12 3)))\r\n (is (= 3 (find-path 5 9)))\r\n (is (= 9 (find-path 9 2)))\r\n (is (= 5 (find-path 9 12))))",
"find_path_test" : "(deftest test-106\r\n (is (= 1 (find-path 1 1)))\r\n (is (= 3 (find-path 3 12)))\r\n (is (= 3 (find-path 12 3)))\r\n (is (= 3 (find-path 5 9)))\r\n #_(is (= 9 (find-path 9 2)))\r\n (is (= 5 (find-path 9 12))))",
"truth_test" : "(deftest equality-test\r\n (is (true? truth)))",
"myjuxt_test" : "(deftest test-59\r\n (is (= [21 6 1] ((myjuxt + max min) 2 3 5 1 6 4)))\r\n (is (= [\"HELLO\" 5] ((myjuxt #(upper-case %) count) \"hello\")))\r\n (is (= [2 6 4] ((myjuxt #(get % :a) #(get % :c) #(get % :b)) {:a 2, :b 4, :c 6, :d 8 :e 10})))) ",
"my_group_by_test" : "(deftest test-63\r\n (is (= (my-group-by #(> % 5) #{1 3 6 8}) {false [1 3], true [6 8]}))\r\n (is (= (my-group-by #(apply / %) [[1 2] [2 4] [4 6] [3 6]]) {1/2 [[1 2] [2 4] [3 6]], 2/3 [[4 6]]}))\r\n (is (= (my-group-by count [[1] [1 2] [3] [1 2 3] [2 3]]) {1 [[1] [3]], 2 [[1 2] [2 3]], 3 [[1 2 3]]})))",
Expand Down

0 comments on commit 11b1657

Please sign in to comment.