Functional programming essentials for Dart. Tail call optimization, partial application, memoization, and more.

Related tags

Utilities pure
Overview

Pure

Pure is a Dart package that brings functional programming essentials through extension methods. It offers function composition/pipes, function memoization, partial application, and recursion trampolines.

Extensions

Down are listed extensions included in the pure package. Every extension is valid from 0/1 arguments up to 9 arguments.

Composition

Provides function composition functionality.

Substituting function application with composition or pipes helps to make code more readable and reduces nesting.

double intToDouble(int x) => x.toDouble();
String doubleToString(double x) => x.toString();

/// Traditional application
String intToStringApplication(int x) => doubleToString(intToDouble(x));

/// Composition
final intToStringComposition = doubleToString.dot(intToDouble);

/// Pipes
String intToStringPipe1(int x) => x.pipe(intToDouble).pipe(doubleToString); 
// or 
String intToStringPipe2(int x) => intToDouble(x).pipe(doubleToString); 

Recursion Trampolines

Provides stack-safe recursion functionality.

Trampolines essentially do the same work as compiler's tail call optimization โ€“ flatten the stack of calls, making function never exceeding stack and never resulting in Stack Overflow.

/// Regular
int regularSum(int number, int sum) =>
    number == 0 ? sum : regularSum(number - 1, sum + number);

/// Trampolined
Tram<int> tramSum(int number, int sum) => number == 0
    ? Tram.done(sum)
    : Tram.call(() => tramSum(number - 1, sum + number));

final n = 99999999;

final trampolined1 = tramSum.bounce(n, 0); // 4999999950000000
// or
final trampolined12 = tramSum(n, 0).bounce(); // 4999999950000000

final regular = regularSum(n, 0); // Unhandled exception: Stack Overflow

Memoization

Provides hash-based memoization functionality.

Allows to perform optimization and perform actual computations only when inputs given to the function are given for the first time. It is highly discouraged to use memoization on impure functions, mainly because it does not make very much sense.

Bellow, memoization is used on an impure function only for demonstrational purposes.

int Function(int base) newCounter() {
  var counter = 0;
  return (base) => base + counter++;
}

final counter = newCounter().memoized;

counter(0); // Returns 0, counter becomes 1
counter(10); // Returns 11, counter becomes 2
counter(0); // Returns 0, counter stays 2
counter(11); // Returns 13, count becomes 3

Partial Application

Provides non-curried function partial application.

Existing ways do so through creating a function buffer or through currying, applying arguments, and uncurrying the function back. This approach eliminates the need for two intermediate steps and allows application arguments directly.

int sumThree(int a, int b, int c) => a + b + c;

int buffer(int b, int c) => sumThree(1, b, c);
final currying = sumThree.curry()(1).uncurry(); // Not included in this package
final partial = sumThree.apply(1);
You might also like...

๐Ÿ” ๐Ÿ‘€ CLI utility to check last-visit of your CodeForces friends & much more, ๐Ÿš€ powered by CodeForces API

JoJo ๐Ÿ” ๐Ÿ‘€ CLI utility to check last-visit of your CodeForces friends & much more, ๐Ÿš€ powered by CodeForces API Features Online Friends All Friends Pr

Jul 20, 2020

A Dart build script that downloads the Protobuf compiler and Dart plugin to streamline .proto to .dart compilation.

A Dart build script that downloads the Protobuf compiler and Dart plugin to streamline .proto to .dart compilation.

Oct 26, 2022

Hot restart for Dart console application with fast incremental compilation.

Hot restart for Dart console application with fast incremental compilation. Why do I need this? If your console application compiles too long before r

Oct 7, 2022

Dart wrapper via dart:ffi for https://github.com/libusb/libusb

libusb Dart wrapper via dart:ffi for https://github.com/libusb/libusb Environment Windows(10) macOS Linux(Ubuntu 18.04 LTS) Usage Checkout example Fea

Dec 20, 2022

Extensible Dart interpreter for Dart with full interop

dart_eval is an extensible interpreter for the Dart language, written in Dart. It's powered under the hood by the Dart analyzer, so it achieves 100% c

Dec 28, 2022

The Dart Time Machine is a date and time library for Flutter, Web, and Server with support for timezones, calendars, cultures, formatting and parsing.

The Dart Time Machine is a date and time library for Flutter, Web, and Server with support for timezones, calendars, cultures, formatting and parsing.

The Dart Time Machine is a date and time library for Flutter, Web, and Server with support for timezones, calendars, cultures, formatting and parsing.

Oct 8, 2021

Library for help you make userbot or bot telegram and support tdlib telegram database and only support nodejs dart and google-apps-script

To-Do telegram client dart โœ…๏ธ support multi token ( bot / userbot ) โœ…๏ธ support bot and userbot โœ…๏ธ support telegram-bot-api local server โœ…๏ธ support tel

Jan 7, 2023

An android application built using Flutter that computes the Body Mass Index of person and suggestion to carry ,by taking Inputs (Weight, Height, and Age), Built using Flutter

An android application built using Flutter that computes the Body Mass Index of person and suggestion to carry ,by taking Inputs (Weight, Height, and Age), Built using Flutter

BMI Calculator ๐Ÿ’ช Our Goal The objective of this tutorial is to look at how we can customise Flutter Widgets to achieve our own beautiful user interfa

Nov 2, 2022

A dart package to help you parse and evaluate infix mathematical expressions into their prefix and postfix notations.

A dart package to help you parse and evaluate infix mathematical expressions into their prefix and postfix notations.

Jan 28, 2022
Owner
Yakov Karpov
Yakov Karpov
Call OpenGL ES Api By Dart

Flutter GL Flutter GL can call OpenGL ES API with Dart Support iOS,Android,Web OpenGL ES API Now the api is similar to WebGL How to use Now this is on

zhaolei 163 Jan 5, 2023
This package wraps Airtime, Sms, and Voice call from Africa's Talking APIs.

This package wraps some functionalities from Africa's Talking API The functionalities implemented are; Sms send message fetch messages generate checko

Eddie Genius 7 May 31, 2022
Doing cool stuff with Git VCS in dart programming language.

Git Assistant Assists you to use git and .git in easy and simple way with robust API Features Generates git commands in simple form Supports commands,

Ehsan Aramide 10 Mar 4, 2022
Quiver is a set of utility libraries for Dart that makes using many Dart libraries easier and more convenient, or adds additional functionality.

Quiver is a set of utility libraries for Dart that makes using many Dart libraries easier and more convenient, or adds additional functionality.

Google 905 Jan 2, 2023
Dependency Injection is a great design pattern that allows us to eliminate rigid dependencies between elements and it makes the application more flexible

GetX lib DI pattern Dependency Injection is a great design pattern that allows us to eliminate rigid dependencies between elements and it makes the ap

Trฦฐฦกng Viแป‡t Hoร ng 4 Feb 1, 2022
Collects screen sizes and pixel densities for real iPhones, iPads, Google phones, Samsung phones, and more.

Device Sizes This package aggregates screen sizes and pixel densities for as many physical devices as possible. The purpose of this package is to help

Matt Carroll 16 Jan 8, 2023
Scribble is a lightweight library for freehand drawing in Flutter supporting pressure, variable line width and more!

Scribble Scribble is a lightweight library for freehand drawing in Flutter supporting pressure, variable line width and more! A

Tim Created It. 73 Dec 16, 2022
With this package you can display numbers or any other text more nicely

flutter_number_animation With this package you can display numbers or any other text more nicely Preview Works with text too! How to use Add this to y

idan ben shimon 8 Jun 7, 2022
Official Git of flutter code-push made by Chimera inc. If you want to get more info or seek for biz corporation, you can contact [email protected].

ไธญๆ–‡็‰ˆ Chimera Flutter Code Push Chimera is a Dart compiler developed by ourselves, which generates interpretable and executable bytecode to implement co

Waytoon 21 Oct 6, 2022
A Flutter library to make Rest API clients more easily. Inspired by Java Feing.

A Flutter library to make Rest API clients more easily. Inspired by Java Feing. Features Facilitated JSON encode and decode using common interfaces. F

null 2 Mar 15, 2022