calcit 0.12.30

Interpreter and js codegen for Calcit
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
use core::cmp::Ordering;
use std::cell::RefCell;
use std::sync::Arc;

use rpds::HashTrieSet;

use crate::calcit::{Calcit, CalcitErr, CalcitErrKind, CalcitList, CalcitTuple};
use crate::util::number::f64_to_usize;

use crate::builtins;
use crate::call_stack::CallStackList;
use crate::runner;

pub fn new_list(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  Ok(Calcit::List(Arc::new(xs.into())))
}

pub fn count(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 1 {
    return CalcitErr::err_nodes(
      CalcitErrKind::Arity,
      "&list:count requires exactly 1 argument (a list), but received:",
      xs,
    );
  }
  match &xs[0] {
    Calcit::List(ys) => Ok(Calcit::Number(ys.len() as f64)),
    a => {
      let msg = format!(
        "&list:count requires a list as argument, but received a value of type: {}",
        crate::builtins::meta::type_of(&[a.clone()])?.lisp_str()
      );
      let hint = crate::calcit::format_proc_examples_hint(&crate::calcit::CalcitProc::NativeListCount).unwrap_or_else(|| {
        String::from(
          "💡 Hint: Use `count` function which works on multiple types including lists, or ensure you're passing a list to &list:count",
        )
      });
      CalcitErr::err_str_with_hint(CalcitErrKind::Type, msg, hint)
    }
  }
}

pub fn nth(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 2 {
    let msg = format!(
      "&list:nth requires exactly 2 arguments (list and index), but received {} arguments",
      xs.len()
    );
    let hint = String::from(
      "💡 Correct usage: `&list:nth ([] 1 2 3) 0` => 1\n  First argument: a list\n  Second argument: numeric index (0-based)",
    );
    return CalcitErr::err_nodes_with_hint(CalcitErrKind::Arity, msg, xs, hint);
  }
  match (&xs[0], &xs[1]) {
    (Calcit::List(ys), Calcit::Number(n)) => match f64_to_usize(*n) {
      Ok(idx) => match ys.get(idx) {
        Some(v) => Ok((*v).to_owned()),
        None => Ok(Calcit::Nil),
      },
      Err(e) => CalcitErr::err_str(
        CalcitErrKind::Type,
        format!("&list:nth requires a valid non-negative integer index, {e}"),
      ),
    },
    (_, _) => {
      let msg = format!(
        "&list:nth requires (list, number) but received: ({}, {})",
        crate::builtins::meta::type_of(&[xs[0].clone()])?.lisp_str(),
        crate::builtins::meta::type_of(&[xs[1].clone()])?.lisp_str()
      );
      let hint =
        String::from("💡 Correct usage: `&list:nth ([] 1 2 3) 0` => 1\n  Or use higher-level `nth` which works on multiple types");
      CalcitErr::err_str_with_hint(CalcitErrKind::Type, msg, hint)
    }
  }
}

pub fn slice(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 2 && xs.len() != 3 {
    return CalcitErr::err_str(
      CalcitErrKind::Arity,
      format!("&list:slice expected 2 or 3 arguments, but received: {}", CalcitList::from(xs)),
    );
  }
  match (&xs[0], &xs[1]) {
    (Calcit::List(ys), Calcit::Number(from)) => {
      let from_idx = f64_to_usize(*from)?;
      let to_idx = match xs.get(2) {
        Some(Calcit::Number(to)) => f64_to_usize(*to)?,
        Some(a) => {
          return CalcitErr::err_str(
            CalcitErrKind::Type,
            format!("&list:slice expected a number for index, but received: {a}"),
          );
        }
        None => ys.len(),
      };

      Ok(Calcit::List(Arc::new(ys.slice(from_idx, to_idx)?)))
    }
    (a, b) => CalcitErr::err_str(
      CalcitErrKind::Type,
      format!("&list:slice expected a list and numbers for indexes, but received: {a} {b}"),
    ),
  }
}

pub fn last(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 1 {
    return CalcitErr::err_nodes(CalcitErrKind::Arity, "&list:last expected 1 argument (a list), but received:", xs);
  }
  match &xs[0] {
    Calcit::List(ys) => {
      let n = ys.len();
      if n == 0 {
        Ok(Calcit::Nil)
      } else {
        match ys.get(n - 1) {
          Some(v) => Ok((*v).to_owned()),
          None => Ok(Calcit::Nil),
        }
      }
    }
    a => CalcitErr::err_str(CalcitErrKind::Type, format!("&list:last expected a list, but received: {a}")),
  }
}

