1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-17 16:47:22 +03:00
mal/impls/sml/util.sml
2021-05-02 17:35:40 -05:00

41 lines
1.2 KiB
Standard ML

fun takeWhile f xs = takeWhile' f [] xs
and takeWhile' f acc [] = rev acc
| takeWhile' f acc (x::xs) = if f x then takeWhile' f (x::acc) xs else rev acc
infix 3 |> fun x |> f = f x
fun eq a b = a = b
fun optOrElse NONE b = b ()
| optOrElse a _ = a
fun valOrElse (SOME x) _ = x
| valOrElse a b = b ()
fun optIfNone b NONE = b ()
| optIfNone _ a = a
fun valIfNone _ (SOME a) = a
| valIfNone b _ = b ()
fun interleave (x::xs) (y::ys) = x :: y :: interleave xs ys
| interleave [] ys = ys
| interleave xs [] = xs
fun identity x = x
fun triml k s = String.extract (s, k, NONE)
fun malEscape s = String.translate (fn #"\"" => "\\\""
| #"\n" => "\\n"
| #"\\" => "\\\\"
| c => String.str c) s
fun malUnescape s = malUnescape' (String.explode s)
and malUnescape' (#"\\"::(#"\""::rest)) = "\"" ^ malUnescape' rest
| malUnescape' (#"\\"::(#"n" ::rest)) = "\n" ^ malUnescape' rest
| malUnescape' (#"\\"::(#"\\"::rest)) = "\\" ^ malUnescape' rest
| malUnescape' (c::rest) = (String.str c) ^ malUnescape' rest
| malUnescape' ([]) = ""