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

2

6

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');
Younglove answered 22/11, 2018 at 16:4 Comment(0)
P
5

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);
Precedent answered 22/11, 2018 at 16:13 Comment(0)
M
0

The answer to OP's question is: NO, partial application and curried functions are not supported features of the Dart language. Günter Zöchbauer's answer provides a workaround that may work for you.

There is a package named functional that can be used to do what you want.

import 'package:functional/functional.dart';

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

const buildHttpsUri = buildUri % 'https';

const twitterFavicon = buildHttpsUri('twitter.com', 'favicon.ico');
Martella answered 25/8, 2024 at 21:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.