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

Implementation of Predicate class #2

Open
26 of 28 tasks
zero-plusplus opened this issue Sep 5, 2021 · 0 comments
Open
26 of 28 tasks

Implementation of Predicate class #2

zero-plusplus opened this issue Sep 5, 2021 · 0 comments
Labels
draft Draft of new features

Comments

@zero-plusplus
Copy link
Owner

zero-plusplus commented Sep 5, 2021

Tasks

  • Implement the basic mechanism.
  • Implement the predicate variations
    • all
    • any
    • none
    • not
  • Implement the method and valiation
    • equals
    • hasKey
    • hasNestedKey
    • is
    • isArray
    • isBoolean
    • isCallable
    • isDigit
    • isDigitLike
    • isFalsy
    • isFucn
    • isNegative
    • isNumber
    • isNumberLike
    • isObject
    • isPositive
    • isPrimitive
    • isString
    • isTruthy
    • startsWith
    • ...

Predicate refers to a function that returns a boolean, like the IsObject function.

Predicate takes one or two or more arguments. The first argument is always the value to be checked.

It is used as follows.

; 2.0-beta.1
bee.Predicate.isObject({}) ; => true
bee.Predicate.isObject("abc") ; => false

bee.Predicate.startsWith("abc", "a") ; => true
bee.Predicate.startsWith("abc", "b") ; => false

bee.Predicate.between(100, 50, 150) => true
bee.Predicate.between(100, 150, 200) => false

If the predicate does not have enough arguments, it will be partially applied starting with the second argument.

This is valid where you want to check the values more specifically, as in the following.

; 2.0-beta.1
startsWithBySharp := bee.Predicate.startsWith("#") ; => true
%startsWithBySharp%("#abc") ; => true
%startsWithBySharp%("$abc") ; => false

; This is the same meaning as above.
bee.Predicate.startsWith("#abc", "#") => true
bee.Predicate.startsWith("$abc", "#") => false

There are several variations of each predicate.
all, any, and none are useful for checking multiple values. It takes an array of values to check.

bee.Predicate.not.isObject({}) ; => false
bee.Predicate.not.isObject("abc") ; => true

bee.Predicate.all.isObject([ {}, [] ]) ; => true
bee.Predicate.all.isObject([ {}, [], "abc" ]) ; => false

bee.Predicate.any.isObject([ {}, [], "abc" ]) ; => true
bee.Predicate.any.isObject([ "abc", "abc", "abc" ]) ; => false

bee.Predicate.none.isObject([ {}, [], "abc" ]) ; => false
bee.Predicate.none.isObject([ "abc", "abc", "abc" ]) ; => true

These can be combined to create new predicate.

; 2.0-beta.1
pred := bee.Predicate ; shorthund

isPublicMethod := pred.and(
  pred.not.startsWith("_"),
  pred.isMethod()
)
isCallable := pred.or(
  pred.isGlobalFunc(),
  pred.isMethod(),
  pred.isBindFunc(),
  pred.isClosure(),
)

This class is especially useful for v1, which has no fat-arrow function. For example, if you define a function that takes a function and retrieves only certain values of an array, as follows.

; 1.1.33.10
SelectArr(arr, predicate) {
  selected := []
  for i, value in arr {
    if (%predicate%(value, i, arr)) {
        selected.push(value)
    }
  }
  return selected
}

You can use this function in the follows way.

; 1.1.33.10
SelectArr([ 1, 3, -1, -3 ], bee.Predicate.isPositive()) ; => [ 1, 3 ]
@zero-plusplus zero-plusplus added the draft Draft of new features label Sep 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
draft Draft of new features
Projects
None yet
Development

No branches or pull requests

1 participant