Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
brekk committed Mar 27, 2024
1 parent c7b85b0 commit dbbdbb9
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 194 deletions.
56 changes: 15 additions & 41 deletions prelude/__internal__/Function.mad
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export type Step a b
= Loop(a)
| Done(b)
export type Step a b = Loop(a) | Done(b)

// inspired from:
// https://github.com/purescript/purescript-tailrec/blob/master/src/Control/Monad/Rec/Class.purs
Expand All @@ -14,7 +12,10 @@ export tailRec = (f, a) => {
b
}

return pipe(f, go)(a)
return pipe(
f,
go,
)(a)
}


Expand Down Expand Up @@ -105,6 +106,16 @@ export ifElse = (predicate, truthy, falsy, value) => predicate(value) ? truthy(v
when :: (a -> Boolean) -> (a -> a) -> a -> a
export when = (predicate, truthy, value) => ifElse(predicate, truthy, always(value), value)

/**
* Run a transformation only if the predicate returns false,
* otherwise return the initial value.
* @since 0.23.1
* @example
* unless(equals(5), (x) => x * 10, 5) //
*/
unless :: (a -> Boolean) -> (a -> a) -> a -> a
export unless = (predicate, falsy, value) => ifElse(predicate, always(value), falsy, value)

/**
* Returns the complement of the given Boolean value.
*
Expand Down Expand Up @@ -132,43 +143,6 @@ flip :: (a -> b -> c) -> b -> a -> c
export flip = (f) => ((b, a) => f(a, b))


/**
* Given a predicate and a list of values it returns true if the predicate returns
* true for any of the values in the list.
*
* @since 0.11.0
* @example
* any((x) => x > 10, [1, 2, 3]) // false
* any((x) => x < 10, [1, 2, 3]) // true
*/
any :: (a -> Boolean) -> List a -> Boolean
export any = (pred, list) => where(list) {
[] =>
false

[x, ...xs] =>
pred(x) || any(pred, xs)
}


/**
* Given a predicate and a list of values it returns true if the predicate returns
* true for all values in the list.
*
* @since 0.11.0
* @example
* all((x) => x > 2, [1, 2, 3]) // false
* all((x) => x < 4, [1, 2, 3]) // true
*/
all :: (a -> Boolean) -> List a -> Boolean
export all = (predicate, list) => where(list) {
[] =>
true

[x, ...xs,] =>
predicate(x) && all(predicate, xs)
}

/**
* Functional "or", given two predicates and a value, return true if either predicate is true.
*
Expand Down
Loading

0 comments on commit dbbdbb9

Please sign in to comment.