Skip to content

Instantly share code, notes, and snippets.

@aegypius
Created October 29, 2011 13:07
Show Gist options
  • Save aegypius/1324426 to your computer and use it in GitHub Desktop.
Save aegypius/1324426 to your computer and use it in GitHub Desktop.
Testing Highlighting
pygments: true
markdown: redcarpet
redcarpet:
extensions: [:smart, :fenced_code, :gh_blockcode]
layout title
post
Testing Features
package foo;

import java.util.Iterator;

/**
 * the fibonacci series implemented as an Iterable.
 */
public final class Fibonacci implements Iterable<Integer> {
  /** the next and previous members of the series. */
  private int a = 1, b = 1;

  @Override
  public Iterator<Integer> iterator() {
    return new Iterator<Integer>() {
      /** the series is infinite. */
      public boolean hasNext() { return true; }
      public Integer next() {
        int tmp = a;
        a += b;
        b = tmp;
        return a;
      }
      public void remove() { throw new UnsupportedOperationException(); }
    };
  }

  /**
   * the n<sup>th</sup> element of the given series.
   * @throws NoSuchElementException if there are less than n elements in the
   *   given Iterable's {@link Iterable#iterator iterator}.
   */
  public static <T>
  T nth(int n, Iterable<T> iterable) {
    Iterator<? extends T> it = iterable.iterator();
    while (--n > 0) {
      it.next();
    }
    return it.next();
  }

  public static void main(String[] args) {
    System.out.print(nth(10, new Fibonacci()));
  }
}

{% highlight java linenos=table %} package foo;

import java.util.Iterator;

/**

  • the fibonacci series implemented as an Iterable. / public final class Fibonacci implements Iterable { /* the next and previous members of the series. */ private int a = 1, b = 1;

@Override public Iterator iterator() { return new Iterator() { /** the series is infinite. */ public boolean hasNext() { return true; } public Integer next() { int tmp = a; a += b; b = tmp; return a; } public void remove() { throw new UnsupportedOperationException(); } }; }

/**

  • the nth element of the given series.
  • @throws NoSuchElementException if there are less than n elements in the
  • given Iterable's {@link Iterable#iterator iterator}. */ public static T nth(int n, Iterable iterable) { Iterator<? extends T> it = iterable.iterator(); while (--n > 0) { it.next(); } return it.next(); }

public static void main(String[] args) { System.out.print(nth(10, new Fibonacci())); } } {% endhighlight %}

Copy link

ghost commented Dec 16, 2014

thanks

pygments: true

change to

highlighter: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment