loda 1.0.2 copy "loda: ^1.0.2" to clipboard
loda: ^1.0.2 copied to clipboard

The loda module is a combination of the two Lodash and Ramda modules. It promotes a more functional programming (FP) friendly style by exporting an instance of loda with its methods wrapped to produce [...]

Features #

Functional programming in Dart and Flutter.

Getting started #

The loda module is a combination of the two Lodash and Ramda modules. It promotes a more functional programming (FP) friendly style by exporting an instance of loda with its methods wrapped to produce immutable auto-curried iteratee-first data-last methods.

Usage #

install

# pubspec.yaml
dependencies:
  loda: ^1.0.2

Ex 1:

import 'package:loda/loda.dart';

const _ = LodaConstants.placeholder;

final profiles = [
    {
      "name": "John",
      "age": 19,
      "address": {
        "country": "DN",
      },
    },
    {
      "name": "Michel",
      "age": 18,
      "address": {
        "country": "QN",
      },
    },
    {
      "name": "Cel",
      "age": 17,
      "address": {
        "country": 'ĐL',
      },
    },
  ];
  final bool Function(dynamic) isEligible = and([
    has('name'),
    has('age'),
    flow([
      select('age'),
      gte(_, 18),
    ]),
  ]);
  final getCountriesOfEligibleProfiles = flow([
    filter(isEligible),
    map(select('address.country')),
    distinct,
  ]);
  final countries = getCountriesOfEligibleProfiles(profiles);
  print(countries); // [DN, QN]

Ex 2:

import 'package:loda/loda.dart';
import 'package:collection/collection.dart';

   final profiles = [
      {
        "id": "1",
        "address": {
          "city": "DN",
        },
        "name": "Duy Nguyen",
      },
      {
        "id": "2",
        "name": "Minh D",
        "address": {
          "city": "DL",
        }
      },
      {
        "id": "3",
        "name": 'Monad',
        "address": {
          "city": null,
        }
      }
    ];
    Maybe<Map> findMaybe(List<Map>? entries, id) {
      final result = entries?.firstWhereOrNull(
        (obj) => obj["id"] == id,
      );
      return (result?.isNotEmpty ?? false)
          ? Maybe.some(result)
          : Maybe.nothing();
    }

    Function get = curry(
      (key, obj) =>
          obj.containsKey(key) ? Maybe.some(obj[key]) : Maybe.nothing(),
    );

    toLowerCase(String str) => str.toLowerCase();

    findProfileWithId(id) => findMaybe(profiles, id)
        .flatMap(get("address"))
        .flatMap(get("city"))
        .map(toLowerCase)
        .unwrap();

  print(findProfileWithId("1")); // dn

  print(findProfileWithId("3")); // null

Additional information #

My article introduces to Functional Programming LINK

3
likes
110
pub points
0%
popularity

Publisher

unverified uploader

The loda module is a combination of the two Lodash and Ramda modules. It promotes a more functional programming (FP) friendly style by exporting an instance of loda with its methods wrapped to produce immutable auto-curried iteratee-first data-last methods.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

dartx, flutter

More

Packages that depend on loda