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 List::toArray;
  }

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

  /**
   * A function that converts lists to options.
   *
   * @return A function that converts lists to options.
   */
  public static  F, Option> List_Option() {
    return List::headOption;
  }

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

  /**
   * A function that converts lists to strings.
   */
  public static final F, String> List_String = List::asString;

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

  /**
   * A function that converts lists to string builders.
   */
  public static final F, StringBuilder> List_StringBuilder = cs -> 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 Array::toList;
  }

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

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

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

  /**
   * A function that converts arrays to strings.
   */
  public static final F, String> Array_String = cs -> {
      final StringBuilder sb = new StringBuilder(cs.length());
      cs.foreachDoEffect(sb::append);
      return sb.toString();
  };

  /**
   * A function that converts arrays to string buffers.
   */
  public static final F, StringBuffer> Array_StringBuffer = cs -> {
      final StringBuffer sb = new StringBuffer(cs.length());
      cs.foreachDoEffect(sb::append);
      return sb;
  };

  /**
   * A function that converts arrays to string builders.
   */
  public static final F, StringBuilder> Array_StringBuilder = cs -> {
      final StringBuilder sb = new StringBuilder(cs.length());
      cs.foreachDoEffect(sb::append);
      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 Stream::toList;
  }

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

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

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

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

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

  /**
   * A function that converts streams to string builders.
   */
  public static final F, StringBuilder> Stream_StringBuilder = cs -> {
      final StringBuilder sb = new StringBuilder();
      cs.foreachDoEffect(sb::append);
      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 Option::toList;
  }

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

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

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

  /**
   * A function that converts options to strings.
   */
  public static final F, String> Option_String = o -> asString(o.toList());

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

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

  // END Option ->

    // BEGIN Effect

    public static F> Effect0_P1() {
        return Conversions::Effect0_P1;
    }

    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 Conversions::Effect1_F;
    }

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

    public static F> Effect_IO() {
        return Conversions::Effect_IO;
    }

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

    public static F> Effect_SafeIO() {
        return Conversions::Effect_SafeIO;
    }

    // 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 e -> 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 e -> 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 e -> 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 e -> 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 e -> 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 e -> 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 e -> 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 e -> 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 e -> 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 e -> 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 e -> 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 e -> 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 e -> 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 e -> 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 Conversions::F_SafeIO;
    }

    // END F

  // BEGIN String ->

  /**
   * A function that converts strings to lists.
   */
  public static final F> String_List = List::fromString;

  /**
   * A function that converts strings to arrays.
   */
  public static final F> String_Array = s -> fromString(s).toArray();

  /**
   * A function that converts strings to options.
   */
  public static final F> String_Option = s -> fromString(s).headOption();

  /**
   * A function that converts string to eithers.
   *
   * @return A function that converts string to eithers.
   */
  public static  F, F>> String_Either() {
    return a -> s -> fromString(s).toEither(a);
  }

  /**
   * A function that converts strings to streams.
   */
  public static final F> String_Stream = s -> fromString(s).toStream();

  /**
   * A function that converts strings to string buffers.
   */
  public static final F String_StringBuffer = StringBuffer::new;

  /**
   * A function that converts strings to string builders.
   */
  public static final F String_StringBuilder = StringBuilder::new;

  // END String ->

  // BEGIN StringBuffer ->

  /**
   * A function that converts string buffers to lists.
   */
  public static final F> StringBuffer_List = s -> fromString(s.toString());

  /**
   * A function that converts string buffers to arrays.
   */
  public static final F> StringBuffer_Array = s -> fromString(s.toString()).toArray();

  /**
   * A function that converts string buffers to streams.
   */
  public static final F> StringBuffer_Stream = s -> fromString(s.toString()).toStream();

  /**
   * A function that converts string buffers to options.
   */
  public static final F> StringBuffer_Option = s -> fromString(s.toString()).headOption();

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

  /**
   * A function that converts string buffers to strings.
   */
  public static final F StringBuffer_String = StringBuffer::toString;

  /**
   * A function that converts string buffers to string builders.
   */
  public static final F StringBuffer_StringBuilder = StringBuilder::new;

  // END StringBuffer ->

  // BEGIN StringBuilder ->

  /**
   * A function that converts string builders to lists.
   */
  public static final F> StringBuilder_List = s -> fromString(s.toString());

  /**
   * A function that converts string builders to arrays.
   */
  public static final F> StringBuilder_Array = s -> fromString(s.toString()).toArray();

  /**
   * A function that converts string builders to streams.
   */
  public static final F> StringBuilder_Stream = s -> fromString(s.toString()).toStream();

  /**
   * A function that converts string builders to options.
   */
  public static final F> StringBuilder_Option = s -> fromString(s.toString()).headOption();

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

  /**
   * A function that converts string builders to strings.
   */
  public static final F StringBuilder_String = StringBuilder::toString;

  /**
   * A function that converts string builders to string buffers.
   */
  public static final F StringBuilder_StringBuffer = StringBuffer::new;

  // 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 Conversions::Try_SafeIO;
    }

    public static  IO Try_IO(Try0 t) {
        return t::f;
    }

    public static  F, IO> Try_IO() {
        return Conversions::Try_IO;
    }

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

    public static  F, F>> Try_F() {
        return Conversions::Try_F;
    }

    // END Try

    // BEGIN TryEffect

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


    // END TryEffect

}