All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fj.data.Conversions Maven / Gradle / Ivy

Go to download

Functional Java is an open source library that supports closures for the Java programming language

There is a newer version: 5.0
Show newest version
package fj.data;

import fj.F;
import fj.P1;
import fj.Unit;
import fj.function.TryEffect0;
import fj.function.Effect0;
import fj.function.Effect1;

import fj.Try;
import fj.TryEffect;
import fj.Effect;
import fj.function.Try0;
import fj.function.Try1;

import java.io.IOException;
import static fj.Unit.unit;
import static fj.data.List.asString;
import static fj.data.List.fromString;

/**
 * Functions that convert between data structure types.
 *
 * @version %build.number%
 */
public final class Conversions {
  private Conversions() {
    throw new UnsupportedOperationException();
  }

  // BEGIN List ->

  /**
   * A function that converts lists to arrays.
   *
   * @return A function that converts lists to arrays.
   */
  public static  F, Array> List_Array() {
    return new F, Array>() {
      public Array f(final List as) {
        return as.toArray();
      }
    };
  }

  /**
   * A function that converts lists to streams.
   *
   * @return A function that converts lists to streams.
   */
  public static  F, Stream> List_Stream() {
    return new F, Stream>() {
      public Stream f(final List as) {
        return as.toStream();
      }
    };
  }

  /**
   * A function that converts lists to options.
   *
   * @return A function that converts lists to options.
   */
  public static  F, Option> List_Option() {
    return new F, Option>() {
      public Option f(final List as) {
        return as.toOption();
      }
    };
  }

  /**
   * A function that converts lists to eithers.
   *
   * @return A function that converts lists to eithers.
   */
  public static  F, F, Either>> List_Either() {
    return new F, F, Either>>() {
      public F, Either> f(final P1 a) {
        return new F, Either>() {
          public Either f(final List bs) {
            return bs.toEither(a);
          }
        };
      }
    };
  }

  /**
   * A function that converts lists to strings.
   */
  public static final F, String> List_String = new F, String>() {
    public String f(final List cs) {
      return asString(cs);
    }
  };

  /**
   * A function that converts lists to string buffers.
   */
  public static final F, StringBuffer> List_StringBuffer = new F, StringBuffer>() {
    public StringBuffer f(final List cs) {
      return new StringBuffer(asString(cs));
    }
  };

  /**
   * A function that converts lists to string builders.
   */
  public static final F, StringBuilder> List_StringBuilder = new F, StringBuilder>() {
    public StringBuilder f(final List cs) {
      return new StringBuilder(asString(cs));
    }
  };

  // END List ->

  // BEGIN Array ->

  /**
   * A function that converts arrays to lists.
   *
   * @return A function that converts arrays to lists.
   */
  public static  F, List> Array_List() {
    return new F, List>() {
      public List f(final Array as) {
        return as.toList();
      }
    };
  }

  /**
   * A function that converts arrays to streams.
   *
   * @return A function that converts arrays to streams.
   */
  public static  F, Stream> Array_Stream() {
    return new F, Stream>() {
      public Stream f(final Array as) {
        return as.toStream();
      }
    };
  }

  /**
   * A function that converts arrays to options.
   *
   * @return A function that converts arrays to options.
   */
  public static  F, Option> Array_Option() {
    return new F, Option>() {
      public Option f(final Array as) {
        return as.toOption();
      }
    };
  }

  /**
   * A function that converts arrays to eithers.
   *
   * @return A function that converts arrays to eithers.
   */
  public static  F, F, Either>> Array_Either() {
    return new F, F, Either>>() {
      public F, Either> f(final P1 a) {
        return new F, Either>() {
          public Either f(final Array bs) {
            return bs.toEither(a);
          }
        };
      }
    };
  }