pub fn append(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 2 {
    return CalcitErr::err_str(
      CalcitErrKind::Arity,
      format!("&list:append expected 2 arguments, but received: {}", CalcitList::from(xs)),
    );
  }
  match &xs[0] {
    Calcit::List(ys) => Ok(Calcit::List(Arc::new(ys.push_right(xs[1].to_owned())))),
    a => CalcitErr::err_str(CalcitErrKind::Type, format!("&list:append expected a list, but received: {a}")),
  }
}

pub fn prepend(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  match (xs.first(), xs.get(1)) {
    (Some(Calcit::List(ys)), Some(a)) => Ok(Calcit::List(Arc::new(ys.push_left(a.to_owned())))),
    (Some(a), _) => CalcitErr::err_str(CalcitErrKind::Type, format!("&list:prepend expected a list, but received: {a}")),
    (None, _) => CalcitErr::err_str(CalcitErrKind::Arity, "&list:prepend expected 2 arguments, but received none"),
  }
}

pub fn rest(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 1 {
    let hint = String::from("💡 Usage: `&list:rest ([] 1 2 3)` => ([] 2 3)\n  Returns all elements except the first one");
    return CalcitErr::err_nodes_with_hint(
      CalcitErrKind::Arity,
      "&list:rest requires exactly 1 argument (a list), but received:",
      xs,
      hint,
    );
  }
  match &xs[0] {
    Calcit::List(ys) => {
      if ys.is_empty() {
        Ok(Calcit::Nil)
      } else {
        Ok(Calcit::List(Arc::new(ys.drop_left())))
      }
    }
    a => {
      let msg = format!(
        "&list:rest requires a list as argument, but received: {}",
        crate::builtins::meta::type_of(&[a.clone()])?.lisp_str()
      );
      let hint = String::from(
        "💡 Hint: Use the higher-level `rest` function which works on multiple types,\n  or ensure you're passing a list to &list:rest",
      );
      CalcitErr::err_str_with_hint(CalcitErrKind::Type, msg, hint)
    }
  }
}

pub fn butlast(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 1 {
    return CalcitErr::err_nodes(CalcitErrKind::Arity, "&list:butlast expected a list, but received:", xs);
  }
  match &xs[0] {
    Calcit::Nil => Ok(Calcit::Nil),
    Calcit::List(ys) => {
      if ys.is_empty() {
        Ok(Calcit::Nil)
      } else {
        Ok(Calcit::List(Arc::new(ys.butlast()?)))
      }
    }
    a => CalcitErr::err_str(CalcitErrKind::Type, format!("&list:butlast expected a list, but received: {a}")),
  }
}

pub fn concat(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  let mut total_size = 0;
  for x in xs {
    if let Calcit::List(zs) = x {
      total_size += zs.len();
    } else {
      return CalcitErr::err_str(
        CalcitErrKind::Type,
        format!("&list:concat expected list arguments, but received: {x}"),
      );
    }
  }

  let mut ys = Vec::with_capacity(total_size);
  for x in xs {
    if let Calcit::List(zs) = x {
      // Use extend to efficiently append elements from the inner list
      ys.extend(zs.iter().map(|v| v.to_owned()));
    }
    // no need for else, already checked
  }
  Ok(Calcit::List(Arc::new(CalcitList::Vector(ys))))
}

pub fn range(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.is_empty() || xs.len() > 3 {
    return CalcitErr::err_nodes(CalcitErrKind::Arity, "&list:range expected 1 to 3 arguments, but received:", xs);
  }
  let (base, bound) = match (&xs[0], xs.get(1)) {
    (Calcit::Number(bound), None) => (0.0, *bound),
    (Calcit::Number(base), Some(Calcit::Number(bound))) => (*base, *bound),
    (a, b) => {
      return CalcitErr::err_str(
        CalcitErrKind::Type,
        format!("&list:range expected numbers for base and bound, but received: {a} {b:?}"),
      );
    }
  };

  let step = match xs.get(2) {
    Some(Calcit::Number(n)) => *n,
    Some(a) => {
      return CalcitErr::err_str(
        CalcitErrKind::Type,
        format!("&list:range expected a number for step, but received: {a}"),
      );
    }
    None => 1.0,
  };

  if (bound - base).abs() < f64::EPSILON {
    return Ok(Calcit::from(CalcitList::default()));
  }

  if step == 0.0 || (bound > base && step < 0.0) || (bound < base && step > 0.0) {
    return CalcitErr::err_str(
      CalcitErrKind::Unexpected,
      "&list:range cannot construct list with a step of 0 or invalid step direction",
    );
  }

  let mut ys = vec![];
  let mut i = base;
  if step > 0.0 {
    while i < bound {
      ys.push(Calcit::Number(i));
      i += step;
    }
  } else {
    while i > bound {
      ys.push(Calcit::Number(i));
      i += step;
    }
  }
  Ok(Calcit::from(ys))
}

