Last active
February 7, 2026 22:06
-
-
Save thedeemon/9c04bd98f8c2aae2175801163baf728e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Maybe a = Some { value : a } | None | |
| class Conv a b { | |
| $ : a -> b | |
| } | |
| instance Conv a String where Show a { | |
| $ = show | |
| } | |
| instance Conv Int Float { | |
| $ = floatOfInt | |
| } | |
| fst (a, b) = a | |
| snd (a, b) = b | |
| != a b = !(a==b) | |
| print x = puts (show x) | |
| println x = do | |
| puts (show x) | |
| puts "\n" | |
| instance Show (Maybe a) where Show a { | |
| show (Some x) = "Some " ~ show x | |
| show None = "None" | |
| } | |
| type Result t e = Ok { value: t } | Err { error: e } | |
| instance Conv (Result t e) (Maybe t) { | |
| $ (Ok x) = Some x | |
| $ (Err e) = None | |
| } | |
| class OrElse m -> a { | |
| ?? : m -> a -> a | |
| } | |
| instance OrElse (Maybe t) t { | |
| ?? (Some x) v = x | |
| ?? None v = v | |
| } | |
| instance OrElse (Result t e) t { | |
| ?? (Ok x) v = x | |
| ?? (Err _) v = v | |
| } | |
| class Iterator it -> a { | |
| next : it -> Maybe a | |
| } | |
| type IotaIter { | |
| mut cur : Int | |
| end : Int | |
| } | |
| instance Iterator IotaIter Int { | |
| next it = | |
| if it.cur < it.end then do | |
| x = it.cur | |
| it.cur := x + 1 | |
| Some x | |
| else None | |
| } | |
| iota n = IotaIter 0 n | |
| type VectorIter e { | |
| arr : Vector e | |
| mut pos : Int | |
| } | |
| instance Iterator (VectorIter e) e { | |
| next it = | |
| if it.pos < length it.arr then do | |
| x = it.arr[it.pos] | |
| it.pos := it.pos + 1 | |
| Some x | |
| else None | |
| } | |
| type ArrayIter e { | |
| arr : Array e | |
| mut pos : Int | |
| } | |
| instance Iterator (ArrayIter e) e { | |
| next it = | |
| if it.pos < length it.arr then do | |
| x = it.arr[it.pos] | |
| it.pos := it.pos + 1 | |
| Some x | |
| else None | |
| } | |
| class Sequence s -> e it where Iterator it e { | |
| getIterator : s -> it | |
| } | |
| instance Sequence (Vector t) t (VectorIter t) { | |
| getIterator arr = VectorIter arr 0 | |
| } | |
| instance Sequence (Array t) t (ArrayIter t) { | |
| getIterator arr = ArrayIter arr 0 | |
| } | |
| instance Sequence t e t where Iterator t e { | |
| getIterator it = it | |
| } | |
| type Filter iter e { | |
| orig : iter | |
| pred : e -> Bool | |
| } | |
| instance Iterator (Filter r e) e where Iterator r e { | |
| next f = | |
| match (next f.orig) { | |
| | None => None | |
| | (Some x) => if f.pred x then Some x else next f | |
| } | |
| } | |
| filter : (elt -> Bool) -> seq -> Filter it elt where Sequence seq elt it | |
| filter p seq = Filter (getIterator seq) p | |
| type MapResult iter a b { | |
| orig : iter | |
| func : a -> b | |
| } | |
| instance Iterator (MapResult r a b) b where Iterator r a { | |
| next m = | |
| match (next m.orig) { | |
| | None => None | |
| | (Some x) => Some (m.func x) | |
| } | |
| } | |
| map : (a -> b) -> seq -> MapResult it a b where Sequence seq a it | |
| map f seq = MapResult (getIterator seq) f | |
| instance HasLength (MapResult r a b) where HasLength r { | |
| length m = length m.orig | |
| } | |
| instance HasLength (VectorIter e) { | |
| length it = length it.arr | |
| } | |
| instance HasLength IotaIter { | |
| length it = it.end - it.cur | |
| } | |
| instance OpIndex IotaIter Int Int { | |
| opIndex iot idx = iot.cur + idx | |
| } | |
| instance OpIndex (MapResult r a b) Int b where OpIndex r Int a { | |
| opIndex m i = m.func m.orig[i] | |
| } | |
| instance OpIndex (VectorIter e) Int e { | |
| opIndex it i = it.arr[it.pos + i] | |
| } | |
| eachIter f it = | |
| match (next it) { | |
| | (Some x) => {f x; eachIter f it} | |
| | None => () | |
| } | |
| each f xs = eachIter f (getIterator xs) | |
| class Zero a { zero : a } | |
| instance Zero Int { zero = 0 } | |
| instance Zero Float { zero = 0.0 } | |
| sumIt it s = | |
| match (next it) { | |
| | None => s | |
| | (Some x) => sumIt it (s+x) | |
| } | |
| sum xs = sumIt (getIterator xs) zero | |
| max a b = if a > b then a else b | |
| showIt it notFirst = | |
| match (next it) { | |
| | None => "]" | |
| | (Some x) => (if notFirst then ", " ~ show x else show x) ~ showIt it True | |
| } | |
| showSeq xs = "[" ~ showIt (getIterator xs) False | |
| instance Show (Vector a) where Show a { show = showSeq } | |
| instance Show (Array a) where Show a { show = showSeq } | |
| type Buf a { | |
| mut buf : Array a | |
| mut count : Int | |
| } | |
| arrToVec xs = makeVector (length xs) { i => xs[i] } | |
| vecToArr xs = makeArray (length xs) { i => xs[i] } | |
| newBuf x = Buf (makeArray 1 {i => x}) 1 | |
| addToBuf b x = do | |
| len = length b.buf | |
| if b.count == len then | |
| b.buf := makeArray (len * 2) { i => if i < len then b.buf[i] else x } | |
| b.buf[b.count] := x | |
| b.count := b.count + 1 | |
| toVector : it -> Vector a where Iterator it a | |
| toVector it = do | |
| toVectorLoop buff = | |
| match (next it) { | |
| | (Some x) => { addToBuf buff x; toVectorLoop buff} | |
| | None => makeVector (buff.count) {i => buff.buf[i]} | |
| } | |
| match (next it) { | |
| | (Some x) => toVectorLoop (newBuf x) | |
| | None => [] | |
| } | |
| type Flatten itOfIt it { | |
| org : itOfIt | |
| mut cur : Maybe it | |
| } | |
| nextFlat : (Flatten it of) -> Maybe e where Iterator of e, Iterator it of | |
| nextFlat fl = | |
| match fl.cur { | |
| | (Some xs) => match (next xs) { | |
| | (Some x) => Some x | |
| | None => { fl.cur := next fl.org; nextFlat fl } | |
| } | |
| | None => None | |
| } | |
| instance Iterator (Flatten it of) e where Iterator of e, Iterator it of { | |
| next = nextFlat | |
| } | |
| flatten xss = Flatten xss (next xss) | |
| type FilterMaybe iter { | |
| it : iter | |
| } | |
| nextFilterMaybe : (FilterMaybe iter) -> Maybe e where Iterator iter (Maybe e) | |
| nextFilterMaybe fm = | |
| match (next fm.it) { | |
| | (Some (Some x)) => Some x; | |
| | (Some None) => nextFilterMaybe fm | |
| | None => None | |
| } | |
| instance Iterator (FilterMaybe iter) e where Iterator iter (Maybe e) { | |
| next = nextFilterMaybe | |
| } | |
| filterMaybe it = FilterMaybe it |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment