Your SlideShare is downloading. ×
0
Poor man’s FPHow to change your javacode in a way you neverthought about
Dmitry Lebedev• WILDCARD conf• AIESEC/Lotus conf
Functional Programming• Higher order functions• Pure functions• Recursion• Non-strict evaluation• Currying
Higher order functionsfoo = Proc.new { |prompt| prompt.echo = false }new_pass = ask("Enter your new password: ", &foo)
Pure Functionsfoo = Proc.new { |x| x*x+(x+20) }
Recursiondef fib(n)return n if (0..1).include? nfib(n-1) + fib(n-2) if n > 1end
Non-strict evaluationprint length([2+1, 3*2, 1/0, 5-4])
Curryingplus = lambda {|a,b| a + b}plus.(3,5) #=> 8plus_one = plus.curry.(1)plus_one.(5) #=> 6plus_one.(11) #=> 12
Java 8?import java.awt.Button;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class Java8 {p...
Java 8?import java.awt.Button;import java.awt.event.ActionEvent;public class Java8 {public static void main(String[] args)...
Java 8?import java.awt.Button;public class Java8 {public static void main(String[] args) {Button myButton = new Button();m...
Still living under Java 6?Languages• Scala• ClojureLibraries• Guava• LambdaJ• Functional Java
Guava• Function<A, B>, which has the single method Bapply(A input). Instances of Function aregenerally expected to be refe...
GuavaList converted = ImmutableList.copyOf(Iterables.transform( userDTOs, new Function(){public User apply( UserDTO input ...
LambdaJPerson me = new Person("Mario", "Fusco", 35);Person luca = new Person("Luca", "Marrocco", 29);Person biagio = new P...
FunctionalJavaimport fj.data.Array;import static fj.data.Array.array;import static fj.Show.arrayShow;import static fj.Show...
Invention of Own Bicycle
First Attemptpublic static <T> void forEach(Collection<T> list,Procedure<T> proc) {for (T object : list) {proc.invoke(obje...
Another trypublic static <T, E> Collection<E> each(Collection<T>list, Function<T, E> function) {ArrayList<E> result = new ...
Use Caseresult.addAll(each(imageList,new Function<S3ObjectSummary, String>({@Overridepublic String apply(@NullableS3Object...
Let’s complicate!public interface MapFunction<T, K, V> {Pair<K,V> proceed(T key);}
Let’s complicate!public static <T, K, V> Map<K, V>map(Collection<T> list,MapFunction<T, K, V> function) {Map<K, V> result ...
Let’s complicate!...List<Future> fs = newArrayList<Future>(list.size());for (final T object : list) {fs.add(exec.submit(ne...
Let’s complicate!...for (Future ft : fs) {Pair<K, V> pair = (Pair<K, V>) ft.get();result.put(pair.getKey(), pair.getValue(...
Question?
The End
Upcoming SlideShare
Loading in...5
×

Poor Man's Functional Programming

2,799

Published on

Published in: Technology, News & Politics
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,799
On Slideshare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
2
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • Functions are higher-order when they can take other functions as arguments, and return them as results.
  • Pure functions support the mathematical definition of a function:y = f(x)Which means:1- A function should return the same exact value given the same exact input.2- A function does one thing exactly(has one clear mission), it has no side effects like changing some other value or write to stream or any other mission rather than its assigned one.In fact Ruby doesn’t force you to write pure functions, but certainly it helps you to do so. Actually exercising yourself to write in pure functional style when possible helps you really in many ways:1- It makes your code clean, readable and self-documenting.2- It makes your code more “thread safe”3- It helps you more when it comes to TDD.
  • Iteration (looping) in functional languages is usually accomplished via recursion.
  • Higher-order functions enable Currying, which the ability to take a function that accepts n parameters and turns it into a composition of n functions each of them take 1 parameter. A direct use of currying is the Partial Functions where if you have a function that accepts n parameters then you can generate from it one of more functions with some parameter values already filled in.
  • Transcript of "Poor Man's Functional Programming"

    1. 1. Poor man’s FPHow to change your javacode in a way you neverthought about
    2. 2. Dmitry Lebedev• WILDCARD conf• AIESEC/Lotus conf
    3. 3. Functional Programming• Higher order functions• Pure functions• Recursion• Non-strict evaluation• Currying
    4. 4. Higher order functionsfoo = Proc.new { |prompt| prompt.echo = false }new_pass = ask("Enter your new password: ", &foo)
    5. 5. Pure Functionsfoo = Proc.new { |x| x*x+(x+20) }
    6. 6. Recursiondef fib(n)return n if (0..1).include? nfib(n-1) + fib(n-2) if n > 1end
    7. 7. Non-strict evaluationprint length([2+1, 3*2, 1/0, 5-4])
    8. 8. Curryingplus = lambda {|a,b| a + b}plus.(3,5) #=> 8plus_one = plus.curry.(1)plus_one.(5) #=> 6plus_one.(11) #=> 12
    9. 9. Java 8?import java.awt.Button;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class Java8 {public static void main(String[] args) {Button myButton = new Button();myButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent ae) {System.out.println(ae.getSource());}});}}
    10. 10. Java 8?import java.awt.Button;import java.awt.event.ActionEvent;public class Java8 {public static void main(String[] args) {Button myButton = new Button();myButton.addActionListener((ActionEvent ae) -> {System.out.println(ae.getSource());});}}
    11. 11. Java 8?import java.awt.Button;public class Java8 {public static void main(String[] args) {Button myButton = new Button();myButton.addActionListener(ae ->{System.out.println(ae.getSource());});}}
    12. 12. Still living under Java 6?Languages• Scala• ClojureLibraries• Guava• LambdaJ• Functional Java
    13. 13. Guava• Function<A, B>, which has the single method Bapply(A input). Instances of Function aregenerally expected to be referentially transparent -- noside effects -- and to be consistent with equals, thatis, a.equals(b) implies thatfunction.apply(a).equals(function.apply(b)).• Predicate<T>, which has the single methodboolean apply(T input). Instances ofPredicate are generally expected to be side-effect-freeand consistent with equals.
    14. 14. GuavaList converted = ImmutableList.copyOf(Iterables.transform( userDTOs, new Function(){public User apply( UserDTO input ){return new User( input.name, input.id );}}));
    15. 15. LambdaJPerson me = new Person("Mario", "Fusco", 35);Person luca = new Person("Luca", "Marrocco", 29);Person biagio = new Person("Biagio", "Beatrice", 39);Person celestino = new Person("Celestino", "Bellone", 29);List<Person> meAndMyFriends =asList(me, luca, biagio, celestino);List<Person> oldFriends =filter(having(on(Person.class).getAge(), greaterThan(30)),meAndMyFriends);
    16. 16. FunctionalJavaimport fj.data.Array;import static fj.data.Array.array;import static fj.Show.arrayShow;import static fj.Show.intShow;import static fj.function.Integers.even;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(even);arrayShow(intShow).println(b);// {44,22,90,98,1078,6,64,6,42}}}
    17. 17. Invention of Own Bicycle
    18. 18. First Attemptpublic static <T> void forEach(Collection<T> list,Procedure<T> proc) {for (T object : list) {proc.invoke(object);}}
    19. 19. Another trypublic static <T, E> Collection<E> each(Collection<T>list, Function<T, E> function) {ArrayList<E> result = new ArrayList<E>();for (T object : list) {result.add(function.apply(object));}return result;}
    20. 20. Use Caseresult.addAll(each(imageList,new Function<S3ObjectSummary, String>({@Overridepublic String apply(@NullableS3ObjectSummary input) {return siteURL + input.getKey();}}));
    21. 21. Let’s complicate!public interface MapFunction<T, K, V> {Pair<K,V> proceed(T key);}
    22. 22. Let’s complicate!public static <T, K, V> Map<K, V>map(Collection<T> list,MapFunction<T, K, V> function) {Map<K, V> result = new HashMap<K, V>();for (T object : list) {Pair<K, V> pair =function.proceed(object);result.put(pair.getKey(),pair.getValue());}return result;}
    23. 23. Let’s complicate!...List<Future> fs = newArrayList<Future>(list.size());for (final T object : list) {fs.add(exec.submit(new Callable() {@Overridepublic Object call() throws Exception {return function.proceed(object);}}));}...
    24. 24. Let’s complicate!...for (Future ft : fs) {Pair<K, V> pair = (Pair<K, V>) ft.get();result.put(pair.getKey(), pair.getValue());}...
    25. 25. Question?
    26. 26. The End
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×