Add cartProd to util
This commit is contained in:
parent
fde322e772
commit
2c012c57db
21
src/Util.md
21
src/Util.md
|
@ -7,6 +7,7 @@ module Util
|
||||||
|
|
||||||
import Data.SortedSet
|
import Data.SortedSet
|
||||||
import Data.String
|
import Data.String
|
||||||
|
import Data.List.Lazy
|
||||||
|
|
||||||
%default total
|
%default total
|
||||||
```
|
```
|
||||||
|
@ -98,3 +99,23 @@ We first check to see if the needle is a prefix of the top level haystack, befor
|
||||||
then True
|
then True
|
||||||
else isSubstr' str | x
|
else isSubstr' str | x
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Lazy List
|
||||||
|
|
||||||
|
### Cartesian product
|
||||||
|
|
||||||
|
Lazily take the cartesian product of two foldables
|
||||||
|
|
||||||
|
```idris
|
||||||
|
export
|
||||||
|
cartProd : Foldable a => Foldable b => a e -> b f -> LazyList (e, f)
|
||||||
|
cartProd x y =
|
||||||
|
let y = foldToLazy y
|
||||||
|
in foldr (\e, acc => combine e y acc) [] x
|
||||||
|
where
|
||||||
|
foldToLazy : Foldable a' => a' e' -> LazyList e'
|
||||||
|
foldToLazy x = foldr (\e, acc => e :: acc) [] x
|
||||||
|
combine : e -> LazyList f -> LazyList (e, f) -> LazyList (e, f)
|
||||||
|
combine x [] rest = rest
|
||||||
|
combine x (y :: ys) rest = (x, y) :: combine x ys rest
|
||||||
|
```
|
||||||
|
|
Loading…
Reference in a new issue