Skip to content

Consider additional adapt Ops between FunctionalTypes #47

@gselzer

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);
    };
  }
};

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Status

No status

Relationships

None yet

Development

No branches or pull requests

Issue actions