Skip to content

Commit c36a96e

Browse files
committed
Added block function syntax sugar for calls.
1 parent 9ba9600 commit c36a96e

File tree

19 files changed

+258
-41
lines changed

19 files changed

+258
-41
lines changed

core/source/Array.mint

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module Array {
1212
Array.any([1, 3], (number : Number) { number % 2 == 0 }) == false
1313
*/
1414
fun any (array : Array(item), function : Function(item, Bool)) : Bool {
15-
Array.find(array, function) != Maybe.Nothing
15+
find(array, function) != Maybe.Nothing
1616
}
1717

1818
/*
@@ -23,7 +23,7 @@ module Array {
2323
Array.all(["hello", "mint"], (str : String) { String.size(str) > 3 }) == true
2424
*/
2525
fun all (array : Array(item), function : Function(item, Bool)) : Bool {
26-
reduce(array, true, (memo : Bool, val : item) { memo && function(val) })
26+
reduce(array, true) { |memo : Bool, val : item| memo && function(val) }
2727
}
2828

2929
/*
@@ -52,13 +52,12 @@ module Array {
5252
Array.compact([Maybe.Just("A"), Maybe.Nothing]) == ["A"]
5353
*/
5454
fun compact (array : Array(Maybe(item))) : Array(item) {
55-
Array.reduce(array, [],
56-
(memo : Array(item), item : Maybe(item)) : Array(item) {
57-
case item {
58-
Just(value) => Array.push(memo, value)
59-
Nothing => memo
60-
}
61-
})
55+
reduce(array, []) { |memo : Array(item), item : Maybe(item)|
56+
case item {
57+
Just(value) => Array.push(memo, value)
58+
Nothing => memo
59+
}
60+
}
6261
}
6362

6463
/*
@@ -96,7 +95,7 @@ module Array {
9695
Array.delete(["a", "b", "c", "a"], "a") == ["b", "c"]
9796
*/
9897
fun delete (array : Array(item), what : item) : Array(item) {
99-
reject(array, (item : item) { item == what })
98+
reject(array) { |item : item| item == what }
10099
}
101100

102101
/*
@@ -582,7 +581,7 @@ module Array {
582581
*/
583582
fun reverseIf (array : Array(item), condition : Bool) : Array(item) {
584583
if condition {
585-
Array.reverse(array)
584+
reverse(array)
586585
} else {
587586
array
588587
}
@@ -714,8 +713,7 @@ module Array {
714713
Array.sum([]) == 0
715714
*/
716715
fun sum (array : Array(Number)) : Number {
717-
Array.reduce(array, 0,
718-
(memo : Number, item : Number) : Number { item + memo })
716+
Array.reduce(array, 0) { |memo : Number, item : Number| item + memo }
719717
}
720718

721719
/*

core/source/CSV.mint

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -256,25 +256,23 @@ module CSV {
256256
lineEnding : CSV.LineEnding = CSV.LineEnding.Unix
257257
) : String {
258258
rows
259-
|> Array.map(
260-
(row : Array(String)) {
261-
Array.map(row,
262-
(field : String) {
263-
// Double quotes need to be escaped with an extra doublequote.
264-
let value =
265-
String.replace(field, "\"", "\"\"")
266-
267-
// If the string contains a separator, \n, \r\n or " it needs to
268-
// be escaped by wrapping in double quotes.
269-
if String.contains(value, separator) || String.contains(value, "\n") || String.contains(
270-
value, "\"") {
271-
"\"#{value}\""
272-
} else {
273-
value
274-
}
275-
})
276-
})
277-
|> Array.map((row : Array(String)) { String.join(row, separator) })
259+
|> Array.map() { |row : Array(String)|
260+
Array.map(row) { |field : String|
261+
// Double quotes need to be escaped with an extra doublequote.
262+
let value =
263+
String.replace(field, "\"", "\"\"")
264+
265+
// If the string contains a separator, \n, \r\n or " it needs to
266+
// be escaped by wrapping in double quotes.
267+
if String.contains(value, separator) || String.contains(value, "\n") || String.contains(
268+
value, "\"") {
269+
"\"#{value}\""
270+
} else {
271+
value
272+
}
273+
}
274+
}
275+
|> Array.map() { |row : Array(String)| String.join(row, separator) }
278276
|> String.join(
279277
case lineEnding {
280278
Windows => "\r\n"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
component Main {
2+
fun renderTest (block : Function(String, Html)) {
3+
block("TEST")
4+
}
5+
6+
fun render : Html {
7+
renderTest() { |x : String|
8+
<div>x</div>
9+
}
10+
}
11+
}
12+
--------------------------------------------------------------------------------
13+
import { createElement as A } from "./runtime.js";
14+
15+
export const B = () => {
16+
const a = (b) => {
17+
return b(`TEST`)
18+
};
19+
return a((c) => {
20+
return A(`div`, {}, [c])
21+
})
22+
};

spec/examples/call

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,15 @@ component Main {
292292
<div>"Hello World!"</div>
293293
}
294294
}
295+
--------------------------------------------------------------------------------
296+
component Main {
297+
fun renderTest (block : Function(String, Html)) {
298+
block("TEST")
299+
}
300+
301+
fun render : Html {
302+
renderTest() { |x : String|
303+
<div>x</div>
304+
}
305+
}
306+
}

spec/formatters/call_with_block

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
component Main {
2+
fun renderTest (block : Function(String, Html)) {
3+
block("TEST")
4+
}
5+
6+
fun render : Html {
7+
renderTest() { |x:String|
8+
<div>x</div>
9+
}
10+
}
11+
}
12+
--------------------------------------------------------------------------------
13+
component Main {
14+
fun renderTest (block : Function(String, Html)) {
15+
block("TEST")
16+
}
17+
18+
fun render : Html {
19+
renderTest() { |x : String| <div>x</div> }
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
component Main {
2+
fun renderTest (block : Function(String, Html)) {
3+
block("TEST")
4+
}
5+
6+
fun render : Html {
7+
renderTest() { |x:String|
8+
<div>"Hello World! This is a long, long, long line...#{x}"</div>
9+
}
10+
}
11+
}
12+
--------------------------------------------------------------------------------
13+
component Main {
14+
fun renderTest (block : Function(String, Html)) {
15+
block("TEST")
16+
}
17+
18+
fun render : Html {
19+
renderTest() { |x : String|
20+
<div>"Hello World! This is a long, long, long line...#{x}"</div>
21+
}
22+
}
23+
}

src/ast/block_function.cr

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Mint
2+
class Ast
3+
class BlockFunction < Node
4+
getter arguments, body, type
5+
6+
def initialize(@arguments : Array(Argument),
7+
@from : Parser::Location,
8+
@to : Parser::Location,
9+
@file : Parser::File,
10+
@type : Node?,
11+
@body : Block)
12+
end
13+
end
14+
end
15+
end

src/ast/call.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Mint
22
class Ast
33
class Call < Node
4-
getter arguments, expression, await
4+
getter arguments, expression, await, block
55

66
def initialize(@arguments : Array(Field),
77
@from : Parser::Location,

src/compilers/inline_function.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Mint
22
class Compiler
3-
def compile(node : Ast::InlineFunction) : Compiled
3+
def compile(node : Ast::InlineFunction | Ast::BlockFunction) : Compiled
44
compile node do
55
body =
66
case item = node.body

src/formatter.cr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ module Mint
8989

9090
# The possibilities on how to format groups.
9191
enum Behavior
92+
BreakAllButFirst
9293
BreakNotFits
9394
BreakAll
9495
Block
@@ -149,6 +150,7 @@ module Mint
149150
def format_arguments(
150151
nodes : Array(Ast::Node), *,
151152
empty_parenthesis = true,
153+
block = false,
152154
) : Nodes
153155
return empty_parenthesis ? ["()"] of Node : [] of Node if nodes.empty?
154156

@@ -162,12 +164,19 @@ module Mint
162164
end
163165
end || Behavior::BreakNotFits
164166

167+
ends =
168+
if block
169+
{"|", "|"}
170+
else
171+
{"(", ")"}
172+
end
173+
165174
[
166175
Group.new(
167176
items: nodes.map(&->format(Ast::Node)),
168177
behavior: behavior,
169-
ends: {"(", ")"},
170178
separator: ",",
179+
ends: ends,
171180
pad: false),
172181
] of Node
173182
end

0 commit comments

Comments
 (0)