  /**
   * A function that converts arrays to strings.
   */
  public static final F, String> Array_String = new F, String>() {
    public String f(final Array cs) {
      final StringBuilder sb = new StringBuilder();
      cs.foreachDoEffect(new Effect1() {
        public void f(final Character c) {
          sb.append(c);
        }
      });
      return sb.toString();
    }
  };

  /**
   * A function that converts arrays to string buffers.
   */
  public static final F, StringBuffer> Array_StringBuffer = new F, StringBuffer>() {
    public StringBuffer f(final Array cs) {
      final StringBuffer sb = new StringBuffer();
      cs.foreachDoEffect(new Effect1() {
        public void f(final Character c) {
          sb.append(c);
        }
      });
      return sb;
    }
  };

  /**
   * A function that converts arrays to string builders.
   */
  public static final F, StringBuilder> Array_StringBuilder =
      new F, StringBuilder>() {
        public StringBuilder f(final Array cs) {
          final StringBuilder sb = new StringBuilder();
          cs.foreachDoEffect((Character c) -> sb.append(c));
          return sb;
        }
      };

  // END Array ->

  // BEGIN Stream ->

  /**
   * A function that converts streams to lists.
   *
   * @return A function that converts streams to lists.
   */
  public static  F, List> Stream_List() {
    return new F, List>() {
      public List f(final Stream as) {
        return as.toList();
      }
    };
  }

  /**
   * A function that converts streams to arrays.
   *
   * @return A function that converts streams to arrays.
   */
  public static  F, Array> Stream_Array() {
    return new F, Array>() {
      public Array f(final Stream as) {
        return as.toArray();
      }
    };
  }

  /**
   * A function that converts streams to options.
   *
   * @return A function that converts streams to options.
   */
  public static  F, Option> Stream_Option() {
    return new F, Option>() {
      public Option f(final Stream as) {
        return as.toOption();
      }
    };
  }

  /**
   * A function that converts streams to eithers.
   *
   * @return A function that converts streams to eithers.
   */
  public static  F, F, Either>> Stream_Either() {
    return new F, F, Either>>() {
      public F, Either> f(final P1 a) {
        return new F, Either>() {
          public Either f(final Stream bs) {
            return bs.toEither(a);
          }
        };
      }
    };
  }

  /**
   * A function that converts streams to strings.
   */
  public static final F, String> Stream_String = new F, String>() {
    public String f(final Stream cs) {
      final StringBuilder sb = new StringBuilder();
      cs.foreachDoEffect((Character c) -> sb.append(c));
      return sb.toString();
    }
  };

  /**
   * A function that converts streams to string buffers.
   */
  public static final F, StringBuffer> Stream_StringBuffer =
      new F, StringBuffer>() {
        public StringBuffer f(final Stream cs) {
          final StringBuffer sb = new StringBuffer();
          cs.foreachDoEffect((Character c) -> sb.append(c));
          return sb;
        }
      };

  /**
   * A function that converts streams to string builders.
   */
  public static final F, StringBuilder> Stream_StringBuilder =
      new F, StringBuilder>() {
        public StringBuilder f(final Stream cs) {
          final StringBuilder sb = new StringBuilder();
          cs.foreachDoEffect((Character c) -> sb.append(c));
          return sb;
        }
      };

  // END Stream ->

  // BEGIN Option ->

  /**
   * A function that converts options to lists.
   *
   * @return A function that converts options to lists.
   */
  public static  F, List> Option_List() {
    return new F, List>() {
      public List f(final Option o) {
        return o.toList();
      }
    };
  }

  /**
   * A function that converts options to arrays.
   *
   * @return A function that converts options to arrays.
   */
  public static  F, Array> Option_Array() {
    return new F, Array>() {
      public Array f(final Option o) {
        return o.toArray();
      }
    };
  }

  /**
   * A function that converts options to streams.
   *
   * @return A function that converts options to streams.
   */
  public static  F, Stream> Option_Stream() {
    return new F, Stream>() {
      public Stream f(final Option o) {
        return o.toStream();
      }
    };
  }

