Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to utilise partial application in Dart (partial / apply / fixing arguments)

From a function with multiple parameters can we partially apply just one or two parameters to it returning a new function that takes the remaining parameters?

Javascript example using Ramda

function buildUri (scheme, domain, path) {
  return `${scheme}://${domain}/${path}`
}

const buildHttpsUri = R.partial(buildUri, ['https']);

const twitterFavicon = buildHttpsUri('twitter.com', 'favicon.ico');
like image 494
atreeon Avatar asked Nov 22 '18 16:11

atreeon


People also ask

What are the best practices for defining functions in Dart?

In Dart, even functions are objects. Here are some best practices involving functions. DO use a function declaration to bind a function to a name. Modern languages have realized how useful local nested functions and closures are. It’s common to have a function defined inside another one.

Why don’t more DART developers use part?

Many Dart developers avoid using part entirely. They find it easier to reason about their code when each library is a single file. If you do choose to use part to split part of a library out into another file, Dart requires the other file to in turn indicate which library it’s a part of.

What is a partial function application?

Partial function application allows you to modify a function by pre-filling some of the arguments. It is particularly useful in conjunction with functionals and other function operators. Note that an argument can only be partialised once.

When to use exceptions in Dart 2?

Basically, any place where it would be an error to write new instead of const, Dart 2 allows you to omit the const. Dart uses exceptions when an error occurs in your program. The following best practices apply to catching and throwing exceptions.


1 Answers

You can just forward to another function

String buildUri (String scheme, String domain, String path) {
  return `${scheme}://${domain}/${path}`
}

String buildHttpsUri(String domain, String path) => buildUri('https', domain, path);
like image 187
Günter Zöchbauer Avatar answered Oct 23 '22 03:10

Günter Zöchbauer