-
Notifications
You must be signed in to change notification settings - Fork 0
/
Named_parameters.fold
120 lines (75 loc) · 2.52 KB
/
Named_parameters.fold
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
--
-- Named parameters
-- http://rosettacode.org/wiki/Named_parameters#Fold
-- http://briancarper.net/blog/579/keyword-arguments-ruby-clojure-common-lisp
--
-- Named arguments
def range from: (start = 0) to: stop by: (step = 1) =
if start >= stop
[]
else
[start & range from: start + 1 to: stop by: step]
end
range from: 10 to: 200 by: 5
-- Implicit named arguments (all default arguments are named)
def translate :: lang: (`en | `ru) -> String -> String
def translate (lang = `en) message =
match lang
| `en -> English.translate message
| `ru -> Russian.translate message
end
translate lang: `en "Hello"
- Implementation:
- records
- mixfix operators
## Records
if (a > 3) then: "yes" else: "no"
join (s1::String) (to s2::String) (with joiner::String = " ") -> String =
s1 + joiner + s2
join (s1::String) (to s2::String) (with joiner::String = " ") -> String =
join "hello" to: "world" with: ", "
join("hello", to: "world", with: ", ")
range from: start = 0, to: stop, by: step = 1 =
start == stop
? []
: [start, (range from: (start + 1) to: stop by: step)...]
|| Function signature with named parameters.
range :: from: Int -> to: Int -> by: Int
|| Named application syntax
range from: 0 to: 100 by: 5
range from => 0 to 100 by => 5
range ~from: 0 ~to: 100 ~by: 5
let surround string (border = `Double) (padding = 0) =
print ul (ho ^ (|string| + 2 * padding)) ur "\n"
ve (" " ^ padding) string (" " ^ padding) ve "\n"
dl (ho ^ (|string| + 2 * padding)) dr
where ve, ho, ul, ur, dl, dr = border =>
| `Round -> ("\u2502", "\u2500", "\u256d", "\u256e", "\u2570", "\u256f")
| `Bold -> ("\u2503", "\u2501", "\u250F", "\u2513", "\u2517", "\u251b")
| `Double -> ("\u2551", "\u2550", "\u2554", "\u2557", "\u255a", "\u255d")
| `Dotted -> ("\u254e", "\u254c", "\u250c", "\u2510", "\u2514", "\u2518")
| `Cross -> ("\u2502", "\u2500", "\u253c", "\u253c", "\u253c", "\u253c")
-> surround "Hello" padding: 1 border: `double
╔═══════╗
║ Hello ║
╚═══════╝
"hello" |> capitalize |> surround with: ("***", "***") |> print
"hello"
=> capitalize
>> surround with: ("***", "***")
>> print
"hello" => capitalize
(surround with: ("***", "***"))
print
dot =
scale 2 (move (20, 20) (filled blue (circle 10)))
dot =
circle 10
|> filled blue
|> move (20, 20)
|> scale 2
dot =
circle 10
=> filled blue
>> move (20, 20)
>> scale 2