From c7f2be15434cf8f8cbad78bc18eb6acf1bacba83 Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Sun, 1 Aug 2021 23:46:46 +0200 Subject: [PATCH 1/2] Add puzzler for Iterable.joinToString parameter confusion --- .../FunctionParameterConfusion.kts | 11 +++++++++++ src/syntax/functionParameterConfusion/Rationale.md | 9 +++++++++ src/syntax/gameOfLife/Rationale.md | 2 +- 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/syntax/functionParameterConfusion/FunctionParameterConfusion.kts create mode 100644 src/syntax/functionParameterConfusion/Rationale.md diff --git a/src/syntax/functionParameterConfusion/FunctionParameterConfusion.kts b/src/syntax/functionParameterConfusion/FunctionParameterConfusion.kts new file mode 100644 index 0000000..c4a88fc --- /dev/null +++ b/src/syntax/functionParameterConfusion/FunctionParameterConfusion.kts @@ -0,0 +1,11 @@ +package syntax.functionParameterConfusion + +val items = listOf("a", null, "b") + +println(items.joinToString { " + " }) + +// What will the output be? +// a) null +// b) " + , + , + " +// c) "a, null, b" +// d) "a + null + b" diff --git a/src/syntax/functionParameterConfusion/Rationale.md b/src/syntax/functionParameterConfusion/Rationale.md new file mode 100644 index 0000000..c753dc4 --- /dev/null +++ b/src/syntax/functionParameterConfusion/Rationale.md @@ -0,0 +1,9 @@ +Correct answer: **b) " + , + , + "** + +`joinToString` has a default argument for every parameter, including the _separator_. The code uses curly brackets +(`{ ... }`) which therefore represents the _transform_ function, transforming every element. The parameter of the +transform function was omitted. + +The lesson from this: Pay attention to the difference between parentheses and curly brackets and verify that you are +passing a value for the correct parameter. Also be careful when using suggestions from the IDE, here IntelliJ IDEA +would have suggested the variant with _transform_ function first. diff --git a/src/syntax/gameOfLife/Rationale.md b/src/syntax/gameOfLife/Rationale.md index 44fcad4..4c53eb6 100644 --- a/src/syntax/gameOfLife/Rationale.md +++ b/src/syntax/gameOfLife/Rationale.md @@ -1,6 +1,6 @@ Correct answer: **b) 3** -* Languges without semicolons need to make decisions when a statement ends +* Languages without semicolons need to make decisions when a statement ends * Lines starting with '+' can be interpreted as starting with unary '+' * Groovy has the same problem, but JavaScript hasn't * Fix by keeping '+' on the previous line From 2fc545404fbef61cc352bd7ebbc160dd676fa327 Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Fri, 13 Aug 2021 18:25:38 +0200 Subject: [PATCH 2/2] Improve FunctionParameterConfusion puzzler --- .../FunctionParameterConfusion.kts | 10 +++++----- src/syntax/functionParameterConfusion/Rationale.md | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/syntax/functionParameterConfusion/FunctionParameterConfusion.kts b/src/syntax/functionParameterConfusion/FunctionParameterConfusion.kts index c4a88fc..af4d0a7 100644 --- a/src/syntax/functionParameterConfusion/FunctionParameterConfusion.kts +++ b/src/syntax/functionParameterConfusion/FunctionParameterConfusion.kts @@ -1,11 +1,11 @@ package syntax.functionParameterConfusion -val items = listOf("a", null, "b") +val items = listOf("a", "b", "c") println(items.joinToString { " + " }) // What will the output be? -// a) null -// b) " + , + , + " -// c) "a, null, b" -// d) "a + null + b" +// a) "a + b + c" +// b) "a b c" +// c) "a, b, c" +// d) None of the above diff --git a/src/syntax/functionParameterConfusion/Rationale.md b/src/syntax/functionParameterConfusion/Rationale.md index c753dc4..3a3cf7f 100644 --- a/src/syntax/functionParameterConfusion/Rationale.md +++ b/src/syntax/functionParameterConfusion/Rationale.md @@ -1,8 +1,8 @@ -Correct answer: **b) " + , + , + "** +Correct answer: **d) None of the above** - the created string is actually `" + , + , + "` `joinToString` has a default argument for every parameter, including the _separator_. The code uses curly brackets -(`{ ... }`) which therefore represents the _transform_ function, transforming every element. The parameter of the -transform function was omitted. +(`{ ... }`) which therefore represents the _transform_ function, transforming every element. The parameter (`it`) of +the transform function was omitted. The lesson from this: Pay attention to the difference between parentheses and curly brackets and verify that you are passing a value for the correct parameter. Also be careful when using suggestions from the IDE, here IntelliJ IDEA