Speed up development with full-stack environments for every branch.

Learn More

Functions in Dart are Fabulous

197 Runs 292 Views 5 Copies

Demonstration of safer and cleaner medical calculations using Dart Functions.

Saved

Saved

damondouglas 16

damondouglas
published 3 years ago

/*
  Point of this example is to show how Dart Function objects make
  calculations cleaner and safer.
  
  Example in question is a Glomular Filtration Rate (GFR) calculation,
  a preferred equation for Chronic Kidney Disease (CKD) staging.
  Courtesy:   http://pharmacistsletter.therapeuticresearch.com/
              Lexicomp Online
  
  Estimated GFR:
  eGFR = 175 X (Creatinine)^-1.154 X (Age)^-0.203 X (Gender) X (Race)
  where:

  eGFR = estimated GFR; calculated in mL/minute/1.73 m2
  Creatinine is input in mg/dL
  Age is input in years
  Gender: Females: Gender = 0.742; Males: Gender = 1
  Race: Black: Race = 1.212; White or other: Race = 1
*/

import 'dart:math';

/*
  Typical ugly way to program a medical calculation.
*/
num uglyGFR(num creatinine, int age, bool female, bool black) {
  num gender = female ? 0.742 : 1;
  num race = black ? 1.212 : 1;
  
  return 175 * pow(creatinine, -1.154) * pow(age, -0.203) * gender * race;
}

/*
  Elegant Dart way to program a medical calculation.
  Create a function unique to the patient!
*/

Function gfr(int age, bool female, bool black) {
  num gender = female ? 0.742 : 1;
  num race = black ? 1.212 : 1;
  return (num creatinine) {
    return 175 * pow(creatinine, -1.154) * pow(age, -0.203) * gender * race;
  };
}

void main() {
  num creatinine = 2.1;
  int age = 45;
  bool female = true;
  bool black = true;
  
  num egfr_ugly = uglyGFR(creatinine, age, female, black);
  print("Estimated GFR (ugly): $egfr_ugly");
  
  var eGFR_calc = gfr(age, female, black);
  
  num egfr_clean = eGFR_calc(creatinine);
  
  print("Estimated GFR (clean): $egfr_clean");
  
}
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).