  /**
   * A function that converts options to eithers.
   *
   * @return A function that converts options to eithers.
   */
  public static  F, F, Either>> Option_Either() {
    return new F, F, Either>>() {
      public F, Either> f(final P1 a) {
        return new F, Either>() {
          public Either f(final Option o) {
            return o.toEither(a);
          }
        };
      }
    };
  }

  /**
   * A function that converts options to strings.
   */
  public static final F, String> Option_String = new F, String>() {
    public String f(final Option o) {
      return asString(o.toList());
    }
  };

  /**
   * A function that converts options to string buffers.
   */
  public static final F, StringBuffer> Option_StringBuffer =
      new F, StringBuffer>() {
        public StringBuffer f(final Option o) {
          return new StringBuffer(asString(o.toList()));
        }
      };

  /**
   * A function that converts options to string builders.
   */
  public static final F, StringBuilder> Option_StringBuilder =
      new F, StringBuilder>() {
        public StringBuilder f(final Option o) {
          return new StringBuilder(asString(o.toList()));
        }
      };

  // END Option ->

    // BEGIN Effect

    public static F> Effect0_P1() {
        return e -> Effect0_P1(e);
    }

    public static P1 Effect0_P1(Effect0 e) {
        return Effect.f(e);
    }

    public static  F Effect1_F(Effect1 e) {
        return Effect.f(e);
    }

    public static  F, F> Effect1_F() {
        return e -> Effect1_F(e);
    }

    public static IO Effect_IO(Effect0 e) {
        return () ->{
            e.f();
            return Unit.unit();
        };
    }

    public static F> Effect_IO() {
        return e -> Effect_IO(e);
    }

    public static SafeIO Effect_SafeIO(Effect0 e) {
        return () -> {
            e.f();
            return unit();
        };
    }

    public static F> Effect_SafeIO() {
        return e -> Effect_SafeIO(e);
    }

    // END Effect

  // BEGIN Either ->

  /**
   * A function that converts eithers to lists.
   *
   * @return A function that converts eithers to lists.
   */
  public static  F, List> Either_ListA() {
    return new F, List>() {
      public List f(final Either e) {
        return e.left().toList();
      }
    };
  }

  /**
   * A function that converts eithers to lists.
   *
   * @return A function that converts eithers to lists.
   */
  public static  F, List> Either_ListB() {
    return new F, List>() {
      public List f(final Either e) {
        return e.right().toList();
      }
    };
  }

  /**
   * A function that converts eithers to arrays.
   *
   * @return A function that converts eithers to arrays.
   */
  public static  F, Array> Either_ArrayA() {
    return new F, Array>() {
      public Array f(final Either e) {
        return e.left().toArray();
      }
    };
  }

  /**
   * A function that converts eithers to arrays.
   *
   * @return A function that converts eithers to arrays.
   */
  public static  F, Array> Either_ArrayB() {
    return new F, Array>() {
      public Array f(final Either e) {
        return e.right().toArray();
      }
    };
  }

  /**
   * A function that converts eithers to streams.
   *
   * @return A function that converts eithers to streams.
   */
  public static  F, Stream> Either_StreamA() {
    return new F, Stream>() {
      public Stream f(final Either e) {
        return e.left().toStream();
      }
    };
  }

  /**
   * A function that converts eithers to streams.
   *
   * @return A function that converts eithers to streams.
   */
  public static  F, Stream> Either_StreamB() {
    return new F, Stream>() {
      public Stream f(final Either e) {
        return e.right().toStream();
      }
    };
  }

  /**
   * A function that converts eithers to options.
   *
   * @return A function that converts eithers to options.
   */
  public static  F, Option> Either_OptionA() {
    return new F, Option>() {
      public Option f(final Either e) {
        return e.left().toOption();
      }
    };
  }

  /**
   * A function that converts eithers to options.
   *
   * @return A function that converts eithers to options.
   */
  public static  F, Option> Either_OptionB() {
    return new F, Option>() {
      public Option f(final Either e) {
        return e.right().toOption();
      }
    };
  }

