In most collection libraries I know there is the "take" function coming
from the functional world that does that:

#(1 2 3 4 5 6 7 8) take: 5
  => #(1 2 3 4 5)

#(1 2 3 4) take: 5
  => #(1 2 3 4)

Now, I understand there are two different things in here.
 - First is the fact that we would like to apply this iterator to more than
sequenceable collections. I think that the name "take" is order agnostic in
that sense.
 - The other thing is that people would like to not calculate the
sub-collection... The problem is that we will end up with ugly combinations
of selectors like

take:thenDo:
take:thenCollect:
take:thenSelect:
...

And that pattern already exists also for:

select:thenCollect:
select:thenDo:

collect:as:
select:as:

...

I think I would like to have a more composable kind of
iterators/collections. There are Transducers that make "iterators" kind of
command objects that you can apply on a collection. Personally I don't like
the API choice but the idea behind is nice:

https://github.com/Pharophile/Transducers

sum := #+ init: 0.
sum1 := #(1 1 1) reduce: sum.
sum2 := (1 to: 1000) transduce: #odd filter reduce: sum.


And there are XTreams that allows one to work on a collection as a stream
and then compose decorators on top. This means however that we need to
create a stream and "close" it to obtain the resulting collection on every
manipulation.

https://github.com/mkobetic/Xtreams
https://code.google.com/archive/p/xtreams/wikis/Core.wiki

((aCollection reading collect: [ ... ]) select: [ ... ]) rest.

Guille

On Mon, Jan 22, 2018 at 4:11 AM, Ben Coman <b...@openinworld.com> wrote:

> On 22 January 2018 at 00:47, Stephane Ducasse <stepharo.s...@gmail.com>
> wrote:
> > Hi Ben and Clement
> >
> > I have a collection (a dictionary in my case) and I want to get
> > maximum 5 bindings out of it and iterate on them.
> > I want keysAndValuesDo: or do: but only up to 5 elements.
> >
> > aDict atMax: 5 do: [:each | ]
>
> "atMax" sound a bit like comparison rather than counting. Perhaps
> better would be...
>     aDict atMost: 5 do: [:each | ]   "or"
>     aDict for: 5 do: [:each | ]
>
> but rather than introduce three or more messages...
>     aDict atMost: 5 do: [:each| ... ]
>     aDict atMost: 5 select: [:each| ... ]
>     aDict atMost: 5 collect: [:each| ... ]
>
> why not introduce just one method?....
>     (aDict any: 5) do: [:each| ... ]
>     (aDict any: 5) select: [:each| ... ]
>     (aDict any: 5) collect: [:each| ... ]
>
>
> cheers -ben
>
>
> > So I learned from:to:do:
> >
> > aCollection atMax: 5 do: [:each | ]
> >
> > Does it make sense?
> >
> > Stef
> >
> > On Sun, Jan 21, 2018 at 1:16 PM, Clément Bera <bera.clem...@gmail.com>
> wrote:
> >> I don't think we do. Do you need it on SequenceableCollection or
> >> HashedCollection too ?
> >>
> >> Recently I was trying to iterate over the first N elements of a
> collection
> >> and since there was no #first:do: I used #from:to:do:. I guess you
> could use
> >> that too:
> >>
> >> aCollection from: 1 to: (aCollection size min: 1000) do: aBlock
> >>
> >> Which guarantees you iterate at max over 1000 elements. But that API is
> >> SequenceableCollection specific.
> >>
> >> On Sun, Jan 21, 2018 at 11:44 AM, Ben Coman <b...@openinworld.com>
> wrote:
> >>>
> >>> On 21 January 2018 at 18:36, Stephane Ducasse <stepharo.s...@gmail.com
> >
> >>> wrote:
> >>> > Hi
> >>> >
> >>> > I would like to iterate at max on a certain amount of elements in a
> >>> > collection.
> >>> > And I was wondering if we have such iterator.
> >>>
> >>> I'm not clear what functionality your asking for.  Could you present
> >>> it as code & result if you assumed the iterator you want was
> >>> available?
> >>>
> >>> cheers -ben
> >>>
> >>
> >>
> >>
> >> --
> >> Clément Béra
> >> Pharo consortium engineer
> >> https://clementbera.wordpress.com/
> >> Bâtiment B 40, avenue Halley 59650 Villeneuve d'Ascq
> >
>
>


-- 



Guille Polito

Research Engineer

Centre de Recherche en Informatique, Signal et Automatique de Lille

CRIStAL - UMR 9189

French National Center for Scientific Research - *http://www.cnrs.fr
<http://www.cnrs.fr>*


*Web:* *http://guillep.github.io* <http://guillep.github.io>

*Phone: *+33 06 52 70 66 13

Reply via email to