pub fn reverse(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 1 {
    return CalcitErr::err_nodes(CalcitErrKind::Arity, "&list:reverse expected a list, but received:", xs);
  }
  match &xs[0] {
    Calcit::Nil => Ok(Calcit::Nil),
    Calcit::List(ys) => Ok(Calcit::from(ys.reverse())),
    a => CalcitErr::err_str(CalcitErrKind::Type, format!("&list:reverse expected a list, but received: {a}")),
  }
}

/// foldl using syntax for performance, it's supposed to be a function
pub fn foldl(xs: &[Calcit], call_stack: &CallStackList) -> Result<Calcit, CalcitErr> {
  if xs.len() == 3 {
    let mut ret = xs[1].to_owned();

    match (&xs[0], &xs[2]) {
      // dirty since only functions being call directly then we become fast
      (Calcit::List(xs), Calcit::Fn { info, .. }) => {
        for x in xs.iter() {
          ret = runner::run_fn(&[ret, (*x).to_owned()], info, call_stack)?;
        }
        Ok(ret)
      }
      (Calcit::List(xs), Calcit::Proc(proc)) => {
        for x in xs.iter() {
          // println!("foldl args, {} {}", ret, x.to_owned());
          ret = builtins::handle_proc(*proc, &[ret, (*x).to_owned()], call_stack)?;
        }
        Ok(ret)
      }
      // also handles set
      (Calcit::Set(xs), Calcit::Fn { info, .. }) => {
        for x in xs {
          ret = runner::run_fn(&[ret, x.to_owned()], info, call_stack)?;
        }
        Ok(ret)
      }
      (Calcit::Set(xs), Calcit::Proc(proc)) => {
        for x in xs {
          // println!("foldl args, {} {}", ret, x.to_owned());
          ret = builtins::handle_proc(*proc, &[ret, x.to_owned()], call_stack)?;
        }
        Ok(ret)
      }
      // also handles map
      (Calcit::Map(xs), Calcit::Fn { info, .. }) => {
        for (k, x) in xs {
          ret = runner::run_fn(
            &[ret, Calcit::from(CalcitList::from(&[k.to_owned(), x.to_owned()]))],
            info,
            call_stack,
          )?;
        }
        Ok(ret)
      }
      (Calcit::Map(xs), Calcit::Proc(proc)) => {
        for (k, x) in xs {
          // println!("foldl args, {} {}", ret, x.to_owned());
          ret = builtins::handle_proc(
            *proc,
            &[ret, Calcit::from(CalcitList::from(&[k.to_owned(), x.to_owned()]))],
            call_stack,
          )?;
        }
        Ok(ret)
      }

      (a, b) => Err(CalcitErr::use_msg_stack_location(
        CalcitErrKind::Type,
        format!("&list:foldl expected a list and a function, but received: {a} {b}"),
        call_stack,
        a.get_location().or_else(|| b.get_location()),
      )),
    }
  } else {
    Err(CalcitErr::use_msg_stack(
      CalcitErrKind::Arity,
      format!("&list:foldl expected 3 arguments, but received: {}", CalcitList::from(xs)),
      call_stack,
    ))
  }
}

