Skip to content

Commit 784a045

Browse files
author
Rizo Isrof
committed
Finalizing the syntax decisions
1 parent 9cfc7b9 commit 784a045

33 files changed

+3039
-354
lines changed

Anonymous_functions.fold

+38-39
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,59 @@
22
-- # Anonymous Functions
33
--
44

5-
5+
-- Simple lambda functions.
6+
x -> x
67
x -> x + x
78

9+
-- Lambda function with a block.
810
do x ->
9-
x + x
11+
a = x + x
12+
a + 1
1013
end
1114

15+
-- Apply a transformation using a lambda.
16+
map (x -> x * x) [1..10]
1217

13-
map (x => x * x) [1..10]
14-
15-
map { 2 => x * 4
16-
x => x * x } [1..10]
17-
18-
[1..10] . map { \1 * \2 }
18+
-- Lambdas can use pattern matching with the `|` alternative operator.
19+
-- The following lambda will replace 5 by 0.
20+
map (5 -> 0 | x -> x * x) [1..10]
1921

22+
-- The same variable can be referenced multiple times.
23+
\x + \x
2024

21-
tree . { Leaf => "x"
22-
Node x => str x }
2325

24-
tree =>
25-
| Leaf -> "x"
26-
| Node x -> str x
26+
-- * --
2727

28-
=> \ x y => x + y
29-
=> \ 5
30-
=> (~ + ~)
28+
-> \ x y -> x + y
29+
-> \ 5
30+
-> (~ + ~)
3131

3232
(-> system :fetcher :conf :credentials :username)
3333

3434
-> system #fetcher #conf #credentials #username
3535

36-
-> map (data flag => process data verbose: (not flag)) [1, 2, 3, 4, 5]
37-
=> map (process _ verbose: (not _)) [1, 2, 3, 4, 5]
36+
-> map (data flag -> process data verbose: (not flag)) [1, 2, 3, 4, 5]
37+
-> map (process _ verbose: (not _)) [1, 2, 3, 4, 5]
3838

39-
=> map { data flag => process data verbose: (not flag) } [1, 2, 3, 4, 5]
40-
=> map { process _ verbose: (not _) } [1, 2, 3, 4, 5]
39+
-> map { data flag -> process data verbose: (not flag) } [1, 2, 3, 4, 5]
40+
-> map { process _ verbose: (not _) } [1, 2, 3, 4, 5]
4141

42-
=> map (data flag => process data verbose: (not flag)) [1, 2, 3, 4, 5]
43-
=> map {process _ verbose: (not _)} [1, 2, 3, 4, 5]
42+
-> map (data flag -> process data verbose: (not flag)) [1, 2, 3, 4, 5]
43+
-> map {process _ verbose: (not _)} [1, 2, 3, 4, 5]
4444

45-
=> map (λ data flag process data verbose: (not flag)) [1, 2, 3, 4, 5]
46-
=> map λ(process _ verbose: (not _)) [1, 2, 3, 4, 5]
45+
-> map (λ data flag -> process data verbose: (not flag)) [1, 2, 3, 4, 5]
46+
-> map λ(process _ verbose: (not _)) [1, 2, 3, 4, 5]
4747

48-
=> map (data flag => process data verbose: (not flag)) [1, 2, 3, 4, 5]
49-
=> map {process _ verbose: (not _)} [1, 2, 3, 4, 5]
48+
-> map (data flag -> process data verbose: (not flag)) [1, 2, 3, 4, 5]
49+
-> map {process _ verbose: (not _)} [1, 2, 3, 4, 5]
5050

51-
=> map (|data flag| => process data verbose: (not flag)) [1, 2, 3, 4, 5]
52-
=> map {process _ verbose: (not _)} [1, 2, 3, 4, 5]
51+
-> map (|data flag| -> process data verbose: (not flag)) [1, 2, 3, 4, 5]
52+
-> map {process _ verbose: (not _)} [1, 2, 3, 4, 5]
5353

