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

Tuple: Add functional mappers to tuples #382

Closed
minborg opened this issue Mar 20, 2017 · 1 comment
Closed

Tuple: Add functional mappers to tuples #382

minborg opened this issue Mar 20, 2017 · 1 comment
Assignees
Labels
Milestone

Comments

@minborg
Copy link
Contributor

minborg commented Mar 20, 2017

  map(toTuple(Hare::getId,Hare::getName, Hare::getAge));

Similar to toMap() collector extractors

@minborg minborg self-assigned this Mar 20, 2017
@minborg minborg added this to the Future milestone Mar 20, 2017
@minborg minborg changed the title Tuple: Add collectors to tuples Tuple: Add functional mappers to tuples Mar 20, 2017
@minborg
Copy link
Contributor Author

minborg commented Mar 20, 2017

    interface TupleMapper<T> {

        Function<T, ?> getMapper(int index);
    }

    interface Tuple1Mapper<T, T0> extends TupleMapper<T>, Function<T, Tuple1<T0>> {
    }

    interface Tuple2Mapper<T, T0, T1> extends TupleMapper<T>, Function<T, Tuple2<T0, T1>> {
    }

    static class Tuple1MapperImpl<T, T0> implements Tuple1Mapper<T, T0> {

        private final Function<T, T0> mapper0;

        public Tuple1MapperImpl(Function<T, T0> mapper0) {
            this.mapper0 = requireNonNull(mapper0);
        }

        @Override
        public Tuple1<T0> apply(T t) {
            return Tuples.of(mapper0.apply(t));
        }

        @Override
        public Function<T, ?> getMapper(int index) {
            if (index == 0) {
                return mapper0;
            };
            throw new IllegalArgumentException();
        }

    }

    private <T, T0> Tuple1Mapper<T, T0> tupleMapper(Function<T, T0> mapper0) {
        return new Tuple1MapperImpl<>(mapper0);
        //return (T t) -> Tuples.of(mapper0.apply(t));
    }

    private <T, T0, T1> Function<T, Tuple2<T0, T1>> tupleMapper(Function<T, T0> mapper0, Function<T, T1> mapper1) {
        return (T t) -> Tuples.of(mapper0.apply(t), mapper1.apply(t));
    }

    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants