Skip to content

Commit e7a6a06

Browse files
brielovclaude
andcommitted
Add common utility functions to prelude
- Maybe.toEither: convert Maybe to Either with error message - Either.toMaybe: convert Either to Maybe (discards error) - List.forEach: iterate over list with side effects 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 3dd0f5c commit e7a6a06

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

lib/prelude/core.alg

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ module Maybe
111111
when Nothing -> Nil
112112
when Just x -> Cons x Nil
113113
end
114+
let toEither =
115+
err m ->
116+
match m
117+
when Nothing -> Left err
118+
when Just x -> Right x
119+
end
114120
end
115121

116122
module Either
@@ -158,6 +164,12 @@ module Either
158164
when Nothing -> Left err
159165
when Just x -> Right x
160166
end
167+
let toMaybe =
168+
e ->
169+
match e
170+
when Left a -> Nothing
171+
when Right b -> Just b
172+
end
161173
end
162174

163175
module List
@@ -358,4 +370,11 @@ module List
358370
end
359371
let intercalate =
360372
sep xss -> concat (intersperse sep xss)
373+
let rec forEach = f xs ->
374+
match xs
375+
when Nil -> ()
376+
when Cons x rest ->
377+
let unused = f x in
378+
forEach f rest
379+
end
361380
end

0 commit comments

Comments
 (0)