main function

void main()

Implementation

void main() {
  bool isNotNull(int? value) => value != null;
  bool isNotEmpty(String value) => value.isNotEmpty;

  final profiles = [
    User("John", null, Address("DN")),
    User("Doe", 20, Address("HN")),
    User("Jane", 17, Address("SG")),
    User("Michel", 18, Address("HN")),
    User("Jack", 20, Address("ĐL")),
  ];

  final isEligible = FP.and([
    FP.pipe<User>().then((profile) => profile.name).then(isNotEmpty).call,
    FP.pipe<User>().then((profile) => profile.age).then(isNotNull).call,
    FP.pipe<User>().then((profile) => profile.age ?? 0).then(FP.p >= 18).call,
  ]);

  final getCountries = FP
      .pipe<List<User>>()
      .then(FP.filter(isEligible))
      .then(FP.map((profile) => profile.address.country))
      .then(FP.distinct());

  getCountries(profiles).forEach(print);

  final isEligibleFP = FP
      .pipe<User>()
      .then((profile) => profile.age)
      .then((age) => age ?? 0)
      .then(FP.p >= 18)
      .call;

  FP.map(isEligibleFP)(profiles).forEach(print);
  final pipeline = FP
      .pipe<int>()
      .then((n) => n + 1)
      .thenEither<String, int>(
        (int n) {
          if (n % 2 == 0) {
            return Right(n);
          } else {
            return const Left("Số không phải là số chẵn");
          }
        },
      )
      .mapEitherRight((int n) => n * 2)
      .mapEitherRight((int n) => n.toString())
      .mapEitherLeft((String message) => 1)
      .mapEitherBoth(
        (int l) => 1,
        (String r) => 2,
      )
      .then((Either<int, int> either) {
        return either.fold(
          (l) => "Lỗi: $l",
          (r) => r.toString(),
        );
      });

  print(pipeline(3));
  print(pipeline(4));
}