/// foldl-shortcut using syntax for performance, it's supposed to be a function
/// by returning `:: bool acc`, bool indicates where performace a shortcut return
pub fn foldl_shortcut(xs: &[Calcit], call_stack: &CallStackList) -> Result<Calcit, CalcitErr> {
  if xs.len() == 4 {
    let acc = &xs[1];
    let default_value = &xs[2];
    match (&xs[0], &xs[3]) {
      // dirty since only functions being call directly then we become fast
      (Calcit::List(xs), Calcit::Fn { info, .. }) => {
        let mut state = acc.to_owned();
        for x in xs.iter() {
          let pair = runner::run_fn(&[state.to_owned(), (*x).to_owned()], info, call_stack)?;
          match pair {
            Calcit::Tuple(CalcitTuple { tag: x0, extra, .. }) => match &*x0 {
              Calcit::Bool(b) => {
                let x1 = extra.first().ok_or(CalcitErr::use_msg_stack_location(
                  CalcitErrKind::Arity,
                  "&list:foldl-shortcut expected a value in the tuple",
                  call_stack,
                  x0.get_location(),
                ))?;
                if *b {
                  return Ok((*x1).to_owned());
                } else {
                  x1.clone_into(&mut state)
                }
              }
              a => {
                return Err(CalcitErr::use_msg_stack_location(
                  CalcitErrKind::Type,
                  format!("&list:foldl-shortcut return value must be a boolean, but received: {a}"),
                  call_stack,
                  a.get_location(),
                ));
              }
            },
            _ => {
              return Err(CalcitErr::use_msg_stack(
                CalcitErrKind::Type,
                format!("&list:foldl-shortcut return value must be `:: boolean accumulator`, but received: {pair}"),
                call_stack,
              ));
            }
          }
        }
        Ok(default_value.to_owned())
      }
      // almost identical body, except for the type
      (Calcit::Set(xs), Calcit::Fn { info, .. }) => {
        let mut state = acc.to_owned();
        for x in xs {
          let pair = runner::run_fn(&[state.to_owned(), x.to_owned()], info, call_stack)?;
          match pair {
            Calcit::Tuple(CalcitTuple { tag: x0, extra, .. }) => match &*x0 {
              Calcit::Bool(b) => {
                let x1 = extra.first().ok_or(CalcitErr::use_msg_stack_location(
                  CalcitErrKind::Arity,
                  "&list:foldl-shortcut expected a value in the tuple",
                  call_stack,
                  x0.get_location(),
                ))?;
                if *b {
                  return Ok((*x1).to_owned());
                } else {
                  x1.clone_into(&mut state)
                }
              }
              a => {
                return Err(CalcitErr::use_msg_stack_location(
                  CalcitErrKind::Type,
                  format!("&list:foldl-shortcut return value must be a boolean, but received: {a}"),
                  call_stack,
                  a.get_location(),
                ));
              }
            },
            _ => {
              return Err(CalcitErr::use_msg_stack(
                CalcitErrKind::Type,
                format!("&list:foldl-shortcut return value must be `:: boolean accumulator`, but received: {pair}"),
                call_stack,
              ));
            }
          }
        }
        Ok(default_value.to_owned())
      }
      // almost identical body, escept for the type
      (Calcit::Map(xs), Calcit::Fn { info, .. }) => {
        let mut state = acc.to_owned();
        for (k, x) in xs {
          let pair = runner::run_fn(
            &[state.to_owned(), Calcit::from(CalcitList::from(&[k.to_owned(), x.to_owned()]))],
            info,
            call_stack,
          )?;
          match pair {
            Calcit::Tuple(CalcitTuple { tag: x0, extra, .. }) => match &*x0 {
              Calcit::Bool(b) => {
                let x1 = extra.first().ok_or(CalcitErr::use_msg_stack_location(
                  CalcitErrKind::Arity,
                  "&list:foldl-shortcut expected a value in the tuple",
                  call_stack,
                  x0.get_location(),
                ))?;
                if *b {
                  return Ok((*x1).to_owned());
                } else {
                  (*x1).clone_into(&mut state)
                }
              }
              a => {
                return Err(CalcitErr::use_msg_stack_location(
                  CalcitErrKind::Type,
                  format!("&list:foldl-shortcut return value must be a boolean, but received: {a}"),
                  call_stack,
                  a.get_location(),
                ));
              }
            },
            _ => {
              return Err(CalcitErr::use_msg_stack(
                CalcitErrKind::Type,
                format!("&list:foldl-shortcut return value must be `:: boolean accumulator`, but received: {pair}"),
                call_stack,
              ));
            }
          }
        }
        Ok(default_value.to_owned())
      }

      (a, b) => Err(CalcitErr::use_msg_stack_location(
        CalcitErrKind::Type,
        format!("&list:foldl-shortcut expected a list and a function, but received: {a} {b}"),
        call_stack,
        a.get_location().or_else(|| b.get_location()),
      )),
    }
  } else {
    Err(CalcitErr::use_msg_stack(
      CalcitErrKind::Arity,
      format!(
        "&list:foldl-shortcut expected 4 arguments (list, state, default, fn), but received: {}",
        CalcitList::from(xs)
      ),
      call_stack,
    ))
  }
}

