← 输入框|github.com|刷新
Skip to content

Consider additional adapt Ops between FunctionalTypes #47

Open

Description

@gselzer

Currently scijava-ops has support for the following adaptations:

  • Computers to Functions
  • Functions to Computers
  • Inplaces to Functions

@ctrueden and I have discussed (several times) why the other three conversions have not been implemented. I think it a good idea to put the discussion here for posterity (and because I found myself again asking why we do not have these):

  • Computer to Inplace: Suppose we have a double[] and want to mutate this array with math.sqrt. Such a converter might look like
@Plugin(type = Op.class, name = "adapt")
public class CompToInplaceAdaptor implements Function<Computers.Arity1<IO, IO>, Inplaces.Arity1<IO>> {

  @OpDependency(name = "copy")
  private Function<IO, IO> copyOp;

  public Inplaces.Arity1<IO> apply (final Computers.Arity1<IO, IO> computer) {
    return (io) -> {
      IO copy = copyOp.apply(io);
      computer.compute(copy, io);
    };
  }
};
  • Inplace to Function: I cannot really think of a use case for this, but the general idea is that the user expects an output to be generated for them, and that their input will not be modified:
@Plugin(type = Op.class, name = "adapt")
public class InplaceToFunctionAdaptor implements Function<Inplaces.Arity1<IO>, Function<IO, IO>> {

  @OpDependency(name = "copy")
  private Function<IO, IO> copyOp;

  public Function<IO, IO> apply (final Inplaces.Arity1<IO> inplace) {
    return (in ) -> {
      IO copy = copyOp.apply(io);
      inplace.mutate(copy);
      return copy;
    };
  }
};
  • Inplace to Computer: I cannot really think of a use case for this, but the general idea is that the user expects the output of the Op to be placed in their preallocated output, and that their input will not be modified:
@Plugin(type = Op.class, name = "adapt")
public class InplaceToComputerAdaptor implements Function<Inplaces.Arity1<IO>, Computers.Arity1<IO, IO>> {

  @OpDependency(name = "copy")
  private Computers.Arity1<IO, IO> copyOp;

  public Computers.Arity1<IO, IO> apply (final Inplaces.Arity1<IO> inplace) {
    return (in, out) -> {
      copyOp.compute(in, out);
      inplace.mutate(out);
    };
  }
};

Activity

  1. ctrueden commented on Jul 7, 2020

    @ctrueden
    Member

    I don't have any clear reasons why we shouldn't implement all of these. We aren't chaining adapt steps anymore, so there is no ambiguity. And each one could make a scripter's life easier in the right circumstance. @dietzc Do you recall any concrete reason why implementing these automatic conversions between op types would be a bad idea?

  2. added this to the unscheduled milestone on Apr 29, 2021
  3. gselzer commented on May 13, 2021

    @gselzer
    MemberAuthor

    Time commitment: 1 week

  4. self-assigned this
    on Sep 26, 2023
  5. modified the milestones: unscheduled, 1.0.0 on Sep 26, 2023
  6. gselzer commented on Nov 16, 2023

    @gselzer
    MemberAuthor

    @hinerm and I are thinking YAGNI. All of these additional functional types are dealing with Inplaces. In our limited experience, we generally don't use inplaces unless we really want efficiency, and we haven't seen that be the goal as of yet. The natural tendency is to call these things as functions, and I'm hesitant to put in X->Inplace converters that give the impression of efficiency when really they are less efficient than the Computers or Functions they're wrapping because of the additional copy operation.

    It's not that we're against adding these in, and I think we can do it if/when there is a need.

  7. modified the milestones: 1.0.0, unscheduled on Nov 16, 2023
  8. added a commit that references this issue on May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions