Пример #1
0
 /**
  * Returns a parser that produces the given stream of characters or fails otherwise.
  *
  * @param missing The error if the producing stream could not supply more characters.
  * @param sat The error if a character was produced that is not the given stream of characters.
  * @param cs The stream of characters to produce.
  * @return A parser that produces the given stream of characters or fails otherwise.
  */
 public static <E> Parser<Stream<Character>, Stream<Character>, E> characters(
     final F0<E> missing, final F<Character, E> sat, final Stream<Character> cs) {
   return cs.isEmpty()
       ? Parser.<Stream<Character>, Stream<Character>, E>value(Stream.<Character>nil())
       : character(missing, sat, cs.head())
           .bind(characters(missing, sat, cs.tail()._1()), Stream.<Character>cons_());
 }
Пример #2
0
 public static void main(final String[] a) {
   final Stream<Integer> tdl =
       iterate(flip(subtract).f(1), 999).takeWhile(intOrd.isGreaterThan(99));
   intShow.println(
       tdl.tails()
           .bind(tdl.zipWith(multiply))
           .filter(
               new F<Integer, Boolean>() {
                 public Boolean f(final Integer i) {
                   final Stream<Character> s = intShow.show(i);
                   return streamEqual(charEqual).eq(s.reverse().take(3), s.take(3));
                 }
               })
           .foldLeft1(intOrd.max));
 }
Пример #3
0
 /**
  * Returns a stream of the elements of this vector.
  *
  * @return a stream of the elements of this vector.
  */
 public Stream<A> toStream() {
   return Stream.cons(
       head._1(),
       new P1<Stream<A>>() {
         public Stream<A> _1() {
           return tail.toStream();
         }
       });
 }
Пример #4
0
 /**
  * Returns a stream of the elements of this vector.
  *
  * @return a stream of the elements of this vector.
  */
 public Stream<A> toStream() {
   return Stream.cons(
       _1(),
       new P1<Stream<A>>() {
         public Stream<A> _1() {
           return Stream.single(_2());
         }
       });
 }
Пример #5
0
 /**
  * Constructs a lazy string from a String.
  *
  * @param s A string from which to construct a lazy string.
  * @return A lazy string with the characters from the given string.
  */
 public static LazyString str(final String s) {
   return new LazyString(
       Stream.unfold(
           new F<P2<String, Integer>, Option<P2<Character, P2<String, Integer>>>>() {
             public Option<P2<Character, P2<String, Integer>>> f(final P2<String, Integer> o) {
               final String s = o._1();
               final int n = o._2();
               final Option<P2<Character, P2<String, Integer>>> none = none();
               return s.length() <= n ? none : some(p(s.charAt(n), p(s, n + 1)));
             }
           },
           p(s, 0)));
 }
Пример #6
0
 /**
  * Splits this lazy string by characters matching the given predicate.
  *
  * @param p A predicate that matches characters to be considered delimiters.
  * @return A stream of the substrings in this lazy string, when separated by the given predicate.
  */
 public Stream<LazyString> split(final F<Character, Boolean> p) {
   final Stream<Character> findIt = s.dropWhile(p);
   final P2<Stream<Character>, Stream<Character>> ws = findIt.split(p);
   return findIt.isEmpty()
       ? Stream.<LazyString>nil()
       : Stream.cons(
           fromStream(ws._1()),
           new P1<Stream<LazyString>>() {
             public Stream<LazyString> _1() {
               return fromStream(ws._2()).split(p);
             }
           });
 }
Пример #7
0
 /**
  * Returns the caracter at the specified index.
  *
  * @param index The index for the character to be returned.
  * @return The character at the specified index.
  */
 public char charAt(final int index) {
   return s.index(index);
 }
Пример #8
0
 /**
  * The length of the lazy string. Note that this operation is O(n).
  *
  * @return The length of this lazy string.
  */
 public int length() {
   return s.length();
 }
Пример #9
0
 /**
  * Joins the given stream of lazy strings into one, separated by spaces.
  *
  * @param str A stream of lazy strings to join by spaces.
  * @return A new lazy string, consisting of the given strings with spaces in between.
  */
 public static LazyString unwords(final Stream<LazyString> str) {
   return fromStream(join(str.intersperse(str(" ")).map(toStream)));
 }
