Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

Commit

Permalink
Add hash comprehension support;fixed some example bug
Browse files Browse the repository at this point in the history
  • Loading branch information
haifenghuang committed Dec 20, 2017
1 parent ffb181b commit 17e115c
Show file tree
Hide file tree
Showing 9 changed files with 657 additions and 66 deletions.
48 changes: 40 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,12 @@ t = true
f = false

// Function literals
let f = add(x, y) { return a + b }
println(f(1,2))
let f1 = add(x, y) { return x + y }
println(f1(1,2))

//short-arrow function literals
let f2 = (x, y) -> x + y
println(f2(1,2))
```

### Variables
Expand Down Expand Up @@ -951,28 +955,56 @@ println(hash)
## Comprehensions
Monkey also support list(array,string, range, tuple) or map comprehensions, please see following examples:
Monkey support list(array,string, range, tuple) comprehensions.
list comprehension will return an array.
please see following examples:
```swift
//array comprehension
x = [[word.upper(), word.lower(), word.title()] for word in ["hello", "world", "good", "morning"]]
println(x)
println(x) //result: [["HELLO", "hello", "Hello"], ["WORLD", "world", "World"], ["GOOD", "good", "Good"], ["MORNING", "morning", "Morning"]]
//string comprehension (here string is treated like an array)
y = [ c.upper() for c in "huanghaifeng" where $_ % 2 != 0] //$_ is the index
println(y)
println(y) //result: ["U", "N", "H", "I", "E", "G"]
//range comprehension
w = [i + 1 for i in 2..10]
println(w)
println(w) //result: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
//tuple comprehension
v = [x+1 for x in (12,34,56)]
println(v)
println(v) //result: [13, 35, 57]
//hash comprehension
z = [v * 10 for k,v in {"key1"=>10, "key2"=>20, "key3"=>30}]
println(z)
println(z) //result: [100, 200, 300]
```
Monkey also support hash comprehension.
hash comprehension will return a hash.
please see following examples:
```swift
//hash comprehension (from hash)
z1 = { v:k for k,v in {"key1"=>10, "key2"=>20, "key3"=>30}} //reverse key-value pair
println(z1) // result: {10 => "key1", 20 => "key2", 30 => "key3"}. Order may differ
//hash comprehension (from array)
z2 = {x:x**2 for x in [1,2,3]}
println(z2) // result: {1 => 1, 2 => 4, 3 => 9}. Order may differ
//hash comprehension (from .. range)
z3 = {x:x**2 for x in 5..7}
println(z3) // result: {5 => 25, 6 => 36, 7 => 49}. Order may differ
//hash comprehension (from string)
z4 = {x:x.upper() for x in "hi"}
println(z4) // result: {"h" => "H", "i" => "I"}. Order may differ
//hash comprehension (from tuple)
z5 = {x+1:x+2 for x in (1,2,3)}
println(z5) // result: {4 => 5, 2 => 3, 3 => 4}. Order may differ
```
## grep and map
Expand Down
47 changes: 39 additions & 8 deletions README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,12 @@ t = true
f = false

// Function literals
let f = add(x, y) { return a + b }
println(f(1,2))
let f1 = add(x, y) { return x + y }
println(f1(1,2))

//short-arrow function literals
let f2 = (x, y) -> x + y
println(f2(1,2))
```