pub fn foldr_shortcut(xs: &[Calcit], call_stack: &CallStackList) -> Result<Calcit, CalcitErr> {
  if xs.len() == 4 {
    // let xs = runner::evaluate_expr(&expr[0], scope, file_ns)?;
    let acc = &xs[1];
    let default_value = &xs[2];
    // let f = runner::evaluate_expr(&expr[3], scope, file_ns)?;
    match (&xs[0], &xs[3]) {
      // dirty since only functions being call directly then we become fast
      (Calcit::List(xs), Calcit::Fn { info, .. }) => {
        let mut state = acc.to_owned();
        let size = xs.len();
        for i in 0..size {
          let x = xs[size - 1 - i].to_owned();
          let pair = runner::run_fn(&[state.to_owned(), x.to_owned()], info, call_stack)?;
          match pair {
            Calcit::Tuple(CalcitTuple { tag: x0, extra, .. }) => match &*x0 {
              Calcit::Bool(b) => {
                let x1 = extra.first().ok_or(CalcitErr::use_msg_stack_location(
                  CalcitErrKind::Arity,
                  "&list:foldr-shortcut expected a value in the tuple",
                  call_stack,
                  x0.get_location(),
                ))?;
                if *b {
                  return Ok((*x1).to_owned());
                } else {
                  (*x1).clone_into(&mut state)
                }
              }
              a => {
                return Err(CalcitErr::use_msg_stack_location(
                  CalcitErrKind::Type,
                  format!("&list:foldr-shortcut return value must be a boolean, but received: {a}"),
                  call_stack,
                  a.get_location(),
                ));
              }
            },
            _ => {
              return Err(CalcitErr::use_msg_stack(
                CalcitErrKind::Type,
                format!("&list:foldr-shortcut return value must be `:: boolean accumulator`, but received: {pair}"),
                call_stack,
              ));
            }
          }
        }
        Ok(default_value.to_owned())
      }

      (a, b) => Err(CalcitErr::use_msg_stack_location(
        CalcitErrKind::Type,
        format!("&list:foldr-shortcut expected a list and a function, but received: {a} {b}"),
        call_stack,
        a.get_location().or_else(|| b.get_location()),
      )),
    }
  } else {
    Err(CalcitErr::use_msg_stack(
      CalcitErrKind::Arity,
      format!(
        "&list:foldr-shortcut expected 4 arguments (list, state, default, fn), but received: {}",
        CalcitList::from(xs)
      ),
      call_stack,
    ))
  }
}

