Completed Open, Any, All and improved test

This commit is contained in:
Rnhmjoj 2014-02-28 18:57:10 +01:00
parent 605422e60d
commit e26a8a8828
4 changed files with 44 additions and 24 deletions

View File

@ -45,7 +45,7 @@ func Bool(x interface{}) bool {
return true return true
} }
//TODO
func Any(iter interface{}) bool { func Any(iter interface{}) bool {
viter := reflect.ValueOf(iter) viter := reflect.ValueOf(iter)
for i := 0; i < viter.Len(); i++ { for i := 0; i < viter.Len(); i++ {
@ -56,11 +56,11 @@ func Any(iter interface{}) bool {
return false return false
} }
//TODO
func All(iter interface{}) bool { func All(iter interface{}) bool {
viter := reflect.ValueOf(iter) viter := reflect.ValueOf(iter)
for i := 0; i < viter.Len(); i++ { for i := 0; i < viter.Len(); i++ {
if Bool(viter.Index(i).Interface()) { if !Bool(viter.Index(i).Interface()) {
return false return false
} }
} }

View File

@ -5,10 +5,12 @@ import (
"math" "math"
) )
func Abs(x float64) float64 { func Abs(x float64) float64 {
return math.Abs(x) return math.Abs(x)
} }
func Pow(args...float64) float64{ func Pow(args...float64) float64{
switch len(args) { switch len(args) {
case 2: case 2:

View File

@ -27,7 +27,7 @@ func Type(obj interface{}) reflect.Type {
return reflect.TypeOf(obj) return reflect.TypeOf(obj)
} }
//TODO
func Open(args...string) *os.File{ func Open(args...string) *os.File{
var ( var (
file *os.File file *os.File
@ -36,14 +36,30 @@ func Open(args...string) *os.File{
switch len(args) { switch len(args) {
case 1: case 1:
file, err = os.Open(args[0]) file, err = os.Open(args[0])
if err != nil {
panic(err)
}
case 2: case 2:
file, err = os.Open(args[0]) var flags int
if err != nil { switch args[1] {
panic(err) case "w":
flags = os.O_WRONLY | os.O_CREATE
case "w+":
flags = os.O_RDWR | os.O_CREATE
case "a":
flags = os.O_WRONLY | os.O_CREATE | os.O_APPEND
case "a+":
flags = os.O_RDWR | os.O_CREATE | os.O_APPEND
case "r":
flags = os.O_RDONLY
case "x":
_, err = os.Create(args[0])
if err != nil {
panic(err)
}
return nil
} }
file, err = os.OpenFile(args[0], flags, 0666)
}
if err != nil {
panic(err)
} }
return file return file
} }

28
test.go
View File

@ -6,18 +6,20 @@ import (
) )
func main(){ func main(){
var test []interface{} test := []interface{}{
test = append(test, Sum(Map(func (x int) int {return x*x}, Range(30)))) Sum(Map(func (x int) int {return x*x}, Range(30))),
test = append(test, Chr(89)) Chr(89),
test = append(test, Ord("a")) Ord("a"),
test = append(test, Bin(144)) Bin(144),
test = append(test, Hex(5831)) Hex(5831),
test = append(test, Bool(1.2)) Bool(1.2),
test = append(test, Abs(-19.3)) Abs(-19.3),
test = append(test, Pow(2,16)) Pow(2,16),
test = append(test, Type(true)) Type(true),
test = append(test, Open("py/misc.go")) Open("ciao2.txt", "a"),
//test = append(test, Input("Scrivi qualcosa: ")) Any([]interface{}{0, 1, []int{}, ""}),
All([]interface{}{true,"",1}),
Input("Scrivi qualcosa: "),
}
fmt.Println(test) fmt.Println(test)
} }