### 变量
Expand Down Expand Up @@ -930,28 +934,55 @@ println(hash)
## 列表推导(Comprehensions)
Monkey也支持列表推导(列表可以为数组,字符串,Range,Tuple, 哈希)。请看下面的例子:
Monkey支持列表推导(列表可以为数组,字符串,Range,Tuple, 哈希)。
列表推导的返回值均为数组。请看下面的例子:
```swift
//数组
x = [[word.upper(), word.lower(), word.title()] for word in ["hello", "world", "good", "morning"]]
println(x)
println(x) //结果:[["HELLO", "hello", "Hello"], ["WORLD", "world", "World"], ["GOOD", "good", "Good"], ["MORNING", "morning", "Morning"]]
//字符串
y = [ c.upper() for c in "huanghaifeng" where $_ % 2 != 0] //$_ is the index
println(y)
println(y) //结果:["U", "N", "H", "I", "E", "G"]
//范围
w = [i + 1 for i in 1..10]
println(w)
println(w) //结果:[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
//tuple
v = [x+1 for x in (12,34,56)]
println(v)
println(v) //结果:[13, 35, 57]
//哈希
z = [v * 10 for k,v in {"key1"=>10, "key2"=>20, "key3"=>30}]
println(z)
println(z) //结果:[100, 200, 300]
```
Monkey同时也支持哈希推导。
哈希推导的返回值均为哈希。请看下面的例子:
```swift
//哈希推导 (from hash)
z1 = { v:k for k,v in {"key1"=>10, "key2"=>20, "key3"=>30}} //reverse key-value pair
println(z1) // 结果: {10 => "key1", 20 => "key2", 30 => "key3"}, 顺序可能不同
//哈希推导 (from array)
z2 = {x:x**2 for x in [1,2,3]}
println(z2) // 结果: {1 => 1, 2 => 4, 3 => 9}, 顺序可能不同
//哈希推导 (from .. range)
z3 = {x:x**2 for x in 5..7}
println(z3) // 结果: {5 => 25, 6 => 36, 7 => 49}, 顺序可能不同
//哈希推导 (from string)
z4 = {x:x.upper() for x in "hi"}
println(z4) // 结果: {"h" => "H", "i" => "I"}, 顺序可能不同
//哈希推导 (from tuple)
z5 = {x+1:x+2 for x in (1,2,3)}
println(z5) // 结果: {4 => 5, 2 => 3, 3 => 4}, 顺序可能不同
```
## Grep和map
Expand Down
41 changes: 41 additions & 0 deletions examples/comprehension.my
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//hash comprehension (from hash)
z1 = { v:k for k,v in {"key1"=>10, "key2"=>20, "key3"=>30}} //reverse key-value pair
println(z1)

//hash comprehension (from array)
z2 = {x:x**2 for x in [1,2,3]}
println(z2)

//hash comprehension (from .. range)
z3 = {x:x**2 for x in 5..7}
println(z3)

//hash comprehension (from string)
z4 = {x:x.upper() for x in "hi"}
println(z4)

//hash comprehension (from tuple)
z5 = {x+1:x+2 for x in (1,2,3)}
println(z5)


println("==================================")
//����
x = [[word.upper(), word.lower(), word.title()] for word in ["hello", "world", "good", "morning"]]
println(x)

//�ַ���
y = [ c.upper() for c in "huanghaifeng" where $_ % 2 != 0] //$_ is the index
println(y)

//��Χ
w = [i + 1 for i in 1..10]
println(w)

//tuple
v = [x+1 for x in (12,34,56)]
println(v)

//��ϣ
z = [v * 10 for k,v in {"key1"=>10, "key2"=>20, "key3"=>30}]
println(z)
2 changes: 1 addition & 1 deletion examples/demo.my
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ aChan1.send("Hello Channel!")

# spawn and 'chan' example 2
let aa = [7, 2, 8, -9, 4, 0]
let nn = len(aa) / 2
let nn = int(len(aa) / 2) //because '/' will always return float, so here we need to convert the result to int
let aChan2 = chan()

let cc = fn(aArr, c) {
Expand Down
12 changes: 6 additions & 6 deletions examples/linq.my
Original file line number Diff line number Diff line change
Expand Up @@ -763,9 +763,9 @@ if result == 0 {

//test 'sequenceEqual'
println()
let se1=[1, 2, 2, 3, 1], se11=[4,6]
let se2=[1, -1, 100], se22=[1, -1, 100]
let se3=[], se33=[]
let se1,se11=[1, 2, 2, 3, 1], [4,6]
let se2,se22=[1, -1, 100], [1, -1, 100]
let se3,se33=[], []
result = linq.from(se1).sequenceEqual(linq.from(se11))
if result {
println("[1, 2, 2, 3, 1] sequenceEqual([4,6]) returns true")
Expand Down Expand Up @@ -879,11 +879,11 @@ println('[1,2,3,4,5,6,7,8,9,10] sumInts()={result}')

//test 'sumFloats'
println()
let farr1 = [1., 2., 2., 3., 1.]
let farr1 = [1.0, 2.0, 2.0, 3.0, 1.0]
result = linq.from(farr1).sumFloats()
println("[1., 2., 2., 3., 1.] sumFloats() returns", result)
println("[1.0, 2.0, 2.0, 3.0, 1.0] sumFloats() returns", result)

let farr2 = [1.]
let farr2 = [1.0]
result = linq.from(farr2).sumFloats()
println("[1.] sumFloats() returns", result)

Expand Down
2 changes: 1 addition & 1 deletion examples/test_program.my
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ do {
y = y + 1
}

z = "a b c d e f".split().map(fn(x) {
z = "a b c d e f".split(" ").map(fn(x) {
println(x.upper())
x.upper()
})
Expand Down
Loading

0 comments on commit 17e115c

Please sign in to comment.