pub fn sort(xs: &[Calcit], call_stack: &CallStackList) -> Result<Calcit, CalcitErr> {
  let comparator_result_to_ordering = |result: Result<Calcit, CalcitErr>, error_slot: &RefCell<Option<CalcitErr>>| -> Ordering {
    match result {
      Ok(Calcit::Number(x)) if x < 0.0 => Ordering::Less,
      Ok(Calcit::Number(x)) if x > 0.0 => Ordering::Greater,
      Ok(Calcit::Number(_)) => Ordering::Equal,
      Ok(v) => {
        *error_slot.borrow_mut() = Some(CalcitErr::use_msg_stack(
          CalcitErrKind::Type,
          format!("&list:sort comparator must return a number, but received: {v}"),
          call_stack,
        ));
        Ordering::Equal
      }
      Err(e) => {
        *error_slot.borrow_mut() = Some(CalcitErr::use_msg_stack(
          CalcitErrKind::Unexpected,
          format!("&list:sort failed: {e}"),
          call_stack,
        ));
        Ordering::Equal
      }
    }
  };

  match xs {
    [Calcit::List(values)] => {
      let mut next_values: Vec<Calcit> = values.to_vec();
      next_values.sort();
      Ok(Calcit::List(Arc::new(CalcitList::Vector(next_values))))
    }
    [Calcit::List(values), Calcit::Fn { info, .. }] => {
      let mut next_values: Vec<Calcit> = values.to_vec();
      let sorting_error: RefCell<Option<CalcitErr>> = RefCell::new(None);
      next_values.sort_by(|a, b| -> Ordering {
        if sorting_error.borrow().is_some() {
          return Ordering::Equal;
        }
        let result = runner::run_fn(&[(*a).to_owned(), (*b).to_owned()], info, call_stack);
        comparator_result_to_ordering(result, &sorting_error)
      });
      if let Some(e) = sorting_error.into_inner() {
        return Err(e);
      }
      Ok(Calcit::List(Arc::new(CalcitList::Vector(next_values))))
    }
    [Calcit::List(values), Calcit::Proc(proc)] => {
      let mut next_values: Vec<Calcit> = values.to_vec();
      let sorting_error: RefCell<Option<CalcitErr>> = RefCell::new(None);
      next_values.sort_by(|a, b| -> Ordering {
        if sorting_error.borrow().is_some() {
          return Ordering::Equal;
        }
        let result = builtins::handle_proc(*proc, &[(*a).to_owned(), (*b).to_owned()], call_stack);
        comparator_result_to_ordering(result, &sorting_error)
      });
      if let Some(e) = sorting_error.into_inner() {
        return Err(e);
      }
      Ok(Calcit::List(Arc::new(CalcitList::Vector(next_values))))
    }
    [a, b] => Err(CalcitErr::use_msg_stack_location(
      CalcitErrKind::Type,
      format!("&list:sort expected a list and a function, but received: {a} {b}"),
      call_stack,
      a.get_location().or_else(|| b.get_location()),
    )),
    _ => Err(CalcitErr::use_msg_stack(
      CalcitErrKind::Arity,
      format!(
        "&list:sort expected 1 or 2 arguments, but received: {}",
        Calcit::List(Arc::new(xs.into()))
      ),
      call_stack,
    )),
  }
}

pub fn first(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 1 {
    return CalcitErr::err_str(CalcitErrKind::Arity, "&list:first expected 1 argument, but received none");
  }
  match &xs[0] {
    Calcit::List(ys) => {
      if ys.is_empty() {
        Ok(Calcit::Nil)
      } else {
        Ok((ys[0]).to_owned())
      }
    }
    a => CalcitErr::err_str(CalcitErrKind::Type, format!("&list:first expected a list, but received: {a}")),
  }
}

// real implementation relies of ternary-tree
pub fn assoc_before(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 3 {
    return CalcitErr::err_nodes(CalcitErrKind::Arity, "&list:assoc-before expected 3 arguments, but received:", xs);
  }
  match (&xs[0], &xs[1]) {
    (Calcit::List(zs), Calcit::Number(n)) => match f64_to_usize(*n) {
      Ok(idx) => {
        // let ys = insert(zs, idx, xs[2].to_owned());
        Ok(Calcit::List(Arc::new(zs.assoc_before(idx, xs[2].to_owned())?)))
      }
      Err(e) => CalcitErr::err_str(CalcitErrKind::Type, format!("&list:assoc-before expected a valid index, {e}")),
    },
    (a, b) => CalcitErr::err_str(
      CalcitErrKind::Type,
      format!("&list:assoc-before expected a list and an index, but received: {a} {b}"),
    ),
  }
}

pub fn assoc_after(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 3 {
    return CalcitErr::err_nodes(CalcitErrKind::Arity, "&list:assoc-after expected 3 arguments, but received:", xs);
  }
  match (&xs[0], &xs[1]) {
    (Calcit::List(zs), Calcit::Number(n)) => match f64_to_usize(*n) {
      Ok(idx) => {
        // let ys = insert(zs, idx + 1, xs[2].to_owned());
        Ok(Calcit::from(zs.assoc_after(idx, xs[2].to_owned())?))
      }
      Err(e) => CalcitErr::err_str(CalcitErrKind::Type, format!("&list:assoc-after expected a valid index, {e}")),
    },
    (a, b) => CalcitErr::err_str(
      CalcitErrKind::Type,
      format!("&list:assoc-after expected a list and an index, but received: {a} {b}"),
    ),
  }
}

pub fn list_ques(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 1 {
    return CalcitErr::err_nodes(CalcitErrKind::Arity, "list? expects exactly 1 argument, but received:", xs);
  }
  Ok(Calcit::Bool(matches!(&xs[0], Calcit::List(_))))
}