  /**
   * A function that converts eithers to strings.
   *
   * @return A function that converts eithers to strings.
   */
  public static  F, String> Either_StringA() {
    return new F, String>() {
      public String f(final Either e) {
        return asString(e.left().toList());
      }
    };
  }

  /**
   * A function that converts eithers to strings.
   *
   * @return A function that converts eithers to strings.
   */
  public static  F, String> Either_StringB() {
    return new F, String>() {
      public String f(final Either e) {
        return asString(e.right().toList());
      }
    };
  }

  /**
   * A function that converts eithers to string buffers.
   *
   * @return A function that converts eithers to string buffers.
   */
  public static  F, StringBuffer> Either_StringBufferA() {
    return new F, StringBuffer>() {
      public StringBuffer f(final Either e) {
        return new StringBuffer(asString(e.left().toList()));
      }
    };
  }

  /**
   * A function that converts eithers to string buffers.
   *
   * @return A function that converts eithers to string buffers.
   */
  public static  F, StringBuffer> Either_StringBufferB() {
    return new F, StringBuffer>() {
      public StringBuffer f(final Either e) {
        return new StringBuffer(asString(e.right().toList()));
      }
    };
  }

  /**
   * A function that converts eithers to string builders.
   *
   * @return A function that converts eithers to string builders.
   */
  public static  F, StringBuilder> Either_StringBuilderA() {
    return new F, StringBuilder>() {
      public StringBuilder f(final Either e) {
        return new StringBuilder(asString(e.left().toList()));
      }
    };
  }

  /**
   * A function that converts eithers to string builders.
   *
   * @return A function that converts eithers to string builders.
   */
  public static  F, StringBuilder> Either_StringBuilderB() {
    return new F, StringBuilder>() {
      public StringBuilder f(final Either e) {
        return new StringBuilder(asString(e.right().toList()));
      }
    };
  }

  // END Either ->

    // BEGIN F

    public static  SafeIO F_SafeIO(F f) {
        return () -> f.f(unit());
    }

    public static  F, SafeIO> F_SafeIO() {
        return f -> F_SafeIO(f);
    }

    // END F

  // BEGIN String ->

  /**
   * A function that converts strings to lists.
   */
  public static final F> String_List = new F>() {
    public List f(final String s) {
      return fromString(s);
    }
  };

  /**
   * A function that converts strings to arrays.
   */
  public static final F> String_Array = new F>() {
    public Array f(final String s) {
      return fromString(s).toArray();
    }
  };

  /**
   * A function that converts strings to options.
   */
  public static final F> String_Option = new F>() {
    public Option f(final String s) {
      return fromString(s).toOption();
    }
  };

  /**
   * A function that converts string to eithers.
   *
   * @return A function that converts string to eithers.
   */
  public static  F, F>> String_Either() {
    return new F, F>>() {
      public F> f(final P1 a) {
        return new F>() {
          public Either f(final String s) {
            return fromString(s).toEither(a);
          }
        };
      }
    };
  }

  /**
   * A function that converts strings to streams.
   */
  public static final F> String_Stream = new F>() {
    public Stream f(final String s) {
      return fromString(s).toStream();
    }
  };

  /**
   * A function that converts strings to string buffers.
   */
  public static final F String_StringBuffer = new F() {
    public StringBuffer f(final String s) {
      return new StringBuffer(s);
    }
  };

  /**
   * A function that converts strings to string builders.
   */
  public static final F String_StringBuilder = new F() {
    public StringBuilder f(final String s) {
      return new StringBuilder(s);
    }
  };

  // END String ->

  // BEGIN StringBuffer ->

  /**
   * A function that converts string buffers to lists.
   */
  public static final F> StringBuffer_List = new F>() {
    public List f(final StringBuffer s) {
      return fromString(s.toString());
    }
  };

