functional programming - Functional Interfaces

Java 8 introduces java.util.function containing a set of interfaces which are target types for lambda expressions and method references. Each interface contains only one abstract method, called the functional method .

// functional/FunctionalAnnotation.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

@FunctionalInterface
interface Functional {
  String goodbye(String arg);
}

interface FunctionalNoAnn {
  String goodbye(String arg);
}

/*
@FunctionalInterface  // [1]
interface NotFunctional {
  String goodbye(String arg);
  String hello(String arg);
}
Produces error message:
NotFunctional is not a functional interface
multiple non-overriding abstract methods
found in interface NotFunctional
*/

public class FunctionalAnnotation {
  public String goodbye(String arg) {
    return "Goodbye, " + arg;
  }

  public static void main(String[] args) {
    FunctionalAnnotation fa = new FunctionalAnnotation();
    Functional f = fa::goodbye;
    FunctionalNoAnn fna = fa::goodbye;
    // Functional fac = fa; // Incompatible
    Functional fl = a -> "Goodbye, " + a;
    FunctionalNoAnn fnal = a -> "Goodbye, " + a;
  }
}

The @FunctionalInterface annotation is optional; Java treats both Functional and FunctionalNoAnn as functional interfaces in main().

[1] ' error:

functional/FunctionalAnnotation.java:15: error: Unexpected @FunctionalInterface annotation
@FunctionalInterface
^
  NotFunctional is not a functional interface
    multiple non-overriding abstract methods found in interface NotFunctional
1 error

The solution is keeping one of method in interface NotFunctional.

Here’s an example that enumerates all the different Function variants, applied to lambda expressions:

// functional/FunctionVariants.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

import java.util.function.*;

class Foo {}

class Bar {
  Foo f;

  Bar(Foo f) {
    this.f = f;
  }
}

class IBaz {
  int i;

  IBaz(int i) {
    this.i = i;
  }
}

class LBaz {
  long l;

  LBaz(long l) {
    this.l = l;
  }
}

class DBaz {
  double d;

  DBaz(double d) {
    this.d = d;
  }
}

public class FunctionVariants {
  static Function<Foo, Bar> f1 = f -> new Bar(f);
  static IntFunction<IBaz> f2 = i -> new IBaz(i);
  static LongFunction<LBaz> f3 = l -> new LBaz(l);
  static DoubleFunction<DBaz> f4 = d -> new DBaz(d);
  static ToIntFunction<IBaz> f5 = ib -> ib.i;
  static ToLongFunction<LBaz> f6 = lb -> lb.l;
  static ToDoubleFunction<DBaz> f7 = db -> db.d;
  static IntToLongFunction f8 = i -> i;
  static IntToDoubleFunction f9 = i -> i;
  static LongToIntFunction f10 = l -> (int) l;
  static LongToDoubleFunction f11 = l -> l;
  static DoubleToIntFunction f12 = d -> (int) d;
  static DoubleToLongFunction f13 = d -> (long) d;

  public static void main(String[] args) {
    System.out.println(f1 + ", " + f2 + ", " + f3 + ", " + f4 + ", " + f5);
    Bar b = f1.apply(new Foo());
    IBaz ib = f2.apply(11); // interface Function method
    LBaz lb = f3.apply(11);
    DBaz db = f4.apply(11);
    int i = f5.applyAsInt(ib); // interface ToIntFunction method
    long l = f6.applyAsLong(lb); // interface ToLongFunction method
    double d = f7.applyAsDouble(db);// interface ToDoubleFunction method
    System.out.println(i + ", " + l + ", " + d);
    l = f8.applyAsLong(12);
    d = f9.applyAsDouble(12);
    i = f10.applyAsInt(12);
    System.out.println(i + ", " + l + ", " + d);
    d = f11.applyAsDouble(12);
    i = f12.applyAsInt(13.0);
    l = f13.applyAsLong(13.0);
    System.out.println(i + ", " + l + ", " + d);
  }
}

My Output:

FunctionVariants$$Lambda$1/135721597@6e8cf4c6, FunctionVariants$$Lambda$2/455659002@12edcd21, FunctionVariants$$Lambda$3/2003749087@34c45dca, FunctionVariants$$Lambda$4/18319
32724@52cc8049, FunctionVariants$$Lambda$5/1149319664@5b6f7412
11, 11, 11.0
12, 12, 12.0
13, 13, 12.0

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/functional/FunctionalAnnotation.java

3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/functional/FunctionVariants.java

4. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/function/Function.java

5. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/function/ToIntFunction.java

6. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/function/ToLongFunction.java

7. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/function/ToDoubleFunction.java

Learning C++ Functional Programming by Wisnu Anggoro English | 10 Aug. 2017 | ISBN: 1787281973 | ASIN: B06WVD7CVT | 304 Pages | AZW3 | 2.4 MB Key Features Modularize your applications and make them highly reusable and testable Get familiar with complex concepts such as metaprogramming, concurrency, and immutability A highly practical guide to building functional code in C++ filled with lots of examples and real-world use cases Book Description Functional programming allows developers to divide programs into smaller, reusable components that ease the creation, testing, and maintenance of software as a whole. Combined with the power of C++, you can develop robust and scalable applications that fulfill modern day software requirements. This book will help you discover all the C++ 17 features that can be applied to build software in a functional way. The book is divided into three modules—the first introduces the fundamentals of functional programming and how it is supported by modern C++. The second module explains how to efficiently implement C++ features such as pure functions and immutable states to build robust applications. The last module describes how to achieve concurrency and apply design patterns to enhance your application's performance. Here, you will also learn to optimize code using metaprogramming in a functional way. By the end of the book, you will be familiar with the functional approach of programming and will be able to use these techniques on a daily basis. What you will learn Get to know the difference between imperative and functional approaches See the use of first-class functions and pure functions in a functional style Discover various techniques to apply immutable state to avoid side effects Design a recursive algorithm effectively Create faster programs using lazy evaluation Structure code using design patterns to make the design process easier Use concurrency techniques to develop responsive software Learn how to use the C++ Standard Template Library and metaprogramming in a functional way to improve code optimization About the Author Wisnu Anggoro is a Microsoft Certified Professional in C# programming and an experienced C/C++ developer. He has also authored the books Boost.Asio C++ Network Programming - Second Edition and Functional C# by Packt. He has been programming since he was in junior high school, which was about 20 years ago, and started developing computer applications using the BASIC programming language in the MS-DOS environment. He has solid experience in smart card programming, as well as desktop and web application programming, including designing, developing, and supporting the use of applications for SIM Card Operating System Porting, personalization, PC/SC communication, and other smart card applications that require the use of C# and C/C++. He is currently a senior smart card software engineer at CIPTA, an Indonesian company that specializes in innovation and technology for smart cards. He can be reached through his email at wisnu@anggoro.net. Table of Contents Diving into Modern C++ Manipulating functions in functional programming Applying immutable state to the function Recurring method invocation using recursive algorithm Procrastinating the execution process using Lazy Evaluation Optimizing code with Metaprogramming Running parallel execution using Concurrency Creating and debugging application in functional approach
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值