Skip to content

Commit

Permalink
Make it run in chez-lift
Browse files Browse the repository at this point in the history
  • Loading branch information
marzipankaiser committed Oct 20, 2023
1 parent 70af3c0 commit 1dfbc7d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
16 changes: 16 additions & 0 deletions libraries/chez/common/immutable/list.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,23 @@ def tail[A](l: List[A]): List[A] = l match {
case Cons(a, rest) => rest
}

def last[A](l: List[A]): Option[A] = {
var res = None()
l.foreach { e => res = Some(e) }
res
}

def headOption[A](l: List[A]): Option[A] = l match {
case Nil() => None()
case Cons(a, rest) => Some(a)
}

def any(l: List[Boolean]): Boolean = l match {
case Nil() => false
case Cons(v,rest) => if (v) { true } else { any(rest) }
}
def sum(l: List[Int]): Int = {
var res = 0
l.foreach { i => res = res + i }
res
}
50 changes: 46 additions & 4 deletions libraries/chez/common/text/string.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module text/string

import immutable/option

// def charAt(str: String, index: Int): Option[String] =
// str.unsafeCharAt(index).undefinedToOption
def charAt(str: String, index: Int): Option[String] =
str.unsafeCharAt(index).undefinedToOption

extern pure def length(str: String): Int =
"(string-length str)"
Expand All @@ -14,15 +14,57 @@ extern pure def repeat(str: String, n: Int): String =
extern pure def substring(str: String, from: Int): String =
"(substring str from (string-length str))"

extern pure def substring(str: String, from: Int, to: Int): String =
"(substring str from to)"

// extern pure def trim(str: String): String =
// "str.trim()"

def toInt(str: String): Option[Int] =
str.unsafeToInt.undefinedToOption

// extern pure def unsafeCharAt(str: String, n: Int): String =
// "str[n]"
extern pure def unsafeCharAt(str: String, n: Int): String =
"(string (string-ref str n))"

// returns #f if not a number
extern pure def unsafeToInt(str: String): Int =
"(string->number str)"

def takeWhile(str: String){ f: String => Boolean }: String = {
def rec(i: Int, acc: String): String = {
if (i < length(str)) {
val c = unsafeCharAt(str, i)
if (f(c)) {
rec(i+1, acc ++ c)
} else acc
} else acc
}
rec(0, "")
}
def map[A](str: String){ f: String => A }: List[A] = {
def rec(i: Int): List[A] = {
if (i < length(str)) {
Cons(f(unsafeCharAt(str, i)), rec(i+1))
} else Nil()
}
rec(0)
}
def split(str: String, sep: String): List[String] = {
def rec(startcheck: Int, startcopy: Int): List[String] = {
if (length(str) < startcheck + length(sep)) {
Cons(substring(str, startcopy, length(str)), Nil())
} else {
if(substring(str, startcheck, startcheck + length(sep)) == sep) {
Cons(substring(str, startcopy, startcheck),
rec(startcheck + length(sep), startcheck + length(sep)))
} else {
rec(startcheck + 1, startcopy)
}
}
}
if (sep == "") {
map(str){ c => c }
} else {
rec(0,0)
}
}

0 comments on commit 1dfbc7d

Please sign in to comment.