Skip to content

stdlib: add indexOf, flatMap and toInt - #263

Merged
vito merged 1 commit into
mainfrom
stdlib-list-helpers
Nov 8, 2026
Merged

vito merged 1 commit into
mainfrom
stdlib-list-helpers

Conversation

@vito

@vito vito commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

dagger/dagger.io#5802 (.dagger/modules/dagger-io-dev/module_index.dang, as of
f0c20d2) still works around two gaps:

workaround in dagger.io#5802 now
indicesOf (line 404), used by roleIndex as indicesOf(roles, role)[0] ?? -1 (line 1017) roles.indexOf(role) ?? -1
draft.entries.reduce([]) { all, e => all + e.args } (line 984; also module_index_tests.dang line 322) draft.entries.flatMap { e => e.args }

renderRedirects (lines 846–847) also calls indicesOf, but it needs every
match (it checks there is exactly one), so indexOf doesn't replace that use.

f0c20d2 already dropped the hex lookup tables (pathUnescape uses
JSON.decode now), but toInt(base:) stays: it fills a real gap, since
nothing parses a non-decimal integer from a string.

sum is no longer part of this PR. It moves to a follow-up that types it
through a built-in Numeric constraint (sum[a: Numeric]), built on the
type-parameter work, instead of a builtin-only typing hook.

What's added

  • [T]!.indexOf(element: T) -> Int: the index of the first equal element
    (Dang equality, like .contains), or null when the element is absent.
    It returns null rather than -1 because in Dang, errors are for errors and
    null is for absence. .find and xs[99] already work that way.
  • [T]!.flatMap { x, i => [U]! } -> [U]!: map, then concatenate. The
    block takes the index, like .map does.
  • String!.toInt(base: Int = 10) -> Int!: accepts bases 2 to 36, letters
    in either case, and an optional sign. It raises on whitespace, 0x, _, any
    other invalid input, or overflow. Nothing covered this before: a "42" :: Int! cast is a type error, and JSON.decode only handles base 10. The name
    follows toUpper/toBase64.

Docs and tests

  • The dang-language skill now covers all three: stdlib.md (needed by
    TestStdlibSkillCoversBuiltins), the SKILL.md idioms, and the "no hex
    literals" note in syntax.md. The collections and strings docs pages have
    live examples.
  • Each builtin has a Doc and an Example. There are new tests/test_*.dang
    specimens, and an error golden for invalid toInt input.
  • pkg/dang, pkg/lsp, docs/go, tests, docs and fmt/stale pass.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: e3e364d
Status: ✅  Deploy successful!
Preview URL: https://4e42aea3.dang-3kk.pages.dev
Branch Preview URL: https://stdlib-list-helpers.dang-3kk.pages.dev

View logs

dagger/dagger.io#5802 hand-wrote an indicesOf helper and flattened lists with
reduce([]) { all, e => all + e.args }.

- [T]!.indexOf(element) -> Int: null when absent, not -1, since absence is
  null in Dang.
- [T]!.flatMap { x, i => [U]! } -> [U]!, taking the index like map.
- String!.toInt(base: Int = 10) -> Int!: bases 2 to 36, raising on invalid
  input or overflow. `s :: Int!` doesn't parse and JSON.decode only covers
  base 10.

The skill's stdlib reference and the collections/strings docs pages cover
them.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito force-pushed the stdlib-list-helpers branch from fe1451e to e3e364d Compare November 8, 2026 22:54
@vito vito changed the title stdlib: add indexOf, flatMap, sum and toInt stdlib: add indexOf, flatMap and toInt Nov 8, 2026
@vito
vito merged commit d5dc80e into main Nov 8, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant