Micha Kops avatar Micha Kops committed 81d59cd

initial import.

Comments (0)

Files changed (7)

+.settings
+target
+.classpath
+.project
+
+# Functional Programming Paradigms in Java Examples #
+
+Some examples from different frameworks like functionaljava or Google Guava - for the full tutorial, please take a look at my blog at [www.hascode.com].
+
+	[www.hascode.com]:http://www.hascode.com/
+	
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>com.hascode.tutorial</groupId>
+  <artifactId>functionaljava-example</artifactId>
+  <version>0.0.1</version>
+  <dependencies>
+  	<dependency>
+  		<groupId>org.functionaljava</groupId>
+  		<artifactId>functionaljava</artifactId>
+  		<version>3.1</version>
+  	</dependency>
+  	<dependency>
+  		<groupId>com.google.guava</groupId>
+  		<artifactId>guava</artifactId>
+  		<version>12.0</version>
+  	</dependency>
+  </dependencies>
+</project>

src/main/java/com/hascode/tutorial/EitherExample.java

+package com.hascode.tutorial;
+
+import fj.data.Either;
+
+public class EitherExample {
+	public static void main(final String[] args) {
+		Either<Exception, Integer> result1 = parseInteger("123456h4");
+		System.out.println(result1.isLeft()); // true
+		System.out.println(result1.left().value().getClass()); // "class java.lang.NumberFormatException"
+
+		Either<Exception, Integer> result2 = parseInteger("123456");
+		System.out.println(result2.isLeft()); // false
+		System.out.println(result2.right().value()); // 123456
+	}
+
+	private static Either<Exception, Integer> parseInteger(final String string) {
+		try {
+			return Either.right(Integer.valueOf(string));
+		} catch (Exception e) {
+			return Either.left(e);
+		}
+	}
+}

src/main/java/com/hascode/tutorial/GuavaPredicates.java

+package com.hascode.tutorial;
+
+import static com.google.common.collect.Lists.newArrayList;
+
+import java.util.List;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.Iterables;
+
+public class GuavaPredicates {
+
+	public static void main(final String[] args) {
+		List<Book> books = newArrayList(new Book("Some book", "Arnold Author",
+				1998), new Book("Programmers are weird", "Some author", 2011),
+				new Book("Another book", "Another author", 2012), new Book(
+						"Ye ole book", "Ye authorr", 1711), new Book(
+						"Some book - second edition", "Arnold Author", 2010));
+
+		Predicate<Book> newerThanYear2000 = new Predicate<GuavaPredicates.Book>() {
+			public boolean apply(final Book book) {
+				return book.getYearPublished() >= 2000;
+			}
+		};
+
+		Predicate<Book> byAuthorArnold = new Predicate<GuavaPredicates.Book>() {
+			public boolean apply(final Book book) {
+				return book.getAuthor().equals("Arnold Author");
+			}
+		};
+
+		System.out.println("books published in year 2000 and later");
+		System.out.println(Iterables.filter(books, newerThanYear2000)
+				.toString());
+
+		System.out.println("books by author=Arnold Author");
+		System.out.println(Iterables.filter(books, byAuthorArnold).toString());
+	}
+
+	static class Book {
+		final String title;
+		final String author;
+		final int yearPublished;
+
+		public Book(final String title, final String author,
+				final int yearPublished) {
+			this.title = title;
+			this.author = author;
+			this.yearPublished = yearPublished;
+		}
+
+		public String getTitle() {
+			return title;
+		}
+
+		public String getAuthor() {
+			return author;
+		}
+
+		public int getYearPublished() {
+			return yearPublished;
+		}
+
+		@Override
+		public String toString() {
+			return "Book [title=" + title + ", author=" + author
+					+ ", yearPublished=" + yearPublished + "]";
+		}
+
+	}
+}

src/main/java/com/hascode/tutorial/LazyP1Example.java

+package com.hascode.tutorial;
+
+import fj.P1;
+import fj.data.Either;
+
+public class LazyP1Example {
+	public static void main(final String[] args) {
+		P1<Either<Exception, Integer>> result1 = parseInteger("123456h4");
+		System.out.println(result1._1().isLeft()); // true
+		System.out.println(result1._1().left().value().getClass()); // "class java.lang.NumberFormatException"
+
+		P1<Either<Exception, Integer>> result2 = parseInteger("123456");
+		System.out.println(result2._1().isLeft()); // false
+		System.out.println(result2._1().right().value()); // 123456
+	}
+
+	private static P1<Either<Exception, Integer>> parseInteger(
+			final String string) {
+		return new P1<Either<Exception, Integer>>() {
+			@Override
+			public Either<Exception, Integer> _1() {
+				try {
+					return Either.right(Integer.valueOf(string));
+				} catch (Exception e) {
+					return Either.left(e);
+				}
+			}
+		};
+	}
+}

src/main/java/com/hascode/tutorial/OptionExample.java

+package com.hascode.tutorial;
+
+import fj.data.Option;
+
+public class OptionExample {
+	public static void main(final String[] args) {
+		Option<Integer> result1 = parseInteger("1234G46");
+		System.out.println(String.format("isNone: %s / isSome: %s",
+				result1.isNone(), result1.isSome())); // "isNone: true / isSome: false"
+
+		Option<Integer> result2 = parseInteger("1234");
+		System.out.println(String.format("isNone: %s / isSome: %s",
+				result2.isNone(), result2.isSome())); // "isNone: false / isSome: true"
+		System.out.println(result2.some()); // 1234
+	}
+
+	private static Option<Integer> parseInteger(final String string) {
+		try {
+			return Option.some(Integer.valueOf(string));
+		} catch (Exception e) {
+			return Option.none();
+		}
+	}
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.