Examples BGGA

Examples (1.5)

These examples use the Java 7 closures proposal and have corresponding examples that use traditional Java syntax in the Functional Java download. The BGGA closure prototype is required to compile these examples, which can be downloaded from http://javac.info/. Any JVM version 1.5 or higher is required to use the Functional Java library or run the compiled examples.


Array exists §

Checks for the existence of a String that has all lower case characters. In this case it is true (since, the case of "what"), but without this specific case, it is false. This example uses List.forall to test the characters of the String.

import fj.data.Array;
import static fj.data.Array.array;
import static fj.data.List.fromString;
import static java.lang.Character.isLowerCase;

public final class Array_exists {
  public static void main(final String[] args) {
    final Array<String> a = array("Hello", "There", "what", "DAY", "iS", "iT");
    final boolean b = a.exists({String s => fromString(s).forall({char c => isLowerCase(c)})});
    System.out.println(b); // true ("what" provides the only example; try removing it)
  }
}

Array filter §

Removes elements from an array that do not meet a certain criteria. In this case, we are using an array of integers and the filter removes any odd numbers.

import fj.data.Array;
import static fj.data.Array.array;
import static fj.pre.Show.arrayShow;
import static fj.pre.Show.intShow;

public final class Array_filter {
  public static void main(final String[] args) {
    final Array<Integer> a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
    final Array<Integer> b = a.filter({int i => i % 2 == 0});
    arrayShow(intShow).println(b); // {44,22,90,98,1078,6,64,6,42}
  }
}

Array foldLeft §

Reduces the list applying a function per element. In this case, the fold sums the elements with a starting value of 0. Since 0 + 97 + 44 + 67 + 3 + 22 + 90 + 1 + 77 + 98 + 1078 + 6 + 64 + 6 + 79 + 42 == 1774 the result of the fold is 1774.

import fj.data.Array;
import static fj.data.Array.array;

public final class Array_foldLeft {
  public static void main(final String[] args) {
    final Array<Integer> a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
    final int b = a.<Integer>foldLeft({int i => {int j => i + j}}, 0);
    System.out.println(b); // 1774
  }
}

Array forall §

Checks that all Strings in the array have lower case characters. In this case, the check fails because of the case of "There", however, the removal of this case produces a result of true.

import fj.data.Array;
import static fj.data.Array.array;
import static fj.data.List.fromString;
import static java.lang.Character.isLowerCase;

public final class Array_forall {
  public static void main(final String[] args) {
    final Array<String> a = array("hello", "There", "what", "day", "is", "it");
    final boolean b = a.forall({String s => fromString(s).forall({char c => isLowerCase(c)})});
    System.out.println(b); // false ("There" is a counter-example; try removing it)
  }
}

Array map §

Maps a function across the array of integers. In this case, the function adds 42 to each element of the array to produce a new array.

import fj.data.Array;
import static fj.data.Array.array;
import static fj.pre.Show.arrayShow;
import static fj.pre.Show.intShow;

public final class Array_map {
  public static void main(final String[] args) {
    final Array<Integer> a = array(1, 2, 3);
    final Array<Integer> b = a.map({int i => i + 42});
    arrayShow(intShow).println(b); // {43,44,45}
  }
}

List map §

Maps a function across a list of integers. This is similar to the Array map. This example adds 42 to each element of the list to produce a new list.

import static fj.data.List.list;
import fj.data.List;
import static fj.pre.Show.intShow;
import static fj.pre.Show.listShow;

public final class List_map {
  public static void main(final String[] args) {
    final List<Integer> a = list(1, 2, 3);
    final List<Integer> b = a.map({int i => i + 42});
    listShow(intShow).println(b); // [43,44,45]
  }
}

Option bind §

Binds a function across the optional value type. The function checks if the contained value is even and if it is multiples that value by 3 and returns that new value. If the contained value is odd (or if there is no value), then no value is returned (none).

import fj.data.Option;
import static fj.data.Option.none;
import static fj.data.Option.some;
import static fj.pre.Show.intShow;
import static fj.pre.Show.optionShow;

public final class Option_bind {
  public static void main(final String[] args) {
    final Option<Integer> o1 = some(7);
    final Option<Integer> o2 = some(8);
    final Option<Integer> o3 = none();
    final Option<Integer> p1 = o1.bind({int i => i % 2 == 0 ? some(i * 3) : Option.<Integer>none()});
    final Option<Integer> p2 = o2.bind({int i => i % 2 == 0 ? some(i * 3) : Option.<Integer>none()});
    final Option<Integer> p3 = o3.bind({int i => i % 2 == 0 ? some(i * 3) : Option.<Integer>none()});
    optionShow(intShow).println(p1); // None
    optionShow(intShow).println(p2); // Some(24)
    optionShow(intShow).println(p3); // None
  }
}

Option filter §

Removes the value from the optional value if it does not match a given predicate. In this case the condition for preservation is that the contained value is an even number.

import fj.data.Option;
import static fj.data.Option.none;
import static fj.data.Option.some;
import static fj.pre.Show.intShow;
import static fj.pre.Show.optionShow;

public final class Option_filter {
  public static void main(final String[] args) {
    final Option<Integer> o1 = some(7);
    final Option<Integer> o2 = none();
    final Option<Integer> o3 = some(8);
    final Option<Integer> p1 = o1.filter({int i => i % 2 == 0});
    final Option<Integer> p2 = o2.filter({int i => i % 2 == 0});
    final Option<Integer> p3 = o3.filter({int i => i % 2 == 0});
    optionShow(intShow).println(p1); // None
    optionShow(intShow).println(p2); // None
    optionShow(intShow).println(p3); // Some(8)
  }
}

Option map §

Maps a function across the optional value type. The function adds 42 to any contained value.

import fj.data.Option;
import static fj.data.Option.none;
import static fj.data.Option.some;
import static fj.pre.Show.intShow;
import static fj.pre.Show.optionShow;

public final class Option_map {
  public static void main(final String[] args) {
    final Option<Integer> o1 = some(7);
    final Option<Integer> o2 = none();
    final Option<Integer> p1 = o1.map({int i => i + 42});
    final Option<Integer> p2 = o2.map({int i => i + 42});
    optionShow(intShow).println(p1); // Some(49)
    optionShow(intShow).println(p2); // None
  }
}