Skip to content

Commit

Permalink
add Tuple.mapFst / Tuple.mapSnd
Browse files Browse the repository at this point in the history
  • Loading branch information
brekk committed Mar 28, 2024
1 parent 245f96d commit 1b0a4bb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 12 additions & 1 deletion prelude/__internal__/Tuple.mad
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@ export fst = (tuple) => where(tuple) {
a
}


snd :: #[a, b] -> b
export snd = (tuple) => where(tuple) {
#[_, b] =>
b
}

mapFst :: (a -> b) -> #[a, c] -> #[b, c]
export mapFst = (fn, tuple) => where(tuple) {
#[a, b] =>
#[fn(a), b]
}

mapSnd :: (b -> c) -> #[a, b] -> #[a, c]
export mapSnd = (fn, tuple) => where(tuple) {
#[a, b] =>
#[a, fn(b)]
}
10 changes: 10 additions & 0 deletions prelude/__internal__/Tuple.spec.mad
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { assertEquals, test } from "Test"

import { fst, mapFst, mapSnd, snd } from "./Tuple"



test("fst", () => assertEquals(fst(#["a", 200]), "a"))
test("snd", () => assertEquals(snd(#["a", 200]), 200))
test("mapFst", () => assertEquals(mapFst((x) => x / 2, #[200, "a"]), #[100, "a"]))
test("mapSnd", () => assertEquals(mapSnd((x) => x / 2, #["a", 200]), #["a", 100]))

0 comments on commit 1b0a4bb

Please sign in to comment.