crocks icon indicating copy to clipboard operation
crocks copied to clipboard

Every function that takes an Array should also allow an Iterable

Open evilsoft opened this issue 9 years ago • 4 comments

So each one of these should also take an Iterable:

Helpers

  • [ ] mconcat
  • [ ] mconcatMap
  • [ ] mreduce
  • [ ] mreduceMap
  • [ ] take (new)

Pointfree

  • [ ] filter
  • [ ] find (new)
  • [x] head
  • [ ] map
  • [ ] reduce
  • [ ] sequence
  • [ ] tail
  • [ ] traverse

Since we are now exposing the possibility of infinite values, makes sense to add a take function at this point.

evilsoft avatar Jan 18 '17 18:01 evilsoft

how would this be implemented without changing the type or mutating the value? the value of using the known functions is that they take care of creating the correct type but i think filter/map/reduce won't work without mutation or changing type

I think though most of the others should be fine

dalefrancis88 avatar Jun 06 '18 05:06 dalefrancis88

hey @evilsoft I am wondering if you have some magical idea for this? cause i can't quite think of how to do many of these without returning an array instead of the original iterable?

dalefrancis88 avatar Nov 09 '18 01:11 dalefrancis88

I think we need to define a function that clone the object in a way that it doesn't loose the structure. We need some checks for base constructors like (Date, Array, String etc..) and clone it in the right way, and when its a normal object use something like Object.create and assign. Something like (pretty basic without the base constructor checks)

let object = Object.create(Object.getPrototypeOf(source))
Object.assign(object, source)

let symbols = Object.getOwnPropertySymbols(source)
symbols.forEach(key => object[key] = source[key])

return object

To make it easier, we should clone only after isIterable(), so we could call it cloneIterable

HenriqueLimas avatar Nov 11 '18 12:11 HenriqueLimas

@HenriqueLimas @evilsoft I think we may have made a mistake on this one when we did head. I am thinking that the behavior we started with in head is wrong. I'm thinking that it should be if a generator function is passed in, then we will invoke it and iterate, however if you pass in the result of a generator call then your expectation is that you want it iterated over. What do you guys think?

dalefrancis88 avatar Jul 21 '19 16:07 dalefrancis88