Created
September 1, 2011 17:49
-
-
Save missingfaktor/1186763 to your computer and use it in GitHub Desktop.
10(?) Functional Java oneliners(?) to impress(?) your friends
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()) | |
); | |
} | |
} |
Don't know if I would call myself a Clojurian already... rather a clo-newb ;-)
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
@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.