54-
=> map (|x| => x * x) [1 .. 5]
54+
-> map (|x| -> x * x) [1 .. 5]
5555
= [1, 4, 9, 16, 25]
5656

57-
=> map \(_1 * _1) [1 .. 5]
57+
-> map \(_1 * _1) [1 .. 5]
5858
= [1, 4, 9, 16, 25]
5959

6060
-> map \(_1 * _1) [1 .. 5]
@@ -65,23 +65,22 @@ map { 2 => x * 4
6565
-- Mathematica
6666
-- If[#1 == 1, 1, #1 * #0[#1-1]]&
6767

68-
=> \(_1 == 1 ? 1 : _1 * (_0 (_1 - 1)))
69-
=> \(if (_1 == 1): 1 else: _1 * (_0 (_1 - 1)))
68+
-> \(_1 == 1 ? 1 : _1 * (_0 (_1 - 1)))
69+
-> \(if (_1 == 1): 1 else: _1 * (_0 (_1 - 1)))
7070

71-
=> \(\1 == 1 ? 1 : @1 * (@0 (@1 - 1)))
71+
-> \(\1 == 1 ? 1 : @1 * (@0 (@1 - 1)))
7272

7373

74-
fix = \f => (\x a => f (x x) a) (\x a => f (x x) a)
74+
fix = \f -> (\x a -> f (x x) a) (\x a -> f (x x) a)
7575

7676
fix = f -> (x a -> f (x x) a)
7777
(x a -> f (x x) a)
7878

79-
8079
-- // --
8180

8281
-- Anonymous functions can be wrapped in blocks.
8382

84-
[1..99] => fold init: {} do dict i ->
83+
[1..99] -> fold init: {} do dict i ->
8584
dict # i::String <- i + i
8685
end
8786

@@ -90,7 +89,7 @@ end
9089
-- * Since blocks are used, at any time you can extend the block with new statements.
9190
-- * `do` is a named argument for the lambda argument for `fold`.
9291

93-
squares = [1..9] -> map do n =>
92+
squares = [1..9] -> map do n ->
9493
n * n
9594
end
9695

@@ -99,12 +98,12 @@ squares = [1..9] >- map do n ->
9998
n * n
10099
end
101100

102-
squares = [1..9] -> map do n =>
101+
squares = [1..9] -> map do n ->
103102
n * n
104103
end
105104

106105

107-
squares = [1..9] -> map do n =>
106+
squares = [1..9] -> map do n ->
108107
n * n
109108
end
110109

Blocks.fold

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
23
-- https://programmers.stackexchange.com/questions/135544/why-are-several-popular-programming-languages-influenced-by-c
34

45
-- in function application \n denotes :
@@ -10,6 +11,25 @@ do: expr
1011
do expr end
1112

1213

14+
if (x > 3)
15+
print "Too big!"
16+
end
17+
18+
if (x > 3) : print "Too big!"
19+
20+
if (x > 3)
21+
print "Too big!"
22+
else
23+
print "Just fine."
24+
end
25+
26+
if (x > 3) : print "Too big!" else: print "Just fine."
27+
28+
print ((x > 3) ? "Too big!" : "Just fine.")
29+
30+
31+
-- // --
32+
1333
fun dist ((x0, y0), (x1, y1)) = let
1434
val dx = x1 - x0
1535
val dy = y1 - y0
@@ -325,7 +345,7 @@ function (factorial (n::Int) -> Int)
325345
end
326346

327347

328-
function factorial (n::Int) -> Int
348+
function factorial n::Int -> Int
329349
Log.info "Will calculate factorial of {n}"
330350
(n == 0) ? 1 : n * factorial (n - 1)
331351
end
@@ -391,6 +411,15 @@ in:
391411

392412
-- // --
393413

414+
def greet name
415+
print "Hello, " + name "!!!"
416+
end
417+
418+
419+
def greet name {
420+
print "Hello, " + name "!!!"
421+
}
422+
394423

395424
function greet name do
396425
print "Hello, " + name "!!!"

Collections.fold

+111
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,96 @@
11

2+
---
3+
-- Flow
4+
---
5+
6+
sum = do
7+
await >>=
8+
| None -> 0
9+
| Some a -> a + sum
10+
end
11+
12+
fold :: (a -> b -> a) -> a -> Consumer b a
13+
fold f z = go z where
14+
go = next >>= | Some a -> go (f acc a)
15+
| None -> return acc
16+
17+
filter pred = do
18+
a <- await
19+
if | pred a -> yield a >> filter pred
20+
| otherwise -> filter pred
21+
22+
filter = find_all
23+
24+
25+
--
26+
-- Haskell prelude version
27+
--
28+
29+
filter :: (a -> Bool) -> [a] -> [a]
30+
filter _pred [] = []
31+
filter pred (x:xs)
32+
| pred x = x : filter pred xs
33+
| otherwise = filter pred xs
34+
35+
36+
filter :: Foldable f => (a -> Bool) -> f a -> f a
37+
filter _pred [] = []
38+
filter pred (x:xs)
39+
| pred x = x : filter pred xs
40+
| otherwise = filter pred xs
41+
42+
43+
filter f [] = []
44+
filter f (x & xs) = if f x then x & filter f xs
45+
else filter f xs
46+
47+
filter =
48+
| pred [] -> []
49+
| pred (x & xs) -> f x ? x & filter f xs : filter f xs
50+
51+
filter =
52+
| pred [] -> []
53+
| pred (x & xs) if pred x -> x & filter f xs
54+
| pred (_ & xs) -> filter f xs
55+
56+
filter pred xs =
57+
case xs
58+
| [] -> []
59+
| (x & xs) if pred x -> x & filter f xs
60+
| (_ & xs) -> filter f xs
61+
62+
-- Using list builder syntax.
63+
filter pred xs = [ x : x <- xs, pred x ]
64+
65+
66+
--
67+
-- Flow's implementation with explicit constructors
68+
--
69+
70+
let rec filter pred =
71+
fun () -> Await (fun a ->
72+
if pred a
73+
then fun () -> Yield (a, filter pred)
74+
else filter pred)
75+
76+
-- Same as original, but with monadic syntax.
77+
filter pred =
78+
await >>= a ->
79+
if pred a
80+
then yield a >> filter pred
81+
else filter pred
82+
83+
filter pred =
84+
x <- await
85+
if | pred x -> yield x >> filter pred
86+
| otherwise -> filter pred
87+
88+
filter pred = [ x : x <- await, pred x ]
89+
90+
91+
92+
---
93+
294
matrix = [1, 2, 3;
395
4, 5, 6]
496
Iter.at(matrix, 1) # => [4, 5, 6]
@@ -93,3 +185,22 @@ fibs = [1 & 1 & zip fibs (tail fibs) with: (+)]
93185
fibs = 1 & 1 & (zip fibs (tail fibs) with: (+))
94186

95187

188+
--
189+
-- Operations inspired by APL
190+
--
191+
-- https://godoc.org/robpike.io/ivy
192+
193+
-- Find indices in `a` that exists in `b`.
194+
-> a = [10, 20, 30, 40, 50]
195+
-> b = [2, 5, 10, 30]
196+
-> filter ((a in? b) # \x) (^ (# a))
197+
:: [Int] = [0, 2]
198+
199+
-- A ← 10 20 30 40 50
200+
-- B ← 2 5 10 30
201+
-- (A ∊ B) / ⍳⍴A
202+
-- 1 3
203+
204+
205+
206+

Comments.fold

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
-- Single-line comments
3+
4+
x = 42 -- the answer
5+
6+
{{}}
7+
{{ }}
8+
{{xyz}}
9+
{{ xyz }}
10+
11+
12+
{{
13+
}}
14+
15+
{{
16+
next [] = return ()
17+
next (a & l) = yield a >> next l
18+
}}
19+
20+
fold (*) 0 [1, 2, 3] = 6
21+
22+

0 commit comments

Comments
 (0)