2022-09-28 04:12:48 +00:00
|
|
|
module Util where
|
|
|
|
|
|
|
|
open import Agda.Builtin.Bool
|
2022-09-28 05:05:16 +00:00
|
|
|
open import Agda.Builtin.Char
|
2022-09-28 22:06:38 +00:00
|
|
|
open import Agda.Builtin.Equality
|
2022-09-28 04:12:48 +00:00
|
|
|
open import Agda.Builtin.List
|
|
|
|
open import Agda.Builtin.Maybe
|
|
|
|
open import Agda.Builtin.Nat
|
2022-09-28 05:05:16 +00:00
|
|
|
open import Agda.Builtin.String
|
2022-09-28 04:12:48 +00:00
|
|
|
|
2022-09-28 22:06:38 +00:00
|
|
|
-- given some default value and a Maybe, definitely return one of them
|
|
|
|
default : {V : Set} → V → Maybe V → V
|
|
|
|
default n nothing = n
|
|
|
|
default n (just m) = m
|
|
|
|
|
|
|
|
-- return the same thing, for functions that need a noop fn
|
2022-09-28 04:12:48 +00:00
|
|
|
ident : {V : Set} → V → V
|
|
|
|
ident x = x
|
|
|
|
|
2022-09-28 23:04:00 +00:00
|
|
|
-- transform each element of a list
|
2022-09-28 04:12:48 +00:00
|
|
|
map : {A B : Set} → (A → B) → List A → List B
|
|
|
|
map f [] = []
|
|
|
|
map f (x ∷ []) = (f x) ∷ []
|
|
|
|
map f (x ∷ xs) = (f x) ∷ (map f xs)
|
|
|
|
|
2022-09-28 23:04:00 +00:00
|
|
|
-- combine two lists
|
2022-09-28 04:12:48 +00:00
|
|
|
zip : {V : Set} → List V → List V → List V
|
|
|
|
zip [] [] = []
|
2022-09-28 23:04:00 +00:00
|
|
|
zip xs [] = xs
|
|
|
|
zip [] ys = ys
|
2022-09-28 04:12:48 +00:00
|
|
|
zip (x ∷ xs) (y ∷ ys) = x ∷ y ∷ (zip xs ys)
|
|
|
|
|
2022-09-28 23:04:00 +00:00
|
|
|
-- get the length of a list
|
2022-09-28 04:12:48 +00:00
|
|
|
len : {A : Set} → List A → Nat
|
|
|
|
len [] = 0
|
|
|
|
len (x ∷ xs) = suc (len xs)
|
|
|
|
|
2022-09-28 23:04:00 +00:00
|
|
|
-- keep items that pass the predicate
|
2022-09-28 04:12:48 +00:00
|
|
|
filter : {A : Set} → (A → Bool) → List A → List A
|
|
|
|
filter f [] = []
|
|
|
|
filter f (x ∷ xs) with f x
|
|
|
|
... | true = x ∷ filter f xs
|
|
|
|
... | false = filter f xs
|
|
|
|
|
2022-09-28 23:04:00 +00:00
|
|
|
-- remove nothings from a list of maybes
|
2022-09-28 04:12:48 +00:00
|
|
|
filterNothing : {A : Set} → List (Maybe A) → List A
|
|
|
|
filterNothing [] = []
|
|
|
|
filterNothing (nothing ∷ xs) = filterNothing xs
|
|
|
|
filterNothing (just x ∷ xs) = x ∷ filterNothing xs
|
|
|
|
|
2022-09-28 23:04:00 +00:00
|
|
|
-- take the item at the given index
|
2022-09-28 04:12:48 +00:00
|
|
|
takeIndex : {A : Set} → A → List A → Nat → A
|
2022-10-01 01:24:38 +00:00
|
|
|
takeIndex d [] _ = d
|
2022-09-28 04:12:48 +00:00
|
|
|
takeIndex d (x ∷ xs) 0 = x
|
|
|
|
takeIndex d (x ∷ xs) (suc n) = takeIndex d xs n
|
2022-09-28 05:05:16 +00:00
|
|
|
|
2022-09-28 23:04:00 +00:00
|
|
|
-- find the index of a number
|
2022-09-28 05:05:16 +00:00
|
|
|
findIndex : Nat → Nat → List Nat → Maybe Nat
|
|
|
|
findIndex n t [] = nothing
|
|
|
|
findIndex n t (x ∷ xs) with (t == x)
|
|
|
|
... | true = just n
|
|
|
|
... | false = findIndex (suc n) t xs
|
|
|
|
|
2022-09-28 23:04:00 +00:00
|
|
|
-- find the index of a character
|
2022-09-28 05:05:16 +00:00
|
|
|
findCharIndex : Nat → Char → List Char → Maybe Nat
|
|
|
|
findCharIndex n t [] = nothing
|
|
|
|
findCharIndex n t (x ∷ xs) with primCharEquality t x
|
|
|
|
... | true = just n
|
|
|
|
... | false = findCharIndex (suc n) t xs
|
|
|
|
|
2022-09-28 23:04:00 +00:00
|
|
|
-- generic find if I was smarter
|
2022-09-30 04:07:05 +00:00
|
|
|
{-
|
|
|
|
findAny : { A : Set } → Nat → A → List A → Nat
|
|
|
|
findAny n t [] = n
|
|
|
|
findAny n t (x ∷ xs) with (t ≡ x)
|
|
|
|
... | refl = n
|
|
|
|
... | _ = findAny (suc n) t xs
|
|
|
|
-}
|
2022-09-28 05:05:16 +00:00
|
|
|
|
2022-09-28 22:06:38 +00:00
|
|
|
split : List Char → List Char → List (List Char)
|
|
|
|
split = go [] []
|
|
|
|
where
|
|
|
|
go : List Char → List (List Char) → List Char → List Char → List (List Char)
|
2022-09-28 23:04:00 +00:00
|
|
|
go acc acl delims [] = acc ∷ acl
|
2022-09-28 22:32:35 +00:00
|
|
|
go acc acl delims (x ∷ xs) with findCharIndex 0 x delims
|
|
|
|
... | nothing = go (x ∷ acc) acl delims xs
|
|
|
|
... | _ = go [] (acc ∷ acl) delims xs
|