pub fn empty_ques(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 1 {
    return CalcitErr::err_nodes(CalcitErrKind::Arity, "&list:empty? expected a list, but received:", xs);
  }
  match &xs[0] {
    Calcit::List(ys) => Ok(Calcit::Bool(ys.is_empty())),
    a => CalcitErr::err_str(CalcitErrKind::Type, format!("&list:empty? expected a list, but received: {a}")),
  }
}

pub fn contains_ques(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 2 {
    return CalcitErr::err_nodes(
      CalcitErrKind::Arity,
      "&list:contains? expected a list and an index, but received:",
      xs,
    );
  }
  match (&xs[0], &xs[1]) {
    (Calcit::List(xs), Calcit::Number(n)) => match f64_to_usize(*n) {
      Ok(idx) => Ok(Calcit::Bool(idx < xs.len())),
      Err(_) => Ok(Calcit::Bool(false)),
    },
    (a, b) => CalcitErr::err_str(
      CalcitErrKind::Type,
      format!("&list:contains? expected a list and an index, but received: {a} {b}"),
    ),
  }
}

pub fn includes_ques(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  match (xs.first(), xs.get(1)) {
    (Some(Calcit::List(xs)), Some(a)) => Ok(Calcit::Bool(xs.index_of(a).is_some())),
    (Some(a), ..) => CalcitErr::err_str(
      CalcitErrKind::Type,
      format!("&list:includes? expected a list and a value, but received: {a}"),
    ),
    (None, ..) => CalcitErr::err_nodes(CalcitErrKind::Arity, "&list:includes? expected 2 arguments, but received:", xs),
  }
}

pub fn assoc(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 3 {
    return CalcitErr::err_nodes(CalcitErrKind::Arity, "&list:assoc expected 3 arguments, but received:", xs);
  }
  match (&xs[0], &xs[1]) {
    (Calcit::List(zs), Calcit::Number(n)) => match f64_to_usize(*n) {
      Ok(idx) => {
        if idx < zs.len() {
          let mut ys: CalcitList = (**zs).to_owned();
          // ys[idx] = xs[2].to_owned();
          ys = ys.assoc(idx, xs[2].to_owned())?;
          Ok(Calcit::from(ys))
        } else {
          Ok(Calcit::List(Arc::new(xs.into())))
        }
      }
      Err(e) => CalcitErr::err_str(CalcitErrKind::Type, e),
    },
    (a, b) => CalcitErr::err_str(
      CalcitErrKind::Type,
      format!("&list:assoc expected a list and an index, but received: {a} {b}"),
    ),
  }
}

pub fn dissoc(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 2 {
    return CalcitErr::err_nodes(CalcitErrKind::Arity, "&list:dissoc expected 2 arguments, but received:", xs);
  }
  match (&xs[0], &xs[1]) {
    (Calcit::List(xs), Calcit::Number(n)) => match f64_to_usize(*n) {
      Ok(at) => Ok(Calcit::from(xs.dissoc(at)?)),
      Err(e) => CalcitErr::err_str(CalcitErrKind::Type, format!("&list:dissoc expected a valid index, {e}")),
    },
    (Calcit::List(_xs), a) => CalcitErr::err_str(
      CalcitErrKind::Type,
      format!("&list:dissoc expected a number for index, but received: {a}"),
    ),
    (a, ..) => CalcitErr::err_str(CalcitErrKind::Type, format!("&list:dissoc expected a list, but received: {a}")),
  }
}

pub fn list_to_set(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 1 {
    return CalcitErr::err_nodes(CalcitErrKind::Arity, "&list:to-set expected 1 argument, but received:", xs);
  }
  match &xs[0] {
    Calcit::List(ys) => {
      let mut zs = rpds::HashTrieSet::new_sync();
      ys.traverse(&mut |y| {
        zs.insert_mut(y.to_owned());
      });
      Ok(Calcit::Set(zs))
    }
    a => CalcitErr::err_str(CalcitErrKind::Type, format!("&list:to-set expected a list, but received: {a}")),
  }
}

pub fn distinct(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 1 {
    return CalcitErr::err_nodes(CalcitErrKind::Arity, "&list:distinct expected 1 argument, but received:", xs);
  }
  match &xs[0] {
    Calcit::List(ys) => {
      let mut seen = HashTrieSet::new_sync();
      let mut zs = CalcitList::new_inner();
      ys.traverse(&mut |y| {
        if !seen.contains(y) {
          seen.insert_mut(y.to_owned());
          zs = zs.push_right(y.to_owned());
        }
      });
      Ok(Calcit::from(CalcitList::List(zs)))
    }
    a => CalcitErr::err_str(CalcitErrKind::Type, format!("&list:distinct expected a list, but received: {a}")),
  }
}

