seq¶
In [ ]:
//// test
open testing
seq¶
seq¶
In [ ]:
type seq dim el = dim -> option el
try_item¶
In [ ]:
inl try_item n s =
n |> s
from_list¶
In [ ]:
inl from_list list =
fun n =>
list
|> listm'.try_item n
In [ ]:
//// test
listm.init 10i32 print_and_return
|> from_list
|> try_item 5i32
|> _assert_eq (Some 5i32)
print_and_return / x: 0 print_and_return / x: 1 print_and_return / x: 2 print_and_return / x: 3 print_and_return / x: 4 print_and_return / x: 5 print_and_return / x: 6 print_and_return / x: 7 print_and_return / x: 8 print_and_return / x: 9 __assert_eq / actual: US0_0 5 / expected: US0_0 5
map¶
In [ ]:
inl map fn s =
fun n =>
n
|> s
|> optionm.map fn
In [ ]:
//// test
listm.init 10i32 id
|> from_list
|> map ((*) 2)
|> try_item 5i32
|> _assert_eq (Some 10i32)
__assert_eq / actual: US0_0 10 / expected: US0_0 10
mapi¶
In [ ]:
inl mapi fn s =
fun n =>
n
|> s
|> optionm.map (fn n)
In [ ]:
//// test
listm.init 10i32 print_and_return
|> from_list
|> mapi fun i x => i + x
|> try_item 5i32
|> _assert_eq (Some 10i32)
print_and_return / x: 0 print_and_return / x: 1 print_and_return / x: 2 print_and_return / x: 3 print_and_return / x: 4 print_and_return / x: 5 print_and_return / x: 6 print_and_return / x: 7 print_and_return / x: 8 print_and_return / x: 9 __assert_eq / actual: US0_0 10 / expected: US0_0 10
choose¶
In [ ]:
inl choose forall dim {number} t u. (fn : t -> option u) (s : seq dim t) : seq dim u =
fun n =>
inl rec body fn s i i' =
match i |> s with
| None => None
| Some x =>
match x |> fn with
| Some x when n = i' => Some x
| Some _ => loop (i + 1) (i' + 1)
| _ => loop (i + 1) i'
and inl loop i i' =
if n |> var_is |> not
then body fn s i i'
else
inl fn = join fn
inl s = join s
join body fn s i i'
loop 0 0
In [ ]:
//// test
listm.init 10i32 print_and_return
|> from_list
|> choose (fun x => if x % 2 = 0 then Some x else None)
|> try_item 1i32
|> _assert_eq (Some 2i32)
print_and_return / x: 0 print_and_return / x: 1 print_and_return / x: 2 print_and_return / x: 3 print_and_return / x: 4 print_and_return / x: 5 print_and_return / x: 6 print_and_return / x: 7 print_and_return / x: 8 print_and_return / x: 9 __assert_eq / actual: US0_0 2 / expected: US0_0 2
indexed¶
In [ ]:
inl indexed s =
s
|> mapi fun i x =>
i, x
In [ ]:
//// test
listm.init 10i32 ((*) 2)
|> from_list
|> indexed
|> try_item 5i32
|> _assert_eq (Some (5i32, 10i32))
__assert_eq / actual: US0_0 (5, 10) / expected: US0_0 (5, 10)
zip¶
In [ ]:
inl zip n seq1 seq2 =
seq1 n, seq2 n
In [ ]:
//// test
((listm.init 10i32 id |> from_list), (listm.init 10i32 ((*) 2) |> from_list))
||> zip 5i32
|> _assert_eq (Some 5, Some 10)
__assert_eq / actual: struct (US0_0 5, US0_0 10) / expected: struct (US0_0 5, US0_0 10)
zip_with¶
In [ ]:
inl zip_with fn seq1 seq2 =
fun n =>
fn (seq1 n) (seq2 n)
In [ ]:
//// test
((listm.init 10i32 id |> from_list), (listm.init 10i32 ((*) 2) |> from_list))
||> zip_with (optionm'.choose (+))
|> try_item 2i32
|> _assert_eq (Some 6)
__assert_eq / actual: US0_0 6 / expected: US0_0 6
fold¶
In [ ]:
inl fold fn init seq =
inl rec loop acc n =
match seq n with
| Some x => loop (fn acc x) (n + 1)
| None => acc
loop init 0
inl fold_ fn init seq =
let rec loop acc n =
match seq n with
| Some x => loop (fn acc x) (n + 1)
| None => acc
loop init 0
sum¶
In [ ]:
inl sum seq =
seq |> fold (+) 0
inl sum_ seq =
seq |> fold_ (+) 0
In [ ]:
//// test
listm.init 10i32 id
|> from_list
|> fun f (n : i32) => f n
|> sum
|> _assert_eq 45
__assert_eq / actual: 45 / expected: 45
to_list¶
In [ ]:
inl to_list seq =
seq
|> fold (fun acc x => x :: acc) []
|> listm.rev
inl to_list_ seq =
seq
|> fold_ (fun acc x => x :: acc) []
|> listm.rev
In [ ]:
//// test
listm.init 10i32 id
|> from_list
|> fun f (n : i32) => f n
|> to_list
|> _assert_eq (listm.init 10i32 id)
__assert_eq / actual: UH0_1 (0, UH0_1 (1, UH0_1 (2, UH0_1 (3, UH0_1 (4, UH0_1 (5, UH0_1 (6, UH0_1 (7, UH0_1 (8, UH0_1 (9, UH0_0)))))))))) / expected: UH0_1 (0, UH0_1 (1, UH0_1 (2, UH0_1 (3, UH0_1 (4, UH0_1 (5, UH0_1 (6, UH0_1 (7, UH0_1 (8, UH0_1 (9, UH0_0))))))))))
from_array¶
In [ ]:
inl from_array forall dim {number; int} el. (array : a dim el) : seq dim el =
fun n =>
if n >= length array
then None
else index array n |> Some
In [ ]:
//// test
a ;[ 1; 2; 3 ]
|> from_array
|> try_item 1i32
|> _assert_eq (Some 2i32)
__assert_eq / actual: US0_0 2 / expected: US0_0 2
to_array¶
In [ ]:
inl to_array seq =
inl ar = a ;[] |> mut
((), seq)
||> fold fun _ x =>
ar <- *ar ++ a ;[x]
*ar
inl to_array_ seq =
inl ar = a ;[] |> mut
((), seq)
||> fold_ fun _ x =>
ar <- *ar ++ a ;[x]
*ar
In [ ]:
//// test
listm.init 10i32 id
|> from_list
|> fun (x : i32 -> _) => x
|> to_array
|> _assert_eq (a ;[ 0; 1; 2; 3; 4; 5; 6; 7; 8; 9 ] : _ i32 _)
__assert_eq / actual: [|0; 1; 2; 3; 4; 5; 6; 7; 8; 9|] / expected: [|0; 1; 2; 3; 4; 5; 6; 7; 8; 9|]
take_while¶
In [ ]:
inl take_while cond seq =
inl rec loop acc i =
match seq i with
| Some st when cond st i => loop (st :: acc) (i + 1)
| _ => acc |> listm.rev
loop [] 0
In [ ]:
//// test
listm.init 10i32 id
|> from_list
|> take_while (fun n (_ : i32) => n < 5)
|> listm'.sum
|> _assert_eq 10
__assert_eq / actual: 10 / expected: 10
In [ ]:
//// test
stream.new_finite_stream print_and_return 10i32
|> flip stream.try_item
|> take_while (fun n (_ : i32) => n < 5)
|> listm'.sum
|> _assert_eq 10
print_and_return / x: 0 print_and_return / x: 1 print_and_return / x: 1 print_and_return / x: 2 print_and_return / x: 1 print_and_return / x: 2 print_and_return / x: 3 print_and_return / x: 1 print_and_return / x: 2 print_and_return / x: 3 print_and_return / x: 4 print_and_return / x: 1 print_and_return / x: 2 print_and_return / x: 3 print_and_return / x: 4 print_and_return / x: 5 __assert_eq / actual: 10 / expected: 10
take_while_¶
In [ ]:
inl take_while_ cond seq =
let rec loop acc i =
match seq i with
| Some st when cond st i => loop (st :: acc) (i + 1)
| _ => acc |> listm.rev
loop [] 0
In [ ]:
//// test
stream.new_infinite_stream_ print_and_return
|> flip stream.try_item
|> take_while_ (fun n (_ : i32) => n < 5i32)
|> listm'.sum
|> _assert_eq 10
print_and_return / x: 0 print_and_return / x: 1 print_and_return / x: 1 print_and_return / x: 2 print_and_return / x: 1 print_and_return / x: 2 print_and_return / x: 3 print_and_return / x: 1 print_and_return / x: 2 print_and_return / x: 3 print_and_return / x: 4 print_and_return / x: 1 print_and_return / x: 2 print_and_return / x: 3 print_and_return / x: 4 print_and_return / x: 5 __assert_eq / actual: 10 / expected: 10
In [ ]:
//// test
stream.new_infinite_stream_ print_and_return
|> stream.memoize
|> fun list =>
inl list = list ()
fun n =>
list |> stream.try_item n
|> take_while_ (fun n (_ : i32) => n < 5i32)
|> listm'.sum
|> _assert_eq 10
print_and_return / x: 0 print_and_return / x: 1 print_and_return / x: 2 print_and_return / x: 3 print_and_return / x: 4 print_and_return / x: 5 __assert_eq / actual: 10 / expected: 10
In [ ]:
//// test
stream.new_finite_stream print_and_return 10i32
|> stream.memoize
|> fun list =>
inl list = list ()
fun n =>
list |> stream.try_item n
|> take_while_ (fun n (_ : i32) => n < 5)
|> listm'.sum
|> _assert_eq 10
print_and_return / x: 0 print_and_return / x: 1 print_and_return / x: 2 print_and_return / x: 3 print_and_return / x: 4 print_and_return / x: 5 __assert_eq / actual: 10 / expected: 10
memoize¶
In [ ]:
inl memoize seq =
inl state = mut []
fun n =>
match *state |> listm'.try_find (fun (n', _) => n' = n) with
| Some (_, v) => v
| None =>
inl new_state = seq n
state <- (n, new_state) :: *state
new_state
inl memoize_ seq =
inl state = mut []
fun n =>
match *state |> listm'.try_find_ (fun (n', _) => n' = n) with
| Some (_, v) => v
| None =>
inl new_state = seq n
state <- (n, new_state) :: *state
new_state
In [ ]:
//// test
inl seq =
fun n =>
n |> print_and_return |> Some
|> memoize_
seq
|> take_while_ (fun n (_ : i32) => n < 5)
|> listm'.sum
|> _assert_eq 10
seq
|> take_while_ (fun n _ => n < 5)
|> listm'.sum
|> _assert_eq 10
print_and_return / x: 0 print_and_return / x: 1 print_and_return / x: 2 print_and_return / x: 3 print_and_return / x: 4 print_and_return / x: 5 __assert_eq / actual: 10 / expected: 10 __assert_eq / actual: 10 / expected: 10
iterate¶
In [ ]:
inl iterate f x0 num_steps =
inl rec loop x n =
if n <= 0
then x
else loop (f x) (n - 1)
loop x0 num_steps
In [ ]:
//// test
10i32 |> iterate ((*) 2) 1i32
|> _assert_eq 1024
__assert_eq / actual: 1024 / expected: 1024
In [ ]:
inl iterate_ f x0 num_steps =
let rec loop x n =
if n <= 0
then x
else loop (f x) (n - 1)
loop x0 num_steps
In [ ]:
//// test
10i32 |> iterate_ ((*) 2) 1i32
|> _assert_eq 1024
__assert_eq / actual: 1024 / expected: 1024
In [ ]:
inl iterate' f x0 num_steps =
listm.init num_steps id
|> listm.fold (fun x _ => f x) x0
In [ ]:
//// test
10i32 |> iterate' ((*) 2) 1i32
|> _assert_eq 1024
__assert_eq / actual: 1024 / expected: 1024
find_last¶
In [ ]:
inl find_last forall item result. fold_fn fn target : option result =
fold_fn (fun (item : item) (result : option result) =>
match result with
| None => fn item
| result => result
) target (None : option result)
inl array_find_last forall item result. (fn : item -> option result) (target : a i32 item) : option result =
find_last am.foldBack fn target
inl list_find_last forall item result. (fn : item -> option result) (target : list item) : option result =
find_last listm.foldBack fn target
fsharp¶
seq'¶
In [ ]:
nominal seq' t = $"backend_switch `({ Fsharp : $'`t seq'; Python : $'list' })"
length'¶
In [ ]:
inl length' forall t. (items : seq' t) : int =
backend_switch {
Fsharp = fun () => items |> $'Seq.length' : int
Python = fun () => $'len(!items)' : int
}
to_list'¶
In [ ]:
inl to_list' forall t. (items : seq' t) : listm'.list' t =
backend_switch {
Fsharp = fun () => items |> $'Seq.toList' : listm'.list' t
Python = fun () => $'!items ' : listm'.list' t
}
new_seq¶
In [ ]:
inl new_seq forall t. fn : seq' t =
backend_switch {
Fsharp = fun () =>
fun () =>
$'seq {'
fn |> indent
$'}' : ()
|> let'
|> fun x => x : seq' t
Python = fun () =>
$'list(!fn())' : seq' t
}
In [ ]:
//// test
///! fsharp
///! cuda
fun () =>
"a" |> yield
"b" |> yield
|> new_seq
|> to_list'
|> listm'.unbox
|> _assert_eq [ "a"; "b" ]
.py output (Cuda): __assert_eq / actual: UH0_1(v0='a', v1=UH0_1(v0='b', v1=UH0_0())) / expected: UH0_1(v0='a', v1=UH0_1(v0='b', v1=UH0_0()))
.fsx output: __assert_eq / actual: UH0_1 ("a", UH0_1 ("b", UH0_0)) / expected: UH0_1 ("a", UH0_1 ("b", UH0_0))
of_array'¶
In [ ]:
inl of_array' forall dim t. (items : a dim t) : seq' t =
backend_switch {
Fsharp = fun () =>
fun () =>
$'for i = 0 to !items.Length - 1 do yield !items.[i]'
|> new_seq
|> fun x => x : seq' t
Python = fun () => $'[item for item in !items]' : seq' t
}
In [ ]:
//// test
///! fsharp
///! cuda
///! rust
///! typescript
///! python
(a ;[ "a"; "b" ] : _ int _)
|> of_array'
|> to_list'
|> listm'.unbox
|> _assert_eq [ "a"; "b" ]
.py output (Cuda): __assert_eq / actual: UH0_1(v0='a', v1=UH0_1(v0='b', v1=UH0_0())) / expected: UH0_1(v0='a', v1=UH0_1(v0='b', v1=UH0_0())) .rs output: __assert_eq / actual: UH0_1("a", UH0_1("b", UH0_0)) / expected: UH0_1("a", UH0_1("b", UH0_0)) .ts output: __assert_eq / actual: UH0_1 (a, UH0_1 (b, UH0_0)) / expected: UH0_1 (a, UH0_1 (b, UH0_0)) .py output: __assert_eq / actual: UH0_1 ("a", UH0_1 ("b", UH0_0)) / expected: UH0_1 ("a", UH0_1 ("b", UH0_0))
.fsx output: __assert_eq / actual: UH0_1 ("a", UH0_1 ("b", UH0_0)) / expected: UH0_1 ("a", UH0_1 ("b", UH0_0))
of_array¶
In [ ]:
inl of_array forall dim t. (items : a dim t) : seq' t =
backend_switch {
Fsharp = fun () => $'!items |> Seq.ofArray' : seq' t
Python = fun () => $'list(iter(!items))' : seq' t
}
In [ ]:
//// test
///! fsharp
///! cuda
///! rust
///! typescript
///! python
(a ;[ "a"; "b" ] : _ int _)
|> of_array
|> to_list'
|> listm'.unbox
|> _assert_eq [ "a"; "b" ]
.py output (Cuda): __assert_eq / actual: UH0_1(v0='a', v1=UH0_1(v0='b', v1=UH0_0())) / expected: UH0_1(v0='a', v1=UH0_1(v0='b', v1=UH0_0())) .rs output: __assert_eq / actual: UH0_1("a", UH0_1("b", UH0_0)) / expected: UH0_1("a", UH0_1("b", UH0_0)) .ts output: __assert_eq / actual: UH0_1 (a, UH0_1 (b, UH0_0)) / expected: UH0_1 (a, UH0_1 (b, UH0_0)) .py output: __assert_eq / actual: UH0_1 ("a", UH0_1 ("b", UH0_0)) / expected: UH0_1 ("a", UH0_1 ("b", UH0_0))
.fsx output: __assert_eq / actual: UH0_1 ("a", UH0_1 ("b", UH0_0)) / expected: UH0_1 ("a", UH0_1 ("b", UH0_0))
of_array_base¶
In [ ]:
inl of_array_base forall t. (items : array_base t) : seq' t =
a items
|> fun x => x : _ int _
|> of_array
In [ ]:
//// test
///! fsharp
///! cuda
///! rust
///! typescript
///! python
;[ "a"; "b" ]
|> of_array_base
|> to_list'
|> listm'.unbox
|> _assert_eq [ "a"; "b" ]
.py output (Cuda): __assert_eq / actual: UH0_1(v0='a', v1=UH0_1(v0='b', v1=UH0_0())) / expected: UH0_1(v0='a', v1=UH0_1(v0='b', v1=UH0_0())) .rs output: __assert_eq / actual: UH0_1("a", UH0_1("b", UH0_0)) / expected: UH0_1("a", UH0_1("b", UH0_0)) .ts output: __assert_eq / actual: UH0_1 (a, UH0_1 (b, UH0_0)) / expected: UH0_1 (a, UH0_1 (b, UH0_0)) .py output: __assert_eq / actual: UH0_1 ("a", UH0_1 ("b", UH0_0)) / expected: UH0_1 ("a", UH0_1 ("b", UH0_0))
.fsx output: __assert_eq / actual: UH0_1 ("a", UH0_1 ("b", UH0_0)) / expected: UH0_1 ("a", UH0_1 ("b", UH0_0))
to_array'¶
In [ ]:
inl to_array' forall dim t. (items : seq' t) : a dim t =
backend_switch {
Fsharp = fun () => items |> $'Seq.toArray' : a dim t
Python = fun () => $'(cp if cuda else np).array(!items)' : a dim t
}
In [ ]:
//// test
///! fsharp
///! cuda
///! rust
///! typescript
///! python
;[ "a"; "b" ]
|> of_array_base
|> to_array'
|> fun x => x : _ int _
|> am'.to_list'
|> listm'.unbox
|> _assert_eq [ "a"; "b" ]
.py output (Cuda): __assert_eq / actual: UH0_1(v0=np.str_('a'), v1=UH0_1(v0=np.str_('b'), v1=UH0_0())) / expected: UH0_1(v0='a', v1=UH0_1(v0='b', v1=UH0_0())) .rs output: __assert_eq / actual: UH0_1("a", UH0_1("b", UH0_0)) / expected: UH0_1("a", UH0_1("b", UH0_0)) .ts output: __assert_eq / actual: UH0_1 (a, UH0_1 (b, UH0_0)) / expected: UH0_1 (a, UH0_1 (b, UH0_0)) .py output: __assert_eq / actual: UH0_1 ("a", UH0_1 ("b", UH0_0)) / expected: UH0_1 ("a", UH0_1 ("b", UH0_0))
.fsx output: __assert_eq / actual: UH0_1 ("a", UH0_1 ("b", UH0_0)) / expected: UH0_1 ("a", UH0_1 ("b", UH0_0))
of_list'¶
In [ ]:
inl of_list' forall t. (items : listm'.list' t) : seq' t =
backend_switch {
Fsharp = fun () =>
fun () =>
$'for i = 0 to !items.Length - 1 do yield !items.[i]'
|> new_seq
|> fun x => x : seq' t
Python = fun () => $'!items ' : seq' t
}
cast'¶
In [ ]:
inl cast' forall t u. (items : u) : seq' t =
backend_switch {
Fsharp = fun () => items |> $'Seq.cast' : seq' t
Python = fun () => $'list(!items)' : seq' t
}
rev'¶
In [ ]:
inl rev'' forall t. (items : seq' t) : seq' t =
backend_switch {
Fsharp = fun () => items |> $'Seq.rev' : seq' t
Python = fun () => $'list(reversed(!items))' : seq' t
}
In [ ]:
//// test
///! fsharp
///! cuda
///! rust
///! typescript
///! python
[ "a"; "b" ]
|> listm'.box
|> of_list'
|> rev''
|> to_list'
|> listm'.unbox
|> _assert_eq [ "b"; "a" ]
.py output (Cuda): __assert_eq / actual: UH0_1(v0='b', v1=UH0_1(v0='a', v1=UH0_0())) / expected: UH0_1(v0='b', v1=UH0_1(v0='a', v1=UH0_0())) .rs output: __assert_eq / actual: UH0_1("b", UH0_1("a", UH0_0)) / expected: UH0_1("b", UH0_1("a", UH0_0)) .ts output: __assert_eq / actual: UH0_1 (b, UH0_1 (a, UH0_0)) / expected: UH0_1 (b, UH0_1 (a, UH0_0)) .py output: __assert_eq / actual: UH0_1 ("b", UH0_1 ("a", UH0_0)) / expected: UH0_1 ("b", UH0_1 ("a", UH0_0))
.fsx output: __assert_eq / actual: UH0_1 ("b", UH0_1 ("a", UH0_0)) / expected: UH0_1 ("b", UH0_1 ("a", UH0_0))
rust¶
fuse¶
In [ ]:
nominal fuse t =
`(
global "#if FABLE_COMPILER\n[<Fable.Core.Erase; Fable.Core.Emit(\"core::iter::Fuse<$0>\")>]\n#endif\ntype core_iter_Fuse<'T> = class end"
$'' : $'core_iter_Fuse<`t>'
)