ATS2FUNCRASH/mylib
int_foreach
Interface
fun{}
int_foreach
( n0: int
, fwork: cfun(int, void)): void
Implementation
implement
{}(*tmp*)
int_foreach
(n0, fwork) =
loop(0) where
{
//
fun
loop(i: int): void =
if i < n0 then (fwork(i); loop(i+1))
//
} (* end of [int_foreach] *)
Description
Given an integer n0 and a closure-function
fwork, this function applies fwork to the integers
between 0 and n0-1, inclusive.
int_foreach_method
Interface
fun{}
int_foreach_method
(n0: int)
(fwork: cfun(int, void)): void
//
overload
.foreach with int_foreach_method of 100
//
Implementation
implement
{}(*tmp*)
int_foreach_method(n0) =
lam(fwork) => int_foreach<>(n0, fwork)
Description
This function is a curried version of ##dyncode("int_foreach")
for supporting dot-notation.
int_foldleft
Interface
fun
{res:t@ype}
int_foldleft
( n0: int
, res: res
, fopr: cfun(res, int, res)): res
Implementation
implement
{res}(*tmp*)
int_foldleft
(n0, res, fopr) =
loop(res, 0) where
{
//
fun loop(res: res, i: int): res =
if i < n0
then loop(fopr(res, i), i+1) else res
// end of [if]
//
} (* end of [int_foldleft] *)
Description
This function essentially treats its
first argument n0 as the list of integers between
0 and n0-1, inclusive and then performs left list-folding.
int_foldleft_method
Interface
fun
{res:t@ype}
int_foldleft_method
(n0: int, ty: TYPE(res))
(res: res, fopr: cfun(res, int, res)): res
//
overload
.foldleft with int_foldleft_method of 100
//
Implementation
implement
{res}(*tmp*)
int_foldleft_method(n0, ty) =
lam(res, fopr) => int_foldleft<res>(n0, res, fopr)
Description
This function is a curried version of ##dyncode("int_foldleft")
for supporting dot-notation.
int_cross_foreach
Interface
fun{}
int_cross_foreach
( m: int
, n: int
, fwork: cfun(int, int, void)): void
Implementation
implement
{}(*tmp*)
int_cross_foreach
( m, n
, fwork) =
(
int_foreach
( m
, lam(i) =>
int_foreach(n, lam(j) => fwork(i, j)))
)
Description
Given two integers m and n and a closure-function fwork, this function
applies fwork in the row-major fashion to all the pairs (i, j) such that
i ranges between 0 and m-1, inclusive, and j between 0 and n-1, inclusive.
int_cross_foreach_method
Interface
fun{}
int_cross_foreach_method
( m: int, n: int )
( fwork: cfun(int, int, void) ): void
//
overload
.cross_foreach with int_cross_foreach_method of 100
//
Implementation
implement
{}(*tmp*)
int_cross_foreach_method
(m, n) =
lam(fwork) => int_cross_foreach(m, n, fwork)
Description
This function is a curried version of #dyncode("int_cross_foreach")
for supporting dot-notation.
list0_is_nil
Interface
fun{}
list0_is_nil{a:t@ype}(list0(a)): bool
//
overload iseqz with list0_is_nil of 100
//
Implementation
implement
{}(*tmp*)
list0_is_nil(xs) =
(case+ xs of list0_nil() => true | _ => false)
Description
This function tests whether a given list0-value
is empty.
list0_is_cons
Interface
fun{}
list0_is_cons{a:t@ype}(list0(a)): bool
//
overload isneqz with list0_is_cons of 100
//
Implementation
implement
{}(*tmp*)
list0_is_cons(xs) =
(case+ xs of list0_cons _ => true | _ => false)
Description
This function tests whether a given list0-value
is non-empty.
list0_length
Interface
fun
{a:t@ype}
list0_length(xs0: list0(INV(a))): int
//
overload length with list0_length of 100
//
Implementation
implement
{a}(*tmp*)
list0_length(xs) =
list0_foldleft<int><a>(xs, 0, lam(r, _) => r + 1)
Description
This function computes the length of a given list0-value.
list0_head_exn
Interface
fun
{a:t@ype}
list0_head_exn(list0(INV(a))): (a)
//
overload .head with list0_head_exn of 100
//
Implementation
implement
{a}(*tmp*)
list0_head_exn(xs) =
let val-list0_cons(x0, _) = xs in x0 end
Description
This function returns the head of a given list0-value xs
if xs is non-empty or terminates abnormally due to pattern
matching failure.
list0_tail_exn
Interface
fun
{a:t@ype}
list0_tail_exn(list0(INV(a))): list0(a)
//
overload .tail with list0_tail_exn of 100
//
Implementation
implement
{a}(*tmp*)
list0_tail_exn(xs) =
let val-list0_cons(_, xs) = xs in xs end
Description
This function returns the tail of a given list0-value xs
if xs is non-empty or terminates abnormally due to pattern
matching failure.
list0_drop_exn
Interface
fun
{a:t@ype}
list0_drop_exn
(list0(INV(a)), int): list0(a)
//
overload .drop with list0_drop_exn of 100
//
Implementation
implement
{a}(*tmp*)
list0_drop_exn
(xs, n) =
(
if
n > 0
then let
//
val-
list0_cons(_, xs) = xs
//
in
list0_drop_exn<a>(xs, pred(n))
end // end of [then]
else (xs) // end of [else]
//
) (* list0_drop_exn *)
Description
Given a list0-value xs and an integer n, this function
returns the suffix of xs starting from element n (i.e.,
xs[n]). If n is negative, it is treated as 0.
In the case where n is greater than length(xs), the function
terminates abnormally due to pattern matching failure.
list0_take_exn
Interface
fun
{a:t@ype}
list0_take_exn
(list0(INV(a)), int): list0(a)
//
overload .take with list0_take_exn of 100
//
Implementation
implement
{a}(*tmp*)
list0_take_exn
(xs, n) =
(
if
n > 0
then let
//
val-
list0_cons(x, xs) = xs
//
in
list0_cons(x, list0_take_exn(xs, n-1))
end // end of [then]
else list0_nil() // end of [else]
//
) (* list0_take_exn *)
Description
Given a list0-value xs and an integer n, this function
returns the prefix of xs consisting of the first n elements.
If n is negative, it is treated as 0. In the case
where n is greater than length(xs), the function terminates
abnormally due to pattern matching failure.
list0_get_at_exn
Interface
fun
{a:t@ype}
list0_get_at_exn
(xs: list0(INV(a)), n: int): a
//
overload [] with list0_get_at_exn of 100
//
Implementation
implement
{a}(*tmp*)
list0_get_at_exn
(xs, n) =
(
case+ xs of
| list0_nil() =>
$raise ListSubscriptExn()
| list0_cons(x, xs) =>
if n <= 0
then x else list0_get_at_exn<a>(xs, n-1)
// end of [list0_cons]
)
Description
Given a list0-value xs and an integer n, this function
returns xs[n] if it is defined or raises ##dyncode(ListSubscriptExn).
list0_append
Interface
fun
{a:t@ype}
list0_append
(xs: list0(INV(a)), ys: list0(a)): list0(a)
Implementation
implement
{a}(*tmp*)
list0_append(xs, ys) =
list0_foldright<a><list0(a)>
(xs, lam(x, ys) => list0_cons(x, ys), ys)
Description
Given two list0-values xs and ys, this function returns
another list0-value that is the concatenation of xs and ys.
list0_concat
Interface
fun
{a:t@ype}
list0_concat(xs: list0(list0(INV(a)))): list0(a)
Implementation
implement
{a}(*tmp*)
list0_concat(xss) =
list0_foldright<list0(a)><list0(a)>
(xss, lam(xs, res) => list0_append(xs, res), list0_nil())
Description
Given a list0-value xss in which each element is also
a list0-value, this function returns another list0-value
that is formed by concatenating all of the elements in xss.
list0_reverse
Interface
fun
{a:t@ype}
list0_reverse
(xs: list0(INV(a))): list0(a)
Implementation
implement
{a}(*tmp*)
list0_reverse(xs) =
list0_revappend<a>(xs, list0_nil())
Description
Given a list0-value xs, this function returns
another list0-value ys that is the reverse of xs.
list0_revappend
Interface
fun
{a:t@ype}
list0_revappend
(xs: list0(INV(a)), ys: list0(a)): list0(a)
Implementation
implement
{a}(*tmp*)
list0_revappend(xs, ys) =
list0_foldleft<list0(a)><a>
(xs, ys, lam(ys, x) => list0_cons(x, ys))
Description
Given two list0-values xs and ys, this function returns
another list0-value that is the concatenation of the reverse
of xs and ys.
list0_map
Interface
fun
{a:t@ype}
{b:t@ype}
list0_map
( xs: list0(INV(a))
, fopr: cfun(a, b)): list0(b)
Implementation
implement
{a}{b}
list0_map
(
xs, fopr
) = auxlst(xs) where
{
//
fun
auxlst
(xs: list0(a)): list0(b) =
(
case+ xs of
| list0_nil() =>
list0_nil()
| list0_cons(x, xs) =>
list0_cons(fopr(x), auxlst(xs))
)
//
} (* end of [list0_map] *)
Description
Given a list0-value xs and a closure-function f, this function
returns another list0-value consisting of f(xs[0]), ..., f(xs[n-1]),
where n is the length of xs. The implementation of this function in
ATSLIB is tail-recursive.
list0_map_method
Interface
fun
{a:t@ype}
{b:t@ype}
list0_map_method
( xs: list0(INV(a)) )
( fopr: cfun1(a, b) ): list0(b)
//
overload
.map with list0_map_method of 100
//
Implementation
implement
{a}{b}
list0_map_method
(xs) =
(
lam(fopr) => list0_map<a><b>(xs, fopr)
)
Description
This function is
a curried version of #dyncode("list0_map")
for supporting dot-notation.
list0_imap
Interface
fun
{a:t@ype}
{b:t@ype}
list0_imap
( xs: list0(INV(a))
, fopr: cfun(int, a, b)): list0(b)
Implementation
implement
{a}{b}
list0_imap
(
xs, fopr
) = auxlst(0, xs) where
{
//
fun
auxlst
(i: int, xs: list0(a)): list0(b) =
(
case+ xs of
| list0_nil() =>
list0_nil()
| list0_cons(x, xs) =>
list0_cons(fopr(i, x), auxlst(i+1, xs))
)
//
} (* end of [list0_imap] *)
Description
This function is
the standard indexed version of ##dyncode("list0_map").
list0_imap_method
Interface
fun
{a:t@ype}
{b:t@ype}
list0_imap_method
(list0(INV(a)), TYPE(b))
( fopr: cfun(int, a, b) ): list0(b)
Implementation
implement
{a}{b}
list0_imap_method
(xs, _(*type*)) =
(
lam(fopr) => list0_imap<a><b>(xs, fopr)
)
Description
This function is a curried version of ##dyncode("list0_imap")
for supporting dot-notation.
list0_mapopt
Interface
fun
{a:t@ype}
{b:t@ype}
list0_mapopt
( xs: list0(INV(a))
, fopr: cfun(a, option0(b))): list0(b)
Implementation
implement
{a}{b}
list0_mapopt
(
xs, fopr
) = auxlst(xs) where
{
//
fun
auxlst
(xs: list0(a)): list0(b) =
(
case+ xs of
| list0_nil() =>
list0_nil()
| list0_cons(x, xs) =>
(
case+ fopr(x) of
| None0() => auxlst(xs)
| Some0(y) => list0_cons(y, auxlst(xs))
)
)
//
} (* end of [list0_mapopt] *)
Description
Given a list0-value xs and a closure-function f, this function
returns another list0-value consisting of the join of the option
values f(xs[0]), ..., f(xs[n-1]), where n is the length of xs. The
implementation of this function in ATSLIB is tail-recursive.
list0_mapopt_method
Interface
fun
{a:t@ype}
{b:t@ype}
list0_mapopt_method
( xs: list0(INV(a)) )
( fopr: cfun(a, option0(b)) ): list0(b)
//
overload
.mapopt with list0_mapopt_method of 100
//
Implementation
implement
{a}{b}
list0_mapopt_method
(xs) =
(
lam(fopr) => list0_mapopt<a><b>(xs, fopr)
)
Description
This function is a curried version of
##dyncode("list0_mapopt") for supporting dot-notation.
list0_mapjoin
Interface
fun
{a:t@ype}
{b:t@ype}
list0_mapjoin
( xs: list0(INV(a))
, fopr: cfun(a, list0(b))): list0(b)
Implementation
implement
{a}{b}
list0_mapjoin
(xs, fopr) =
(
list0_concat<b>(list0_map<a><list0(b)>(xs, fopr))
)
Description
This function applies ##dyncode("list0_map") to generate
a list0-value yss in which each element is also a list0-value
and then applies ##dyncode("list0_concat") to flatten yss.
list0_mapjoin_method
Interface
fun
{a:t@ype}
{b:t@ype}
list0_mapjoin_method
( xs: list0(INV(a)) )
( fopr: cfun(a, list0(b)) ): list0(b)
//
overload
.mapjoin with list0_mapjoin_method of 100
//
Implementation
implement
{a}{b}
list0_mapjoin_method
(xs) =
(
lam(fopr) => list0_mapjoin<a><b>(xs, fopr)
)
Description
This function is a curried version of
##dyncode("list0_mapjoin") for supporting dot-notation.
int_list0_map
Interface
fun
{a:t@ype}
int_list0_map
( n: int
, fopr: cfun(int, a)): list0(a)
Implementation
implement
{a}(*tmp*)
int_list0_map
(n, fopr) =
auxmain(0) where
{
fun
auxmain
(
i: int
) : list0(a) =
if
(i < n)
then
(
list0_cons(fopr(i), auxmain(i+1))
) else list0_nil((*void*))
} (* end of [int_list0_map] *)
Description
Given an integer n and a closure-function f,
this function returns a list0-value consisting of
f(i) where i ranges between 0 and n-1, inclusive.
int_list0_mapopt
Interface
fun
{a:t@ype}
int_list0_mapopt
( n: int
, fopr: cfun(int, option0(a))): list0(a)
Implementation
implement
{a}(*tmp*)
int_list0_mapopt
(n, fopr) =
auxmain(0) where
{
//
fun
auxmain
(
i: int
) : list0(a) =
if
(i < n)
then
(
case+ fopr(i) of
| None0() => auxmain(i+1)
| Some0(x) => list0_cons(x, auxmain(i+1))
) else list0_nil((*void*))
//
} (* end of [int_list0_mapopt] *)
Description
Given an integer n and a closure-function f,
this function returns a list0-value obtained from joining
the option0-values f(i) where i ranges between 0 and n-1, inclusive.
The actual implementation of this function in ATSLIB is tail-recursive.
int_cross_list0_map
Interface
fun
{a:t@ype}
int_cross_list0_map
( m: int
, n: int
, fopr: cfun(int, int, a)): list0(a)
Implementation
implement
{a:t@ype}
int_cross_list0_map
(m, n, fopr) =
list0_concat<a>
(
int_list0_map<list0(a)>
( m
, lam(i) =>
int_list0_map<a>(n, lam(j) => fopr(i, j))
) (* int_list0_map *)
) (* int_cross_list0_map *)
Description
Given two integers m and n and a closure-function f,
this function returns a list0-value consisting of f(i,j)
enumerated in the row-major fashion for i ranging between
0 and m-1 and j between 0 and n-1.
list0_foreach
Interface
fun
{a:t@ype}
list0_foreach
( xs: list0(INV(a))
, fwork: cfun(a, void)): void
Implementation
implement
{a}(*tmp*)
list0_foreach
(
xs, fwork
) = loop(xs) where
{
//
fun
loop
(xs: list0(a)): void =
(
case+ xs of
| list0_nil() => ()
| list0_cons(x, xs) => (fwork(x); loop(xs))
)
//
} (* end of [list0_foreach] *)
Description
Given a list0-value xs and a closure-function fwork,
this function traverses xs from left to right, applying fwork
to each encountered element.
list0_foreach_method
Interface
fun
{a:t@ype}
list0_foreach_method
( xs: list0(INV(a)) )
( fwork: cfun(a, void) ): void
//
overload
.foreach with list0_foreach_method of 100
//
Implementation
implement
{a}(*tmp*)
list0_foreach_method
(xs) =
(
lam(fwork) => list0_foreach<a>(xs, fwork)
)
Description
This function is a curried version of
##dyncode("list0_foreach") for supporting dot-notation.
list0_iforeach
Interface
fun
{a:t@ype}
list0_iforeach
( xs: list0(INV(a))
, fwork: cfun(int, a, void)): void
Implementation
implement
{a}(*tmp*)
list0_iforeach
(
xs, fwork
) = loop(0, xs) where
{
//
fun
loop
(i: int, xs: list0(a)): void =
(
case+ xs of
| list0_nil() => ()
| list0_cons(x, xs) => (fwork(i, x); loop(i+1, xs))
)
//
} (* end of [list0_iforeach] *)
Description
This function is
the standard indexed version of ##dyncode("list0_foreach").
list0_iforeach_method
Interface
fun
{a:t@ype}
list0_iforeach_method
( xs: list0(INV(a)) )
( fwork: cfun(int, a, void) ): void
Implementation
implement
{a}(*tmp*)
list0_iforeach_method
(xs) =
(
lam(fwork) => list0_iforeach<a>(xs, fwork)
)
Description
This function is a curried version of
##dyncode("list0_iforeach") for supporting dot-notation.
list0_forall
Interface
fun
{a:t@ype}
list0_forall
(xs: list0(INV(a)), test: cfun(a, bool)): bool
Implementation
implement
{a}(*tmp*)
list0_forall(xs, test) =
list0_find_index<a>(xs, lam(x) => not(test(x))) < 0
Description
Given a list0-value xs and a predicate, this function
returns true if and only if every element in xs satisfies
the predicate.
list0_exists
Interface
fun
{a:t@ype}
list0_exists
( xs: list0(INV(a))
, test: cfun(a, bool)): bool
Implementation
implement
{a}(*tmp*)
list0_exists(xs, test) =
list0_find_index<a>(xs, test) >= 0
Description
Given a list0-value xs and a predicate, this function
returns true if and only if there exists in xs one element
satisfying the predicate.
list0_find_index
Interface
fun
{a:t@ype}
list0_find_index
( xs: list0(INV(a))
, test: cfun(a, bool)): int
Implementation
implement
{a}(*tmp*)
list0_find_index
(xs, test) = let
//
fun
loop
(xs: list0(a), i: int): int =
(
case+ xs of
| list0_nil() => ~1
| list0_cons(x, xs) =>
if test(x) then i else loop(xs, i+1)
)
//
in
loop(xs, 0)
end // end of [list0_find_index]
Description
Given a list0-value xs and a predicate, this function
returns the position of the first element in xs that satisfies
the predicate or -1 if there exists no element as such.
list0_forall_method
Interface
fun
{a:t@ype}
list0_forall_method
( xs: list0(INV(a)) )
( test: cfun(a, bool) ): bool
Implementation
implement
{a}(*tmp*)
list0_forall_method(xs) =
lam(test) => list0_forall<a>(xs, test)
Description
This function is a curried version of ##dyncode("list0_forall")
for supporting dot-notation.
list0_exists_method
Interface
fun
{a:t@ype}
list0_exists_method
( xs: list0(INV(a)) )
( test: cfun(a, bool) ): bool
Implementation
implement
{a}(*tmp*)
list0_exists_method(xs) =
lam(test) => list0_exists<a>(xs, test)
Description
This function is a curried version of ##dyncode("list0_exists")
for supporting dot-notation.
list0_foldleft
Interface
fun
{r:t@ype}
{a:t@ype}
list0_foldleft
( xs: list0(INV(a))
, r0: r, fopr: cfun(r, a, r)
) : (r) // end of [list0_foldleft]
Implementation
implement
{r}{a}
list0_foldleft
(xs, r0, fopr) =
loop(xs, r0) where
{
fun
loop
(xs: list0(a), r: r): r =
(
case+ xs of
| list0_nil() => r
| list0_cons(x, xs) => loop(xs, fopr(r, x))
)
}
Description
This function performs so-called left list-folding:
Given a list xs, an initial value r0, and a closure-function
fopr, it returns r0 if xs is empty or it applies fopr to r0
and xs[0] to generate a new value to be used for processing
the tail of xs.
list0_ifoldleft
Interface
fun
{r:t@ype}
{a:t@ype}
list0_ifoldleft
( xs: list0(INV(a))
, r0: r, fopr: cfun(r, int, a, r)
) : (r) // end of [list0_ifoldleft]
Implementation
implement
{r}{a}
list0_ifoldleft
(xs, r0, fopr) =
loop(xs, 0(*i*), r0) where
{
fun
loop
(xs: list0(a), i: int, r: r): r =
(
case+ xs of
| list0_nil() => r
| list0_cons(x, xs) => loop(xs, i+1, fopr(r, i, x))
)
} (* end of [list0_ifoldleft] *)
Description
This function is
the standard indexed version of ##dyncode("list0_foldleft").
list0_foldright
Interface
fun
{a:t@ype}
{r:t@ype}
list0_foldright
( xs: list0(INV(a))
, fopr: cfun(a, r, r), r0: r
) : (r) // end of [list0_foldright]
Implementation
implement
{a}{r}
list0_foldright
( xs
, fopr, r0) =
auxlst(xs) where
{
fun
auxlst
(xs: list0(a)): r =
(
case+ xs of
| list0_nil() => r0
| list0_cons(x, xs) => fopr(x, auxlst(xs))
) (* end of [auxlst] *)
}
Description
This function performs so-called right list-folding
on a given list, which is essentially left list-folding
on the reverse of the list.
int_stream_from
Interface
fun
int_stream_from(n: int): stream(int)
Implementation
implement
int_stream_from(n) =
$delay(stream_cons(n, int_stream_from(n+1)))
Description
Given an integer n, this function
returns the stream of integers n, n+1, n+2, etc.
stream_make_list0
Interface
fun
{a:t@ype}
stream_make_list0(list0(INV(a))): stream(a)
Implementation
implement
{a}(*tmp*)
stream_make_list0
(xs) = $delay
(
case+ xs of
| list0_nil() =>
stream_nil()
| list0_cons(x, xs) =>
stream_cons(x, stream_make_list0<a>(xs))
)
Description
This function turns a given
list0-value into the corresponding stream-value.
stream_takeLte
Interface
fun
{a:t@ype}
stream_takeLte
(xs: stream(a), n: int): stream(a)
Implementation
implement
{a}(*tmp*)
stream_takeLte
(xs, n) = $delay
(
if
n > 0
then
(
case+ !xs of
| stream_nil() =>
stream_nil()
| stream_cons(x, xs) =>
stream_cons(x, stream_takeLte(xs, n-1))
)
else stream_nil((*void*))
) (* end of [stream_takeLte] *)
Description
Given a stream-value xs and an integer n, this function
returns another stream-value consisting of the first n
elements in xs if xs contains so many elements or all of
the elements in xs otherwise.
stream_get_at_exn
Interface
fun
{a:t@ype}
stream_get_at_exn(stream(a), n: int): a
//
overload [] with stream_get_at_exn of 100
//
Implementation
implement
{a}(*tmp*)
stream_get_at_exn
(xs, n) =
(
case- !xs of
(*
| stream_nil() =>
(
$raise StreamSubscriptExn()
)
*)
| stream_cons(x, xs) =>
(
if n <= 0
then x else stream_get_at_exn<a>(xs, n-1)
// end of [if]
)
)
Description
This function is
the corresponding version
of ##dyncode("list0_get_at_exn") on streams.
stream_append
Interface
fun
{a:t@ype}
stream_append
(xs: stream(a), ys: stream(a)): stream(a)
Implementation
implement
{a}(*tmp*)
stream_append
(xs, ys) = $delay
(
case+ !xs of
| stream_nil() => !ys
| stream_cons(x, xs) =>
stream_cons(x, stream_append<a>(xs, ys))
)
Description
This function is
the corresponding version of
##dyncode("list0_append") on streams.
stream_concat
Interface
fun
{a:t@ype}
stream_concat
(xss: stream(stream(a))): stream(a)
Implementation
implement
{a}(*tmp*)
stream_concat(xss) = $delay
(
case+ !xss of
| stream_nil() =>
stream_nil()
| stream_cons(xs, xss) => !
(
stream_append<a>(xs, stream_concat<a>(xss))
) (* end of [stream_cons] *)
)
Description
This function is
the corresponding version of
##dyncode("list0_concat") on streams.
stream_map
Interface
fun
{a:t@ype}
{b:t@ype}
stream_map
(xs: stream(a)
, fopr: cfun(a, b)): stream(b)
Implementation
implement
{a}{b}
stream_map
(xs, fopr) = $delay
(
case+ !xs of
| stream_nil() =>
stream_nil()
| stream_cons(x, xs) =>
stream_cons
(fopr(x), stream_map<a><b>(xs, fopr))
// end of [stream_cons]
)
Description
This function is the corresponding
version of ##dyncode("list0_map") on streams.
stream_imap
Interface
fun
{a:t@ype}
{b:t@ype}
stream_imap
(xs: stream(a)
, fopr: cfun(int, a, b)): stream(b)
Implementation
implement
{a}{b}
stream_imap
(xs, fopr) = let
//
fun
auxmain
(
xs: stream(a), i: int
) : stream(b) = $delay
(
case+ !xs of
| stream_nil() =>
stream_nil()
| stream_cons(x, xs) =>
stream_cons(fopr(i, x), auxmain(xs, i+1))
)
//
in
auxmain(xs, 0(*i*))
end // end of [stream_imap]
Description
This function is the corresponding
version of ##dyncode("list0_imap") on streams.
stream_filter
Interface
fun
{a:t@ype}
stream_filter
( xs: stream(a)
, test: cfun(a, bool)): stream(a)
Implementation
implement
{a}(*tmp*)
stream_filter
(xs, test) = $delay
(
case+ !xs of
| stream_nil() =>
stream_nil()
| stream_cons(x, xs) =>
if test(x)
then
stream_cons
(x, stream_filter<a>(xs, test))
// end of [then]
else !(stream_filter<a>(xs, test))
// end of [if]
)
Description
This function is
the corresponding version of
##dyncode("list0_filter") on streams.
stream_foreach
Interface
fun
{a:t@ype}
stream_foreach
( xs: stream(a)
, fwork: cfun(a, void)
) : void // end-of-function
Implementation
implement
{a}(*tmp*)
stream_foreach(xs, fwork) =
(
//
case+ !xs of
| stream_nil() => ()
| stream_cons(x, xs) =>
(fwork(x); stream_foreach<a>(xs, fwork))
//
) (* end of [stream_foreach] *)
Description
This function is
the corresponding version of
##dyncode("list0_foreach") on streams.
stream_foreach_method
Interface
fun
{a:t@ype}
stream_foreach_method
( xs: stream(INV(a)) )
( fwork: cfun(a, void) ): void
Implementation
implement
{a}(*tmp*)
stream_foreach_method
(xs) =
(
lam(fwork) => stream_foreach<a>(xs, fwork)
)
Description
This function is a curried version of
##dyncode("stream_foreach") for supporting dot-notation.
stream_iforeach
Interface
fun
{a:t@ype}
stream_iforeach
(
xs: stream(a),
fwork: cfun(int, a, void)
) : void // end-of-function
Implementation
implement
{a}(*tmp*)
stream_iforeach
(xs, fwork) =
loop(xs, 0(*i*)) where
{
//
fun
loop
(xs: stream(a), i: int): void =
(
case+ !xs of
| stream_nil() => ()
| stream_cons(x, xs) => (fwork(i, x); loop(xs, i+1))
)
//
} (* end of [stream_iforeach] *)
Description
This function is
the corresponding version of
##dyncode("list0_iforeach") on streams.
stream_iforeach_method
Interface
fun
{a:t@ype}
stream_iforeach_method
( xs: stream(INV(a)) )
( fwork: cfun(int, a, void) ): void
Implementation
implement
{a}(*tmp*)
stream_iforeach_method
(xs) =
(
lam(fwork) => stream_iforeach<a>(xs, fwork)
)
Description
This function is a curried version of
##dyncode("stream_iforeach") for supporting dot-notation.
stream_foldleft
Interface
fun{
res:t@ype
}{a:t@ype}
stream_foldleft
(
xs: stream(a),
r0: res, fopr: cfun(res, a, res)
) : res // end-of-function
Implementation
implement
{res}{a}
stream_foldleft(xs, r0, fopr) =
(
//
case+ !xs of
| stream_nil() => r0
| stream_cons(x, xs) =>
stream_foldleft<res><a>(xs, fopr(r0, x), fopr)
//
) (* end of [stream_foldleft] *)
Description
This function is
the corresponding version of
##dyncode("list0_foldleft") on streams.
int_stream_vt_from
Interface
fun
int_stream_vt_from(n: int): stream_vt(int)
Implementation
implement
int_stream_vt_from(n) =
$ldelay(stream_vt_cons(n, int_stream_vt_from(n+1)))
Description
Given an integer n, this function returns
the linear stream of integers n, n+1, n+2, etc.
stream_vt_append
Interface
fun
{a:t@ype}
stream_vt_append
(stream_vt(a), stream_vt(a)): stream_vt(a)
Implementation
implement
{a}(*tmp*)
stream_vt_append
(xs, ys) = $ldelay
(
case+ !xs of
| ~stream_vt_nil() => !ys
| ~stream_vt_cons(x, xs) =>
stream_vt_cons(x, stream_vt_append<a>(xs, ys))
, (lazy_vt_free(xs); lazy_vt_free(ys)) // for freeing
)
Description
This function is
the corresponding version of
##dyncode("list0_append") on linear streams.
stream_vt_concat
Interface
fun
{a:t@ype}
stream_vt_concat
(xss: stream_vt(stream_vt(a))): stream_vt(a)
Implementation
implement
{a}(*tmp*)
stream_vt_concat
(xss) = $ldelay
(
case+ !xss of
| ~stream_vt_nil() =>
stream_vt_nil()
| ~stream_vt_cons(xs, xss) => !
(
stream_vt_append<a>(xs, stream_vt_concat<a>(xss))
)
, lazy_vt_free(xss) // called when the stream is freed
)
Description
This function is
the corresponding version of
##dyncode("list0_concat") on linear streams.
stream_vt_takeLte
Interface
fun
{a:t@ype}
stream_vt_takeLte
(xs: stream_vt(a), n: int): stream_vt(a)
Implementation
implement
{a}(*tmp*)
stream_vt_takeLte
(xs, n) = $ldelay
(
if
n > 0
then
(
case+ !xs of
| ~stream_vt_nil() =>
stream_vt_nil()
| ~stream_vt_cons(x, xs) =>
stream_vt_cons(x, stream_vt_takeLte(xs, n-1))
)
else (~xs; stream_vt_nil((*void*)))
,
lazy_vt_free(xs) // called when the stream is freed
) (* end of [stream_vt_takeLte] *)
Description
This function is
the corresponding version of ##dyncode("list0_takeLte")
on linear streams.
stream_vt_map
Interface
fun
{a:t@ype}
{b:t@ype}
stream_vt_map
( xs: stream_vt(a)
, fopr: cfun(a, b)): stream_vt(b)
Implementation
implement
{a}{b}
stream_vt_map
(xs, fopr) = $ldelay
(
case+ !xs of
| ~stream_vt_nil() =>
stream_vt_nil()
| ~stream_vt_cons(x, xs) =>
stream_vt_cons(fopr(x), stream_vt_map<a><b>(xs, fopr))
, lazy_vt_free(xs)
)
Description
This function is
the corresponding version of
##dyncode("list0_map") on linear streams.
stream_vt_foldleft
Interface
fun{
res:t@ype
}{a:t@ype}
stream_vt_foldleft
( xs: stream_vt(a)
, r0: res, fopr: cfun(res, a, res)): res
Implementation
implement
{res}{a}
stream_vt_foldleft
(xs, r0, fopr) =
(
//
case+ !xs of
| ~stream_vt_nil() => r0
| ~stream_vt_cons(x, xs) =>
stream_vt_foldleft<res><a>(xs, fopr(r0, x), fopr)
//
) (* end of [stream_vt_foldleft] *)
Description
This function is
the corresponding version of
##dyncode("list0_foldleft") on linear streams.