Skip to content

Commit 4f9766b

Browse files
committed
feat(UI): add exercise sorting by focus skill
(replaces the "by prerequisite" sorting which didn't seem useful, and could cause uncaught exceptions)
1 parent 91f827b commit 4f9766b

File tree

2 files changed

+96
-73
lines changed

2 files changed

+96
-73
lines changed

src/app/learnocaml_index_main.ml

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ let get_url token dynamic_url static_url id =
7777
| Some _ -> dynamic_url ^ Url.urlencode id ^ "/"
7878
| None -> api_server ^ "/" ^ static_url ^ Url.urlencode id
7979

80-
type exercise_ordering = By_category | By_prereq | By_difficulty
80+
type exercise_ordering = By_category | By_skill | By_difficulty
8181

8282
let (exercise_filter_signal: string option React.signal), set_exercise_filter =
8383
React.S.create None
@@ -90,14 +90,34 @@ let make_exercises_to_display_signal index =
9090
let index =
9191
match exo_sort with
9292
| By_category -> index
93-
| By_prereq ->
94-
Exercise.Index.Exercises
95-
Exercise.Graph.(compute_graph index |>
96-
fold (fun acc node ->
97-
let id = node_exercise node in
98-
(id, Some (Exercise.Index.find index id)) :: acc
99-
) [] |>
100-
List.rev)
93+
| By_skill ->
94+
let module StrMap = Map.Make(struct
95+
type t = string
96+
(* Case-insensitive ordering *)
97+
let compare a b =
98+
String.(compare (lowercase_ascii a) (lowercase_ascii b))
99+
end)
100+
in
101+
let by_skill =
102+
Exercise.Index.fold_exercises (fun map id meta ->
103+
List.fold_left (fun acc skill ->
104+
StrMap.update skill
105+
(function None -> Some [id, Some meta]
106+
| Some l -> Some ((id, Some meta) :: l))
107+
acc)
108+
map
109+
meta.Exercise.Meta.focus)
110+
StrMap.empty index
111+
in
112+
let groups =
113+
StrMap.fold (fun skill exercises acc ->
114+
(skill,
115+
{Exercise.Index.title = skill;
116+
contents = Exercise.Index.Exercises (List.rev exercises)})
117+
:: acc)
118+
by_skill []
119+
in
120+
Exercise.Index.Groups (List.rev groups)
101121
| By_difficulty ->
102122
let module IntMap = Map.Make(Int) in
103123
let starmap =
@@ -259,7 +279,7 @@ let exercises_tab token : tab_handler =
259279
btn, signal)
260280
[
261281
"by_category", By_category, [%i"By category"];
262-
"by_prereq", By_prereq, [%i"By prerequisites"];
282+
"by_skill", By_skill, [%i"By skill"];
263283
"by_difficulty", By_difficulty, [%i"By difficulty"];
264284
]
265285
in

0 commit comments

Comments
 (0)