Пример #10
0
 /**
  * Returns true if the given lazy string is a substring of this lazy string.
  *
  * @param cs A substring to find in this lazy string.
  * @return True if the given string is a substring of this string, otherwise False.
  */
 public boolean contains(final LazyString cs) {
   return or(s.tails().map(compose(startsWith().f(cs), fromStream)));
 }
Пример #11
0
 /**
  * Returns a parser that produces the given number of characters, or fails with the given error.
  *
  * @param missing The error if the given number of characters is unavailable.
  * @param n The number of characters to produce in the parse result.
  * @return A parser that produces the given number of characters, or fails with the given error.
  */
 public static <E> Parser<Stream<Character>, Stream<Character>, E> characters(
     final F0<E> missing, final int n) {
   return n <= 0
       ? Parser.<Stream<Character>, Stream<Character>, E>value(Stream.<Character>nil())
       : character(missing).bind(characters(missing, n - 1), Stream.<Character>cons_());
 }
Пример #12
0
 /**
  * Returns the first index of the given substring in this lazy string, if present.
  *
  * @param cs A substring to find in this lazy string.
  * @return The first index of the given substring in this lazy string, or None if there is no such
  *     substring.
  */
 public Option<Integer> indexOf(final LazyString cs) {
   return s.substreams().indexOf(eqS.eq(cs.s));
 }
Пример #13
0
 /**
  * Returns the first index of the given character in this lazy string, if present.
  *
  * @param c A character to find in this lazy string.
  * @return The first index of the given character in this lazy string, or None if the character is
  *     not present.
  */
 public Option<Integer> indexOf(final char c) {
   return s.indexOf(Equal.charEqual.eq(c));
 }
Пример #14
0
 /**
  * Returns the reverse of this string.
  *
  * @return the reverse of this string.
  */
 public LazyString reverse() {
   return fromStream(s.reverse());
 }
Пример #15
0
 /**
  * Checks if this string is empty.
  *
  * @return True if there are no characters in this string, otherwise False.
  */
 public boolean isEmpty() {
   return s.isEmpty();
 }
Пример #16
0
 /**
  * Returns all but the first character of this string.
  *
  * @return All but the first character of this string, or error if the string is empty.
  */
 public LazyString tail() {
   return fromStream(s.tail()._1());
 }
Пример #17
0
 /**
  * Returns the first character of this string.
  *
  * @return The first character of this string, or error if the string is empty.
  */
 public char head() {
   return s.head();
 }
Пример #18
0
 /**
  * Gets the specified subsequence of this lazy string. This operation does not fail for indexes
  * that are out of bounds. If the start index is past the end of this lazy string, then the
  * resulting character sequence will be empty. If the end index is past the end of this lazy
  * string, then the resulting character sequence will be truncated.
  *
  * @param start The character index of this lazy string at which to start the subsequence.
  * @param end The character index of this lazy string at which to end the subsequence.
  * @return A character sequence containing the specified character subsequence.
  */
 public CharSequence subSequence(final int start, final int end) {
   return fromStream(s.drop(start).take(end - start));
 }
Пример #19
0
 /**
  * Returns a parser that repeats application of this parser zero or many times.
  *
  * @return A parser that repeats application of this parser zero or many times.
  */
 public Parser<I, Stream<A>, E> repeat() {
   return repeat1().or(() -> value(Stream.<A>nil()));
 }
Пример #20
0
 /**
  * Returns the String representation of this lazy string.
  *
  * @return The String representation of this lazy string.
  */
 public String toStringEager() {
   final StringBuilder builder = new StringBuilder(length() + 16);
   s.foreachDoEffect(c -> builder.append(c.charValue()));
   return builder.toString();
 }
Пример #21
0
 public String toStringLazy() {
   return s.isEmpty() ? "" : "LazyString(" + Show.charShow.showS(s.head()) + ", ?)";
 }
Пример #22
0
 public LazyString bind(F<Character, LazyString> f) {
   return fromStream(s.bind(c -> f.f(c).toStream()));
 }
Пример #23
0
 public LazyString map(F<Character, Character> f) {
   return fromStream(s.map(f));
 }
Пример #24
0
 /**
  * Appends the given lazy string to the end of this lazy string.
  *
  * @param cs A lazy string to append to this one.
  * @return A new lazy string that is the concatenation of this string and the given string.
  */
 public LazyString append(final LazyString cs) {
   return fromStream(s.append(cs.s));
 }