-
Notifications
You must be signed in to change notification settings - Fork 102
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
Folding has no explicit function #95
Comments
Alrighty, going to redefine this after thinking about it for a second. I would be nice to have a way to fold a As const {
Pred, compose, contramap, fold,
map, isNumber, prop, option,
reverseApply, runWith
} = crocks
// defProp : a -> String -> Object -> a
const defProp = curry(
(def, key) => compose(option(def), prop(key))
)
// defNotFound : String -> Object -> String | a
const defNotFound =
defProp('Not Found')
// preds : [ Pred Object ]
const preds =
map(reverseApply(Pred(isNumber)), [
contramap(defNotFound('a')),
contramap(defNotFound('b')),
contramap(defNotFound('c'))
])
// buildValidator : [ Pred a ] -> b -> Boolean
const buildValidator =
compose(flip(runWith), fold)
// isValid : a -> Boolean
const isValid =
buildValidator(preds)
isValid({ a: 34, b: 10, c: 3 }) // true
isValid({ b: 10, c: 3 }) // false
isValid({ a: 34, b: 'ten', c: 3 }) // false
|
Submitted PR#101 to address this issue. |
What are your thoughts on a
implementation would be something like:
|
I realize this is adding a lot to a single type; but doing some 👀 around, I found that |
@rstegg That is a valid point. I thought about adding The reason why we do not need a combining function for this What you implemented can easily be expressed as a function to reduce that uses an Array as its initial value, and concats each item in the List to it. I really do not see the need to add this as it may be confusing to have this option. But I am open to argument on this one. |
I would say that I am curious what would be gained by providing that robust API? A lot of those functions make sense in an imperative environment (shift/unshift as to cons/head/tail) . And with the improvements to Array based functions, you should be able to get those more imperative bits with your standard JS Array. |
Merged PR#101. Will go out on next release. |
Now that said, your provided implementation does not add much value. List m, Semigroup s => m a ~> (a -> s b) -> s b Then we could do things like: const { List, Sum, foldMap, concat, reduce } = crocks
const ar =
List
.fromArray([ 1, 2, 3 ])
.foldMap(Sum)
// => Sum(6) What do you think about that? |
While that seems like a good idea one thing that comes to mind is that we already have const { Sum, mconcat } = crocks
const data =
[ 1, 2, 3 ]
const sumList =
mconcat(Sum)
sumList(data)
// => Sum(6)
sumList(List.fromArray(data)
// => Sum(6) The only bummer, is that const data =
[ [ 1, 2], [ 3 ], [ 4, 5 ] ]
const flatten =
mconcat(Array)
flatten(data)
// => [ 1, 2, 3, 4, 5 ] |
I think we would get better reusability if instead, we just focus on getting the mconcat family to fully honor the Monoidal qualities of Array and String. |
All good points, in my opinion I like not having to wrap my data in an array (e.g. Back on I think this has solved any problems we've had with |
Well typically, you build a List out by Closing! |
updated to @evilsoft's reply, add a
fold
function:The text was updated successfully, but these errors were encountered: