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

Allow traversals as predicates #184

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 22 additions & 3 deletions src/partial.lenses.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ const mumBy = ord =>

//

const predicateFn = x => x || void 0

const toPredicate = predicate => {
if (typeof predicate === 'function' && predicate.length < 4) return predicate
predicate = toFunction(predicate)
return (x, i) => predicate(x, i, Select, predicateFn)
}

//

const traverseU = function traverse(C, xi2yC, t, s) {
return toFunction(t)(s, void 0, C, xi2yC)
}
Expand Down Expand Up @@ -832,6 +842,7 @@ const disjointFwd = (process.env.NODE_ENV === 'production'

const eitherU = (t, e) =>
function either(c) {
c = toPredicate(c)
return function either(x, i, C, xi2yC) {
return (c(x, i) ? t : e)(x, i, C, xi2yC)
}
Expand Down Expand Up @@ -1001,7 +1012,7 @@ export const condOf = (process.env.NODE_ENV === 'production'
const os = Array(n + 1)
for (let i = 0; i < n; ++i) {
const c = arguments[i + 1]
ps[i] = c[0]
ps[i] = toPredicate(c[0])
os[i] = toFunction(c[1])
}
os[n] = def
Expand Down Expand Up @@ -1227,11 +1238,13 @@ export function query() {
return r
}

export const satisfying = p =>
function satisfying(x, i, C, xi2yC) {
export const satisfying = p => {
p = toPredicate(p)
return function satisfying(x, i, C, xi2yC) {
const rec = (x, i) => (p(x, i) ? xi2yC(x, i) : children(x, i, C, rec))
return rec(x, i)
}
}

export const leafs = satisfying(
x => void 0 !== x && !I.isArray(x) && !I.isObject(x)
Expand All @@ -1240,6 +1253,7 @@ export const leafs = satisfying(
// Folds over traversals

export const all = I.curry(function all(xi2b, t, s) {
xi2b = toPredicate(xi2b)
return !getAsU(
(x, i) => {
if (!xi2b(x, i)) return true
Expand All @@ -1252,6 +1266,7 @@ export const all = I.curry(function all(xi2b, t, s) {
export const and = all(id)

export const any = I.curry(function any(xi2b, t, s) {
xi2b = toPredicate(xi2b)
return !!getAsU(
(x, i) => {
if (xi2b(x, i)) return true
Expand Down Expand Up @@ -1284,6 +1299,7 @@ export const concatAs = mkTraverse(id, ConstantOf)
export const concat = concatAs(id)

export const countIf = I.curry(function countIf(p, t, s) {
p = toPredicate(p)
return traverseU(Sum, (x, i) => (p(x, i) ? 1 : 0), t, s)
})

Expand Down Expand Up @@ -1417,6 +1433,7 @@ export const minimumBy = mumBy(I.ltU)
export const minimum = minimumBy(id)

export const none = I.curry(function none(xi2b, t, s) {
xi2b = toPredicate(xi2b)
return !getAsU(
(x, i) => {
if (xi2b(x, i)) return true
Expand Down Expand Up @@ -1548,6 +1565,7 @@ export const filter = (process.env.NODE_ENV === 'production'
)
])
))(function filter(xi2b) {
xi2b = toPredicate(xi2b)
return function filter(xs, i, F, xi2yF) {
let ts
let fs = I.array0
Expand All @@ -1564,6 +1582,7 @@ export const filter = (process.env.NODE_ENV === 'production'
})

export function find(xih2b) {
xih2b = toPredicate(xih2b)
const hint = arguments.length > 1 ? arguments[1] : {hint: 0}
return function find(xs, _i, F, xi2yF) {
const ys = seemsArrayLike(xs) ? xs : ''
Expand Down