module list-zip-test
imports libstratego-lib
strategies

  main = 
    test-suite(!"list-zip-test",
      nzip-tests
    ; zip-tests
    )

  nzip-tests =
    apply-test(!"nzip0-test"
    , nzip0(id)
    , !["a","b","c"]
    , ![(0, "a"), (1, "b"), (2, "c")]
    )

  zip-tests =
    apply-test(!"zip-test1"
    , zip(Merge)
    , !( [("foo", 1),  ("bar", 2)]
       , [("foo", 11), ("bar", 12) ]
       )
    , ![ ("foo", 1, 11), ("bar", 2, 12) ]
    )
  ; apply-test(!"zip-test2"
    , zipPad(Merge, !("Unknown", "Unknown") )
    , !( [ ("foo", 1), ("bar", 2) ]
       , [("foo", 11), ("bar", 12), ("baz", 13) ]
       )
    , ![ ("foo", 1, 11), ("bar", 2, 12), ("baz", "Unknown", 13) ]
    )
  ; apply-test(!"term argument zip"
    , zip(zip-tester |[1, 2, 3])
    , ![10, 100, 1000]
    , ![11, 102, 1003]
    )

  zip-tester(|y) =
    <add> (<id>, y)

strategies

  Merge : (("Unknown", b), (c, d)) -> (c, b, d)
  Merge : ((a, b), ("Unknown", d)) -> (a, b, d)
  Merge : ((a, b),(c, d)) -> (a, b, d) where <not("Unknown")> a

/*
  combinatory-test =
    apply-test(!"zip-test3"
	      ,join'(Merge2)
	      ,!([ ("foo", 1), ("bar", 2) ], 
		 [ ("foo", 11), ("bar", 12), ("baz", 13) ])
	      ,![ ("foo", 1, 11), ("baz", "Unknown", 13), ("bar", 2, 12)  ]
              )
*/

  join'(merge1 : a * b -> c, merge2 : a -> c, merge3 : b -> c) : // List(a) * List(b) -> List(c)
    (xs, ys) -> 
    <filter(\ x -> <filter(<merge1>(x, <id>)); try([]; <merge2> x)> ys\ )> xs

  Merge2 : ((a, b),(a, d)) -> (a, b, d)
  Merge2 : ((a, b), (c, d)) -> (c, b, "Unknown") where <not(eq)> (a, c)

