All Projects → spkersten → Dart_functional_data

spkersten / Dart_functional_data

Licence: mit
Simple and non-intrusive code generator for lenses and boilerplate of data types

Programming Languages

dart
5743 projects

Projects that are alternatives of or similar to Dart functional data

Typed
The TypeScript Standard Library
Stars: ✭ 124 (+217.95%)
Mutual labels:  lenses, functional
futils
Utilities for generic functional programming
Stars: ✭ 21 (-46.15%)
Mutual labels:  functional, lenses
Fpp
Functional PHP Preprocessor - Generate Immutable Data Types
Stars: ✭ 282 (+623.08%)
Mutual labels:  functional, code-generator
Preact Boilerplate
🎸 Ready-to-rock Preact starter project, powered by Webpack.
Stars: ✭ 959 (+2358.97%)
Mutual labels:  boilerplate
Sao
⚔ Futuristic scaffolding tool
Stars: ✭ 966 (+2376.92%)
Mutual labels:  boilerplate
Disperse
React/Redux dApp (decentralized app) boilerplate using Ethereum's blockchain
Stars: ✭ 36 (-7.69%)
Mutual labels:  boilerplate
Veluxi Starter
Veluxi Vue.js Starter Project with Nuxt JS and Vuetify
Stars: ✭ 39 (+0%)
Mutual labels:  boilerplate
Amino
functional data structures and utilities for python
Stars: ✭ 31 (-20.51%)
Mutual labels:  functional
Relay Fullstack
☝️🏃 Modern Relay Starter Kit - Integrated with Relay, GraphQL, Express, ES6/ES7, JSX, Webpack, Babel, Material Design Lite, and PostCSS
Stars: ✭ 986 (+2428.21%)
Mutual labels:  boilerplate
Typescript Boilerplate
Start writing stuff in TypeScript without bothered by configurations
Stars: ✭ 35 (-10.26%)
Mutual labels:  boilerplate
Html Sass Babel Webpack Boilerplate
Webpack 4 + Babel + ES6 + SASS + HTML Modules + Livereload
Stars: ✭ 35 (-10.26%)
Mutual labels:  boilerplate
Swift Template
A template based module generator for Swift projects.
Stars: ✭ 34 (-12.82%)
Mutual labels:  boilerplate
Es6 Express Mongoose Passport Rest Api
Lightweight boilerplate for Node RESTful API, ES6, Express, Mongoose and Passport 🎁
Stars: ✭ 36 (-7.69%)
Mutual labels:  boilerplate
Express React Boilerplate
🚀🚀🚀 This is a tool that helps programmers create Express & React projects easily base on react-cool-starter.
Stars: ✭ 32 (-17.95%)
Mutual labels:  boilerplate
Web Extension Starter
🖥🔋Web Extension starter to build "Write Once Run on Any Browser" extension
Stars: ✭ 987 (+2430.77%)
Mutual labels:  boilerplate
Enso
Laravel Vue SPA, Bulma themed. For demo login use `[email protected]el-enso.com` & `password` -
Stars: ✭ 959 (+2358.97%)
Mutual labels:  boilerplate
Boilerplate Dynet Rnn Lm
Boilerplate code for quickly getting set up to run language modeling experiments
Stars: ✭ 37 (-5.13%)
Mutual labels:  boilerplate
Go Starter Kit
Go Rest API starter kit / Golang API boilerplate base on Chi framework
Stars: ✭ 35 (-10.26%)
Mutual labels:  boilerplate
React Atomic Design
🔬 Boilerplate with the methodology Atomic Design using a few cool things.
Stars: ✭ 972 (+2392.31%)
Mutual labels:  boilerplate
Js Boilerplate
Boilerplate for any javascript frontend project
Stars: ✭ 36 (-7.69%)
Mutual labels:  boilerplate

functional_data

Simple and non-intrusive code generator for boilerplate of data types. The package generates a simple mixin with operator==, hashCode, copyWith, toString, as well as lenses.

Boiler plate

Because the boiler plate is generated as a mixin, it is minimally intrusive on the interface of the class. You only have to provide a constructor with named arguments for all fields and extend the generated mixin.

@FunctionalData()
class Person extends $Person {
  final String name;
  final int age;
  
  const Person({this.name, this.age});
  
  const Person.anonymous() : this(name: "John Doe", age: null);
  
  int get ageInDays => numberOfDaysInMostYears * age;

  static const numberOfDaysInMostYears = 356;
}

Because of this design, you have complete control over the class. You can, for example, add named constructors or methods to the class like you're used to.

Lenses

For every class, lenses are generated for all fields which allow viewing a field or creating a new instance of the classes with that field modified in some way. For example, the lens of Person's name is Person$.name. To focus a lens on a specific instance use its of method:

final teacher = Person(name: "Arthur", age: 53);

print(Person$.name.of(teacher).value);
// -> "Arthur"

print(Person$.age.of(teacher).update(60);
// -> Person(name: "Arthur", age: 60)

print(Person$.name.of(teacher).map((name) => name.toUpperCase()));
// -> Person(name: "ARTHUR", age: 53)

This isn't very exciting yet. The power of lenses comes to light when you combine them. It allows you to easily create a copy of a large nested data structure with one of the fields in a leaf modified. Two lenses can be chained using then.

class Course extends $Course {
  final String name;
  final List<Person> students;
  
  const Course({this.students});
}

final programming = Course(name: "Programming 101", students: [Person(name: "Jane", age: 21), Person(name: "Tom", age: 20)]);

final firstStudentsName = Course$.students.then(List$.first<Person>()).then(Person$.name);

print(firstStudentsName.of(programming).update("Marcy"));
// -> Course(students: [Person(name: "Marcy", age: 21), Person(name: "Tom", age: 20)]

Compare this with the alternative:

final firstStudent = programming.students.first;
final updatedFirstStudent = Person(name: "Marcy", age: firstStudent.age);
final updatedStudents = [updatedFirstStudent] + programming.students.skip(1);
final updatedCourse = Course(name: programming.name, students: updatedStudents);

This is much less readable and error prone. Imagine what happens when one of the classes gains a field.

Full example:

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

import 'package:functional_data/functional_data.dart';

part 'lens.g.dart';

// Only requirement is that it has a constructor with named arguments for all fields
@FunctionalData()
class Foo extends $Foo {
  final int number;
  final String name;

  const Foo({this.number, this.name});
}

@FunctionalData()
class Bar extends $Bar {
  final Foo foo;

  @CustomEquality(DeepCollectionEquality())
  final List<Foo> foos;

  final String driver;

  const Bar({this.foo, this.foos, this.driver});
}

void main() {
  final foo = Foo(number: 42, name: "Marvin");
  final bar = Bar(foo: foo, foos: [Foo(number: 101, name: "One"), Foo(number: 102, name: "Two")], driver: "One");

  final fooName = Bar$.foo.then(Foo$.name);
  // print(fooName.map((name) => name.toUpperCase(), bar));
  print(fooName.of(bar).map((name) => name.toUpperCase()));
  // Bar(foo: Foo(number: 42, name: MARVIN), foos: [Foo(number: 101, name: One), Foo(number: 102, name: Two)], driver: One)

  final firstFooName = Bar$.foos.then(List$.atIndex<Foo>(0)).then(Foo$.name);
  // print(firstFooName.update(bar, "Twee"));
  print(firstFooName.of(bar).update("Twee"));
  // Bar(foo: Foo(number: 42, name: Marvin), foos: [Foo(number: 101, name: Twee), Foo(number: 102, name: Two)], driver: One)

  final nameOfFooNamedTwo = Bar$.foos.then(List$.where<Foo>((foo) => foo.name == "Two")).then(Foo$.name);
  print(nameOfFooNamedTwo.update(bar, "Due"));
  // Bar(foo: Foo(number: 42, name: Marvin), foos: [Foo(number: 101, name: One), Foo(number: 102, name: Due)], driver: One)

  final driversNumber =
      Bar$.foos.thenWithContext((bar) => List$.where<Foo>((foo) => foo.name == bar.driver).then(Foo$.number));
  print(driversNumber.of(bar).value);
  // 101
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].