Skip to content

Instantly share code, notes, and snippets.

@kirkdrichardson
Created October 12, 2019 02:19
Show Gist options
  • Save kirkdrichardson/8608f4b406d6fbd4504f9a328aa7b044 to your computer and use it in GitHub Desktop.
Save kirkdrichardson/8608f4b406d6fbd4504f9a328aa7b044 to your computer and use it in GitHub Desktop.
Basic Functional Programming in Dart
// Basic Functional Programming in Dart (as of v2.5)
// See live at https://dartpad.dartlang.org/8608f4b406d6fbd4504f9a328aa7b044
void main() {
// Functions are objects
print('-----------------ANONYMOUS (LAMBDA) FUNCTIONS--------------------');
// Here we assign an anonymous fx to a variable.
Function squareANumber = (int num) {
return num*num;
};
// This is an equivalent anonymous, or "lambda" fx, using the fat arrow syntax.
Function square = (int num) => num*num;
// Call as you would a regular class method.
print(squareANumber(4));
print(square(4));
print('-----------------HIGHER ORDER FUNCTIONS--------------------');
// i.e., a function that
// A) accepts a function, OR
// B) returns a function, OR
// C) both accepts and returns a function
// A) accepts a function
String modifyString(String str, Function modifier) {
return modifier(str);
}
String modifiedString = modifyString('Hello', (String str) => str + ' World');
print(modifiedString);
// B) returns a function
Function getStringModifierForType(String str) {
if (str.length < 10) {
return () => print('$str is too short');
}
return () => print('$str is too long');
}
// Build appropriate function based on string length
Function conditionalStringModifier = getStringModifierForType('Hello');
// Call returned function.
conditionalStringModifier();
// Reassign using different argument
conditionalStringModifier = getStringModifierForType('Hello World');
// Call returned function
conditionalStringModifier();
print('-----------------CLOSURES--------------------');
// Closure is a special fx in which you can mutate / modify variables present in parent scope variables
// i.e., it is about scope, e.g.
// A) a function that has access to parent scope
String variableInParentScope = 'You';
Function myLambdaFunctionClosure = () {
variableInParentScope += ' Rock!!!';
};
// we are able not modify variables in the parent scope, so be careful!
myLambdaFunctionClosure();
print(variableInParentScope);
// B) a function that has access to variables in its lexical scope, even when invoked outside of its original scope
Function myFunctionReturningClosure = () {
String lexicalScopeVariable = 'No more';
Function innerFunction = () {
return lexicalScopeVariable + ' Bad Days :)';
};
return innerFunction;
};
// Now we can get that inner function, that forms a closure around that lexically scoped variable:
Function returnedFunction = myFunctionReturningClosure();
// Then we can call the returned fx to extract the value
String valueReturned = returnedFunction();
print(valueReturned);
// Or, we can roll all of that up like this:
print(myFunctionReturningClosure()());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment