Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Random #123

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions prelude/__internal__/List.mad
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { reduceLeft } from "__BUILTINS__"
import type { Comparison } from "Compare"
import type { Maybe } from "Maybe"

import { LT, EQ, GT } from "Compare"
import { EQ, GT, LT } from "Compare"
import Function from "Function"
import { Just, Nothing } from "Maybe"
import {} from "Monad"
import { reduceLeft } from "__BUILTINS__"



/**
Expand Down Expand Up @@ -53,6 +54,7 @@ instance Monad List {

instance Alternative List {
aempty = []

alt = mappend
}

Expand Down Expand Up @@ -382,6 +384,32 @@ export nth = (i, list) => where(list) {
i == 0 ? Just(a) : nth(i - 1, xs)
}

/**
* Set a value at a specific index
*/
set :: Integer -> a -> List a -> List a
export set = (n, x, list) => {
_set = (j, l2) => where(l2) {
[] =>
[]

[a, ...xs] =>
j == n ? [x, ...xs] : [a, ..._set(j + 1, xs)]
}
return _set(0, list)
}

/**
* Set a value at a specific index, maybe
*/
setMaybe :: Integer -> Maybe a -> List a -> List a
export setMaybe = (n, m, list) => where(m) {
Nothing =>
list

Just(x) =>
set(n, x, list)
}

/**
* Aggregate a single value by iterating over a list, from right-to-left.
Expand Down Expand Up @@ -524,8 +552,12 @@ export slice = (start, end, list) => {
[]

[a, ...xs] =>
if (start_ == 0 && end_ > 0) { [a, ...helper(0, end_ - 1, xs)] } else {
if (start_ > 0) { helper(start_ - 1, end_ - 1, xs) } else { [] }
if (start_ == 0 && end_ > 0) {
[a, ...helper(0, end_ - 1, xs)]
} else if (start_ > 0) {
helper(start_ - 1, end_ - 1, xs)
} else {
[]
}
}

Expand Down Expand Up @@ -626,9 +658,7 @@ export sortBy = (compareFn, list) => {
merge :: List a -> List a -> List a
merge = (listA, listB) => where(#[listA, listB]) {
#[[a, ...as], [b, ...bs]] =>
compareFn(a, b) == GT
? [b, ...merge(listA, bs)]
: [a, ...merge(as, listB)]
compareFn(a, b) == GT ? [b, ...merge(listA, bs)] : [a, ...merge(as, listB)]

#[[], bs] =>
bs
Expand Down Expand Up @@ -756,6 +786,8 @@ export includes = (x, list) => where(list) {
[a, ...xs] =>
a == x || includes(x, xs)
}
// a == x ? true : includes(x, xs)



/**
Expand Down
31 changes: 30 additions & 1 deletion prelude/__internal__/List.spec.mad
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
reduce,
reject,
reverse,
set,
setMaybe,
singleton,
sort,
sortDesc,
Expand Down Expand Up @@ -237,4 +239,31 @@ test("endsWith - empty", () => assertEquals(endsWith([4, 6], []), false))

test("tails", () => assertEquals(tails([1, 2, 3]), [[1, 2, 3], [2, 3], [3], []]))

test("groupBy", () => assertEquals(groupBy((a, b) => a == b, [1, 1, 2, 3, 2, 2]), [[1, 1], [2], [3], [2, 2]]))
test(
"groupBy",
() => assertEquals(groupBy((a, b) => a == b, [1, 1, 2, 3, 2, 2]), [[1, 1], [2], [3], [2, 2]]),
)

test(
"set",
() => assertEquals(
set(3, "nice", ["really", "super", "duper", "dumb"]),
(["really", "super", "duper", "nice"]),
),
)

test(
"setMaybe - Just",
() => assertEquals(
setMaybe(3, Just("awesome"), ["this", "is", "really", "dumb"]),
["this", "is", "really", "awesome"],
),
)

test(
"setMaybe - Nothing",
() => assertEquals(
setMaybe(3, Nothing, ["this", "is", "really", "dumb"]),
["this", "is", "really", "dumb"],
),
)
Loading
Loading