Commit 0850fe98 authored by Pietro Abate's avatar Pietro Abate

Merge branch 'master' of https://git.cduce.org/cduce

parents 6561585d a00e10e9
......@@ -53,4 +53,13 @@ let mapf (f : 'a -> 'b)(l : ['a*]) : ['b*] =
| [el; rest] -> aux f rest (acc @ [(f el)]) in
aux f l []
let mapi (f : Int -> 'a -> 'b)(l : ['a*]) : ['b*] =
let aux (f : Int -> 'a -> 'b)(l : ['a*])(pos : Int)(acc : ['b*]) : ['b*] =
match l with
| [] -> acc
| [el; rest] -> aux f rest (pos + 1) (acc @ [(f pos el)]) in
aux f l 0 []
(* let rev_map (f : 'a -> 'b)(l : ['a*]) : ['b*] = rev (mapf f l) *)
(* List scanning *)
......@@ -59,6 +59,18 @@ let concat_tests = [
("Test stdlib.list.concat.3 failed", flatten [[] [1 7 5 2] [] [2 4 7 4] [1]], [1 7 5 2 2 4 7 4 1])
] in
let even = fun (Int -> Bool; 'a\Int -> 'a\Int)
| x & Int -> x mod 2 = 0
| x -> x in
let mapf_tests = [
("Test stdlib.list.mapf.1 failed", mapf (fun ('a -> 'a) x -> x) [], [])
("Test stdlib.list.mapf.2 failed", mapf (fun ('a -> 'a) x -> x) [2 1 3], [2 1 3])
("Test stdlib.list.mapf.3 failed", mapf even [2 1 3], [`true `false `false])
] in
(* TODO: Tests on iterators *)
(* Run tests *)
run_test_suite length_tests;
......@@ -69,4 +81,5 @@ run_test_suite rev_tests;
run_test_suite append_tests;
run_test_suite rev_append_tests;
run_test_suite concat_tests;
run_test_suite mapf_tests;
print (string_of !nb_success); print " / "; print (string_of !nb_tests)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment