Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am eltray on github.
  • I am eltray (https://keybase.io/eltray) on keybase.
  • I have a public key ASBLIysibuUrVUXrlRBu4Kw8wSokRW3GgKyZOXLiG4xVGgo

To claim this, I am signing this object:

@eltray
eltray / main.dart
Created August 2, 2019 09:07
Learning Dart - 6. Use Dart for functional programming
String scream(int length) => "A${"a" * length}h!";
main() {
var values = [1, 2, 3, 4, 10];
// Imperative way
// for (int length in values){
// print(scream(length));
// }
// Functional way
// values.map(scream).forEach(print);
@eltray
eltray / main.dart
Created August 1, 2019 15:33
Learning Dart - 5 Interfaces
import 'dart:math' as math;
abstract class Shape {
num get area;
factory Shape(String type) {
if (type == 'circle') return Circle(2);
if (type == 'square') return Square(2);
throw 'Can\'t create $type';
}
}
@eltray
eltray / main.dart
Created August 1, 2019 15:25
Learning Dart - 4 Create factory via factory constructor
import 'dart:math' as math;
abstract class Shape {
num get area;
factory Shape(String type) {
if (type == 'circle') return Circle(2);
if (type == 'square') return Square(2);
throw 'Can\'t create $type';
}
}
@eltray
eltray / main.dart
Created August 1, 2019 15:25
Learning Dart - 4 Create a factory via top-level function
import 'dart:math' as math;
abstract class Shape {
num get area;
}
class Circle implements Shape {
final num radius;
num get area => math.pi * math.pow(radius, 2);
@eltray
eltray / main.dart
Created August 1, 2019 15:24
Learning Dart - 3 Use optional parameters (instead of overloading)
import 'dart:math';
class Rectangle {
int width;
int height;
Point origin;
Rectangle({this.origin = const Point(0, 0), this.width = 0, this.height = 0});
@override
@eltray
eltray / main.dart
Created August 1, 2019 15:22
Learning Dart - 2 Create a simple Dart class
class Bicycle {
int _speed = 0;
int get speed => _speed;
int cadence;
int wheel;
Bicycle({this.cadence = 0, this.wheel = 0});
@override
String toString() =>
@eltray
eltray / main.dart
Created August 1, 2019 15:21
Learning Dart - 1 Introduction
class Bicycle {
int _speed = 0;
int get speed => _speed;
int cadence;
int wheel;
Bicycle({this.cadence = 0, this.wheel = 0});
@override
String toString() =>