// === BufList (mutable append-only list) ===

use std::sync::Mutex;

/// Create a new empty BufList: `(&buf-list:new)`
pub fn buf_list_new(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if !xs.is_empty() {
    return CalcitErr::err_str(CalcitErrKind::Arity, format!("&buf-list:new expects 0 args, got {}", xs.len()));
  }
  Ok(Calcit::BufList(Arc::new(Mutex::new(Vec::new()))))
}

/// Push a single element: `(&buf-list:push buf item)` — mutates and returns the same buf
pub fn buf_list_push(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 2 {
    return CalcitErr::err_str(CalcitErrKind::Arity, format!("&buf-list:push expects 2 args, got {}", xs.len()));
  }
  match &xs[0] {
    Calcit::BufList(buf) => {
      let mut items = buf.lock().expect("BufList lock");
      items.push(xs[1].to_owned());
      Ok(xs[0].to_owned())
    }
    a => CalcitErr::err_str(CalcitErrKind::Type, format!("&buf-list:push expects a buf-list, got: {a}")),
  }
}

/// Append all elements from a list: `(&buf-list:concat buf list)` — mutates and returns the same buf
pub fn buf_list_concat(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 2 {
    return CalcitErr::err_str(CalcitErrKind::Arity, format!("&buf-list:concat expects 2 args, got {}", xs.len()));
  }
  match (&xs[0], &xs[1]) {
    (Calcit::BufList(buf), Calcit::List(list)) => {
      let mut items = buf.lock().expect("BufList lock");
      for item in list.iter() {
        items.push(item.to_owned());
      }
      Ok(xs[0].to_owned())
    }
    (Calcit::BufList(_), b) => CalcitErr::err_str(CalcitErrKind::Type, format!("&buf-list:concat expects list as 2nd arg, got: {b}")),
    (a, _) => CalcitErr::err_str(CalcitErrKind::Type, format!("&buf-list:concat expects a buf-list, got: {a}")),
  }
}

/// Freeze into an immutable list: `(&buf-list:to-list buf)`
pub fn buf_list_to_list(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 1 {
    return CalcitErr::err_str(CalcitErrKind::Arity, format!("&buf-list:to-list expects 1 arg, got {}", xs.len()));
  }
  match &xs[0] {
    Calcit::BufList(buf) => {
      let items = buf.lock().expect("BufList lock");
      Ok(Calcit::from(items.clone()))
    }
    a => CalcitErr::err_str(CalcitErrKind::Type, format!("&buf-list:to-list expects a buf-list, got: {a}")),
  }
}

/// Get element count: `(&buf-list:count buf)`
pub fn buf_list_count(xs: &[Calcit]) -> Result<Calcit, CalcitErr> {
  if xs.len() != 1 {
    return CalcitErr::err_str(CalcitErrKind::Arity, format!("&buf-list:count expects 1 arg, got {}", xs.len()));
  }
  match &xs[0] {
    Calcit::BufList(buf) => {
      let items = buf.lock().expect("BufList lock");
      Ok(Calcit::Number(items.len() as f64))
    }
    a => CalcitErr::err_str(CalcitErrKind::Type, format!("&buf-list:count expects a buf-list, got: {a}")),
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::calcit::CalcitProc;

  #[test]
  fn sort_returns_error_when_comparator_not_number() {
    let values = Calcit::from(vec![Calcit::Number(3.0), Calcit::Number(1.0), Calcit::Number(2.0)]);
    let comparator = Calcit::Proc(CalcitProc::Not);

    let result = sort(&[values, comparator], &CallStackList::default());
    assert!(result.is_err());
  }

  #[test]
  fn sort_accepts_single_argument_natural_order() {
    let values = Calcit::from(vec![Calcit::Number(3.0), Calcit::Number(1.0), Calcit::Number(2.0)]);

    let result = sort(&[values], &CallStackList::default()).expect("sort should support single list argument");
    assert_eq!(
      result,
      Calcit::from(vec![Calcit::Number(1.0), Calcit::Number(2.0), Calcit::Number(3.0)])
    );
  }
}