  /**
   * A function that converts string buffers to arrays.
   */
  public static final F> StringBuffer_Array = new F>() {
    public Array f(final StringBuffer s) {
      return fromString(s.toString()).toArray();
    }
  };

  /**
   * A function that converts string buffers to streams.
   */
  public static final F> StringBuffer_Stream =
      new F>() {
        public Stream f(final StringBuffer s) {
          return fromString(s.toString()).toStream();
        }
      };

  /**
   * A function that converts string buffers to options.
   */
  public static final F> StringBuffer_Option =
      new F>() {
        public Option f(final StringBuffer s) {
          return fromString(s.toString()).toOption();
        }
      };

  /**
   * A function that converts string buffers to eithers.
   *
   * @return A function that converts string buffers to eithers.
   */
  public static  F, F>> StringBuffer_Either() {
    return new F, F>>() {
      public F> f(final P1 a) {
        return new F>() {
          public Either f(final StringBuffer s) {
            return fromString(s.toString()).toEither(a);
          }
        };
      }
    };
  }

  /**
   * A function that converts string buffers to strings.
   */
  public static final F StringBuffer_String = new F() {
    public String f(final StringBuffer s) {
      return s.toString();
    }
  };

  /**
   * A function that converts string buffers to string builders.
   */
  public static final F StringBuffer_StringBuilder = new F() {
    public StringBuilder f(final StringBuffer s) {
      return new StringBuilder(s);
    }
  };

  // END StringBuffer ->

  // BEGIN StringBuilder ->

  /**
   * A function that converts string builders to lists.
   */
  public static final F> StringBuilder_List = new F>() {
    public List f(final StringBuilder s) {
      return fromString(s.toString());
    }
  };

  /**
   * A function that converts string builders to arrays.
   */
  public static final F> StringBuilder_Array =
      new F>() {
        public Array f(final StringBuilder s) {
          return fromString(s.toString()).toArray();
        }
      };

  /**
   * A function that converts string builders to streams.
   */
  public static final F> StringBuilder_Stream =
      new F>() {
        public Stream f(final StringBuilder s) {
          return fromString(s.toString()).toStream();
        }
      };

  /**
   * A function that converts string builders to options.
   */
  public static final F> StringBuilder_Option =
      new F>() {
        public Option f(final StringBuilder s) {
          return fromString(s.toString()).toOption();
        }
      };

  /**
   * A function that converts string builders to eithers.
   *
   * @return A function that converts string builders to eithers.
   */
  public static  F, F>> StringBuilder_Either() {
    return new F, F>>() {
      public F> f(final P1 a) {
        return new F>() {
          public Either f(final StringBuilder s) {
            return fromString(s.toString()).toEither(a);
          }
        };
      }
    };
  }

  /**
   * A function that converts string builders to strings.
   */
  public static final F StringBuilder_String = new F() {
    public String f(final StringBuilder s) {
      return s.toString();
    }
  };

  /**
   * A function that converts string builders to string buffers.
   */
  public static final F StringBuilder_StringBuffer = new F() {
    public StringBuffer f(final StringBuilder s) {
      return new StringBuffer(s);
    }
  };

  // END StringBuilder ->


    // BEGIN Try

    public static  SafeIO> Try_SafeIO(Try0 t) {
        return F_SafeIO(u -> Try.f(t)._1());
    }

    public static  F, SafeIO>> Try_SafeIO() {
        return t -> Try_SafeIO(t);
    }

    public static  IO Try_IO(Try0 t) {
        return () -> t.f();
    }

    public static  F, IO> Try_IO() {
        return t -> Try_IO(t);
    }

    public static  F> Try_F(Try1 t) {
        return Try.f(t);
    }

    public static  F, F>> Try_F() {
        return t -> Try_F(t);
    }

    // END Try

    // BEGIN TryEffect

    static public  P1> TryEffect_P(final TryEffect0 t) {
        return TryEffect.f(t);
    }


    // END TryEffect

}