Stdlib.Either(3)
π Stdlib.Either β the manual page, but make it bestie-friendly
okay so this is Stdlib.Either from the OCaml library man pages, and the official NAME section literally just says "no description" π which is honestly the most mysterious main-character entrance ever. Anyway bestie, I got you β let's vibe check this thing together π«Ά
πΈ NAME
Stdlib.Either β no description π (yes, fr, that's what it says, the docs said "figure it out" and left)
π Module
The module is Stdlib.Either, and the documentation is for:
Module Either
: (module Stdlib__Either)
Like, it's its own module, it's giving self-sufficient icon energy β¨
π¦ Documentation
π the type itself
type ('a, 'b) t =
| Left of 'a
| Right of 'b
A value of ('a, 'b) Either.t contains either a value of 'a or a value of 'b π
that's the whole personality β it's one or the other, never both, no cap.
π the functions, one by one
val left : 'a -> ('a, 'b) tβleft visLeft v. Simple. Iconic. Wrap that baby up on the left side πval right : 'b -> ('a, 'b) tβright visRight v. Same deal, but the right side this time. Balance β¨val is_left : ('a, 'b) t -> boolβis_left (Left v)istrue, andis_left (Right v)isfalse. A little vibe check for which side you're on πval is_right : ('a, 'b) t -> boolβis_right (Left v)isfalse, andis_right (Right v)istrue. The mirror of the one above, obviously πval get_left : ('a, 'b) t -> 'aβget_left eisvifeisLeft v, and raise otherwise π- Since 5.4
- Raises
Invalid_argumentifeisRight _. So like, don't do that. For real. It will raise and you will be sad π
val get_right : ('a, 'b) t -> 'bβget_right eisvifeisRight v, and raise otherwise π- Since 5.4
- Raises
Invalid_argumentifeisLeft _. Same warning, other side. Respect the raise fr β οΈ
val find_left : ('a, 'b) t -> 'a optionβfind_left (Left v)isSome v, andfind_left (Right _)isNone. The safe, non-dramatic version π§val find_right : ('a, 'b) t -> 'b optionβfind_right (Right v)isSome v, andfind_right (Left _)isNone. Matching set, slay πval map_left : ('a1 -> 'a2) -> ('a1, 'b) t -> ('a2, 'b) tβmap_left f eisLeft (f v)ifeisLeft v, andeifeisRight _. So it only transforms the left side and leaves the right side completely untouched, as it should π₯val map_right : ('b1 -> 'b2) -> ('a, 'b1) t -> ('a, 'b2) tβmap_right f eisRight (f v)ifeisRight v, andeifeisLeft _. Same energy, other wing π¦val map : left:('a1 -> 'a2) -> right:('b1 -> 'b2) -> ('a1, 'b1) t -> ('a2, 'b2) tβmap ~left ~right (Left v)isLeft (left v), andmap ~left ~right (Right v)isRight (right v). Two labelled functions, whichever side shows up gets transformed π«val fold : left:('a -> 'c) -> right:('b -> 'c) -> ('a, 'b) t -> 'cβfold ~left ~right (Left v)isleft v, andfold ~left ~right (Right v)isright v. Both roads lead to the same type'c, so you always get something back, periodt πval retract : ('a, 'a) t -> 'aβretract (Left v)isv, andretract (Right v)isv. When both sides are the same type, you just get the value out either way β chef's kiss π€- Since 5.4
val iter : left:('a -> unit) -> right:('b -> unit) -> ('a, 'b) t -> unitβiter ~left ~right (Left v)isleft v, anditer ~left ~right (Right v)isright v. For when you just wanna do a lil side effect and returnunitπΈval for_all : left:('a -> bool) -> right:('b -> bool) -> ('a, 'b) t -> boolβfor_all ~left ~right (Left v)isleft v, andfor_all ~left ~right (Right v)isright v. Returns abool, obviously πval equal : left:('a -> 'a -> bool) -> right:('b -> 'b -> bool) -> ('a, 'b) t -> ('a, 'b) t -> boolβequal ~left ~right e0 e1tests equality ofe0ande1usingleftandrightto respectively compare values wrapped byLeft _andRight _. So you bring your own comparison functions for each side, and it does the rest β¨val compare : left:('a -> 'a -> int) -> right:('b -> 'b -> int) -> ('a, 'b) t -> ('a, 'b) t -> intβcompare ~left ~right e0 e1totally orderse0ande1usingleftandrightto respectively compare values wrapped byLeft _andRight _. And heads up bestie:Left _values are smaller thanRight _values π₯ that's the tiebreaker baked right in, iykyk.
π friendly reminder thatget_leftandget_rightwill straight up raiseInvalid_argumentif you call them on the wrong side. Usefind_left/find_rightif you're not feeling brave today π«Ά
π cute lil sign-off
This page came from OCamldoc, dated 2026-06-22, and it's the Stdlib.Either(3) page from the OCaml library. That's the whole footer, bestie β consider it delivered with a bow on top πβ¨π
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms