Re:Why I Prefer Functional Programming
by 0xcoffee on Thursday September 19, 2019 @04:56PM
(#21020678)
Obligatory C#:
Most modern `oop` languages these days all support functional constructs and achieve the same thing in same amount of code & style. Language and oop/functional style are not mutually exclusive anymore, which this article seems to overlook by comparing both languages and styles at the same time.
[ Parent
| on HN ]
Re:Why I Prefer Functional Programming
by fnord123 on Thursday September 19, 2019 @05:51PM
(#21021077)
This looks like the Java if the Java was written by someone who knows Java:
List<String> alignText(List<String> texts) {
int maxLength = texts.stream().mapToInt(String::length).max().orElse(0);
return texts.stream().map(text -> {
var spaceCount = (maxLength - text.length()) / 2;
return " ".repeat(spaceCount) + text;
}).collect(Collectors.toList());
}/nit Move the lambda to private method and replace it with method reference and it’ll be just perfect
Re:Why I Prefer Functional Programming
by fnord123 on Friday September 20, 2019 @05:36AM
(#21024827)
Good feedback. +1
I tend to avoid doing a Select followed straight by a Max when you can just pass the select function directly into the max. Also multiline lambda expressions feel ugly but that's probably just me! This is what I would do these days:
string pad(this string e, int padLength) => new String(' ', padLength) + e;
var input = new string[] { "abc", "ab", "abcdef", "abcdefgh" };
var maxLength = input.Max(e => e.Length);
var output = input.Select(e => e.pad((maxLength - e.Length) / 2));Sadly C# is still missing a few important ML features that making functional programming more difficult than in a true ML.
It would be interesting if you could mention which feature/s you think it most sorely lacks. Some of the recent syntax additions have made it really easy to write concise functional code. I don't even use curly braces much anymore. What about f#?