Skip to content

Commit

Permalink
Merge pull request #12 from A1c0/all-any-pass
Browse files Browse the repository at this point in the history
Add allPass and anyPass
  • Loading branch information
A1c0 authored Dec 2, 2021
2 parents 86a8ad6 + d789e3c commit aea8693
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 10 deletions.
62 changes: 53 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,51 @@ Replace a substring with a RegExp

### Logic

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L156">`cond :: Array Pair (a -> Boolean) (a -> b) -> a -> Either a b`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L156">`allPass :: Array (a -> Boolean) -> a -> Boolean`</a>

Return `true` if all predicates return true, else return `false`

```js
> const isEvenNumber = x => x%2 === 0;
> const isPositiveNumber = x => x > 0;
> const isPositiveEvenNumber = allPass ([isEvenNumber, isPositiveNumber]);

> isPositiveEvenNumber (0)
false

> isPositiveEvenNumber (1)
false

> isPositiveEvenNumber (-2)
false

> isPositiveEvenNumber (2)
true
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L189">`anyPass :: Array (a -> Boolean) -> a -> Boolean`</a>

Return `true` if one of predicates return true, else return `false`

```js
> const isSix = x => x === 6;
> const isNegative = x => x < 0;
> const isNegativeOrSix = anyPass ([isNegative, isSix]);

> isNegativeOrSix (0)
false

> isNegativeOrSix (1)
false

> isNegativeOrSix (-2)
true

> isNegativeOrSix (6)
true
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L222">`cond :: Array Pair (a -> Boolean) (a -> b) -> a -> Either a b`</a>

Apply transformer predicate return true anc return a Right value
If any predicate return `true`, it will return initial value in Left Value
Expand All @@ -142,7 +186,7 @@ Left ("123!")

Use [implementation created by David Chambers](https://gist.github.com/davidchambers/45aa0187a32fbac6912d4b3b4e8530c5)

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L200">`view :: Lens s a -> s -> a`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L266">`view :: Lens s a -> s -> a`</a>

Allow to get a value by a Lens

Expand All @@ -154,7 +198,7 @@ Allow to get a value by a Lens
dc@davidchambers.me
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L212">`over :: Lens s a -> (a -> a) -> s -> s`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L278">`over :: Lens s a -> (a -> a) -> s -> s`</a>

Allow to set a value by a Lens

Expand All @@ -168,7 +212,7 @@ Allow to set a value by a Lens

### Maybe

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L229">`toMaybe :: (a -> Boolean) -> a -> Maybe a`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L295">`toMaybe :: (a -> Boolean) -> a -> Maybe a`</a>

Wrapping value in Maybe depending on predicate

Expand All @@ -185,7 +229,7 @@ Just (1)

### Either

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L254">`toEither :: (a -> Boolean) -> (a -> b) -> a -> Either b a`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L320">`toEither :: (a -> Boolean) -> (a -> b) -> a -> Either b a`</a>

Convert to Either depending on predicate

Expand All @@ -202,7 +246,7 @@ Right (2)

### Fluture

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L279">`flMap :: PositiveNumber -> (a -> Fluture b c) -> Array a -> Fluture b Array c`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L345">`flMap :: PositiveNumber -> (a -> Fluture b c) -> Array a -> Fluture b Array c`</a>

Apply a function that return a Fluture on each item of an array and return a Fluture

Expand All @@ -218,7 +262,7 @@ Apply a function that return a Fluture on each item of an array and return a Flu
[rejection]: "error: 1"
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L303">`toFluture :: (a -> Boolean) -> (a -> b) -> a -> Fluture b a`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L369">`toFluture :: (a -> Boolean) -> (a -> b) -> a -> Fluture b a`</a>

Convert to a Fluture depending on predicate

Expand All @@ -233,7 +277,7 @@ Convert to a Fluture depending on predicate
[resolution]: 1
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L324">`maybeToFluture :: b -> Maybe a -> Fluture b a`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L390">`maybeToFluture :: b -> Maybe a -> Fluture b a`</a>

Convert a Maybe to a Fluture

Expand All @@ -248,7 +292,7 @@ Convert a Maybe to a Fluture
[rejection]: "not a number"
```

#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L347">`eitherToFluture :: Either a b -> Fluture a b`</a>
#### <a href="https://github.com/A1c0/sanctuary-lourdes/blob/main/index.mjs#L413">`eitherToFluture :: Either a b -> Fluture a b`</a>

Convert an Either to a Fluture

Expand Down
66 changes: 66 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,72 @@ export const create = ({checkTypes}) => {
// ##### LOGIC #####
// #####################

// allPass :: Array (a -> Boolean) -> a -> Boolean
//
// Return `true` if all predicates return true, else return `false`
//
// > const isEvenNumber = x => x%2 === 0;
// > const isPositiveNumber = x => x > 0;
// > const isPositiveEvenNumber = allPass ([isEvenNumber, isPositiveNumber]);
//
// > isPositiveEvenNumber (0)
// false
//
// > isPositiveEvenNumber (1)
// false
//
// > isPositiveEvenNumber (-2)
// false
//
// > isPositiveEvenNumber (2)
// true
const _allPass = predicates => value => {
for (const predicate of predicates) {
if (!predicate (value)) {
return false;
}
}
return true;
};
const allPass = def ('allPass')
({})
([$.Array ($.Predicate (a)), a, $.Boolean])
(_allPass);
exportFn.allPass = predicates => value => allPass (predicates) (value);

// anyPass :: Array (a -> Boolean) -> a -> Boolean
//
// Return `true` if one of predicates return true, else return `false`
//
// > const isSix = x => x === 6;
// > const isNegative = x => x < 0;
// > const isNegativeOrSix = anyPass ([isNegative, isSix]);
//
// > isNegativeOrSix (0)
// false
//
// > isNegativeOrSix (1)
// false
//
// > isNegativeOrSix (-2)
// true
//
// > isNegativeOrSix (6)
// true
const _anyPass = predicates => value => {
for (const predicate of predicates) {
if (predicate (value)) {
return true;
}
}
return false;
};
const anyPass = def ('anyPass')
({})
([$.Array ($.Predicate (a)), a, $.Boolean])
(_anyPass);
exportFn.anyPass = predicates => value => anyPass (predicates) (value);

// cond :: Array Pair (a -> Boolean) (a -> b) -> a -> Either a b
//
// Apply transformer predicate return true anc return a Right value
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sanctuary-lourdes",
"version": "0.2.0",
"version": "0.3.0",
"main": "index.mjs",
"author": "Alexandre CONTASSOT <alexandrecontassot@hotmail.fr>",
"license": "MIT",
Expand Down

0 comments on commit aea8693

Please sign in to comment.