Skip to content

How would one implement flatMap or bind with zug? #28

@bradphelan

Description

@bradphelan

For example I'm looking for an equivalent to

std::vector<int> src = {1,2,3,4,5,6};

std::vector<int> result = 
    src 
    | flatMap([](int i){ return i%2 == 0 ? std::vector<int>{i,i+1} : std::vector<int>{i} ; })
    | toVector();

result should be

{1,2,3,3,4,5,5,6,7}

ie: flatMap (also known as bind) takes each element of the source and passes it to the function which is expected to return a sequence and all sequences are concatenated together.

Though reading through your doc maybe the way to do this is with map and then cat like.

std::vector<int> src = {1,2,3,4,5,6};
    | map([](int i){ return i%2 == 0 ? std::vector<int>{i,i+1} : std::vector<int>{i} ; })
    | cat();

I notice that you have a mapcat but it doesn't do the same as above. Maybe it should be renamed catmap because it is equivalent to cat | map rather than map | cat

I have my own two libraries that do the same as transducers with one for space mapping and one for temporal mapping. But I'm intrigued by the design of transducers that allows one implementation to handle both by factoring out the processes. I need to spend some time with it.

A small point. Somehow I prefer the term where rather than filter. I'm always have to think twice with filter whether the predicate means to include all things that return positive or exclude them. I think the problem is that filter is most often used with predicates in English as filter out which has the opposite meaning to where and filter as often used in libraries like transducers.

All dogs where tail length is longer than 20cm

vs

Filter out all dogs with tail length longer than 20cm

However I'm aware than different libraries use filter and where interchangeably so maybe it's just me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions