Skip to content

Instantly share code, notes, and snippets.

@missingfaktor
Created September 1, 2011 17:49
Show Gist options
  • Select an option

  • Save missingfaktor/1186763 to your computer and use it in GitHub Desktop.

Select an option

Save missingfaktor/1186763 to your computer and use it in GitHub Desktop.
10(?) Functional Java oneliners(?) to impress(?) your friends
// Ref: http://solog.co/scala/10-scala-one-liners-to-impress-your-friends.html
import static fj.Ord.intOrd;
import static fj.P.p;
import static fj.Show.booleanShow;
import static fj.Show.intShow;
import static fj.Show.listShow;
import static fj.Show.stringShow;
import static fj.data.List.list;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import com.google.common.io.Files;
import fj.F;
import fj.F2;
import fj.P2;
import fj.Show;
import fj.Unit;
import fj.data.List;
import fj.function.Integers;
public class TenOneLiners {
public static void main(String[] args) {
// #1
final List<Integer> s1 = List.range(1, 11).map(Integers.multiply.f(2));
listShow(intShow).println(s1);
// #2
final int s2 = List.range(1, 1001).foldLeft(Integers.add, 0);
intShow.println(s2);
// #3
final List<String> words = list("scala", "akka", "play framework", "sbt", "typesafe");
final String tweet = "This is an example tweet talking about scala and sbt.";
final boolean exists = words.exists(new F<String, Boolean>() {
public Boolean f(String s) {
return tweet.contains(s);
}
});
booleanShow.println(exists);
// #4 (Used Guava here)
try {
final File file = new File("data.txt");
final String fileText = Files.toString(file, Charset.defaultCharset());
final java.util.List<String> fileLines = Files.readLines(file, Charset.defaultCharset());
Show.stringShow.println(fileText);
System.out.println(fileLines);
} catch (IOException e) {
e.printStackTrace();
}
// #5
List.range(1, 5).map(new F<Integer, String>() {
public String f(Integer i) {
return "Happy Birthday " + (i == 3 ? "dear Pradnya" : "to You");
}
}).foreach(new F<String, Unit>() {
public Unit f(String s) {
return stringShow.println(s);
}
});
// #6
final P2<List<Integer>, List<Integer>> partitions =
partition(list(49, 58, 76, 82, 88, 90), new F<Integer, Boolean>() {
public Boolean f(Integer i) {
return i < 60;
}
});
final List<Integer> passed = partitions._1();
final List<Integer> failed = partitions._2();
listShow(intShow).println(passed);
listShow(intShow).println(failed);
// #8
final int max = list(14, 35, -7, 46, 98).maximum(intOrd);
final int min = list(14, 35, -7, 46, 98).minimum(intOrd);
intShow.println(max);
intShow.println(min);
// #7, #9, #10 - Left as an exercise to the reader.
}
public static <A> P2<List<A>, List<A>> partition(List<A> list, final F<A, Boolean> predicate) {
return list.foldLeft(
new F2<P2<List<A>, List<A>>, A, P2<List<A>, List<A>>>() {
public P2<List<A>, List<A>> f(P2<List<A>, List<A>> acc, A cur) {
final List<A> left = acc._1();
final List<A> right = acc._2();
if (predicate.f(cur))
return p(left.cons(cur), right);
else
return p(left, right.cons(cur));
}
},
p(List.<A> nil(), List.<A> nil())
);
}
}
@missingfaktor

Copy link
Copy Markdown
Author

Thanks for the comment, @mnicky! Sure the vanilla Java would be shorter in simple cases such as these (well, at least on the character count metric), but as systems grow larger, compositionality and reusability become more and more important, and this is where Functional Java shines. Agree, the syntax looks atrocious, but I would not blame Functional Java for that. The authors have done the best they could do in Java. :-)

@mnicky

mnicky commented Sep 15, 2011

Copy link
Copy Markdown

A few months ago I came across both Functional Java and LambdaJ and was thinking of trying one of them, but later on I decided to switch to Clojure, (where I don't need them ;-), so I've never used them really, but this is not to say I don't like the idea of bringing functional programming to Java... And I'm really looking forward to JDK 8.

@missingfaktor

Copy link
Copy Markdown
Author

@mnicky: Oh nice, you are a Clojurian. :-) I was preaching to the choir then. I too am looking forward to Java 8. It will greatly aid the adoption of ideas from FP.

@mnicky

mnicky commented Sep 16, 2011

Copy link
Copy Markdown

Don't know if I would call myself a Clojurian already... rather a clo-newb ;-)

@missingfaktor

Copy link
Copy Markdown
Author

I am learning myself some Haskell. While I am at it, I thought I should give a go to these problems.

import Data.List
import Control.Arrow

a1 :: [Int]
a1 = map (2 *) [1 .. 10]

a2 :: Int
a2 = sum [1 .. 1000] -- Or: foldr 0 (+) [1 .. 1000]

a3 :: Bool
a3 = any (`isInfixOf` tweet) keywords
     where keywords = ["scala", "akka", "play framework", "sbt", "typesafe"]
           tweet = "This is an example tweet talking about scala and sbt."

a4 :: IO ()
a4 = readFile "data.txt" >>= putStrLn

a4' :: IO ()
a4' = readFile "data.txt" >>= (mapM_ putStrLn . lines)

a5 :: IO ()
a5 = mapM_ (putStrLn . s) [1 .. 4]
     where s i = "Happy birthday" ++ if i == 3 then "dear Pradnya" else "to you"

a6 :: ([Int], [Int])
a6 = partition (< 60) [49, 58, 76, 82, 88, 90]     

a7 :: (Int, Int)
a7 = (maximum &&& minimum) [14, 35, -7, 46, 98]

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