From e26a8a882861411a2cc756825ae4a23380c94512 Mon Sep 17 00:00:00 2001 From: Rnhmjoj Date: Fri, 28 Feb 2014 18:57:10 +0100 Subject: [PATCH] Completed Open, Any, All and improved test --- py/bool.go | 8 ++++---- py/math.go | 2 ++ py/misc.go | 30 +++++++++++++++++++++++------- test.go | 28 +++++++++++++++------------- 4 files changed, 44 insertions(+), 24 deletions(-) diff --git a/py/bool.go b/py/bool.go index 167621d..0661994 100644 --- a/py/bool.go +++ b/py/bool.go @@ -45,7 +45,7 @@ func Bool(x interface{}) bool { return true } -//TODO + func Any(iter interface{}) bool { viter := reflect.ValueOf(iter) for i := 0; i < viter.Len(); i++ { @@ -56,13 +56,13 @@ func Any(iter interface{}) bool { return false } -//TODO + func All(iter interface{}) bool { viter := reflect.ValueOf(iter) for i := 0; i < viter.Len(); i++ { - if Bool(viter.Index(i).Interface()) { + if !Bool(viter.Index(i).Interface()) { return false } } return true -} \ No newline at end of file +} diff --git a/py/math.go b/py/math.go index 6921a1e..ede7424 100644 --- a/py/math.go +++ b/py/math.go @@ -5,10 +5,12 @@ import ( "math" ) + func Abs(x float64) float64 { return math.Abs(x) } + func Pow(args...float64) float64{ switch len(args) { case 2: diff --git a/py/misc.go b/py/misc.go index ac25421..83c9a31 100644 --- a/py/misc.go +++ b/py/misc.go @@ -27,7 +27,7 @@ func Type(obj interface{}) reflect.Type { return reflect.TypeOf(obj) } -//TODO + func Open(args...string) *os.File{ var ( file *os.File @@ -36,14 +36,30 @@ func Open(args...string) *os.File{ switch len(args) { case 1: file, err = os.Open(args[0]) - if err != nil { - panic(err) - } case 2: - file, err = os.Open(args[0]) - if err != nil { - panic(err) + var flags int + switch args[1] { + 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 } diff --git a/test.go b/test.go index 57def76..a4bd704 100644 --- a/test.go +++ b/test.go @@ -6,18 +6,20 @@ import ( ) func main(){ - var test []interface{} - test = append(test, Sum(Map(func (x int) int {return x*x}, Range(30)))) - test = append(test, Chr(89)) - test = append(test, Ord("a")) - test = append(test, Bin(144)) - test = append(test, Hex(5831)) - test = append(test, Bool(1.2)) - test = append(test, Abs(-19.3)) - test = append(test, Pow(2,16)) - test = append(test, Type(true)) - test = append(test, Open("py/misc.go")) - //test = append(test, Input("Scrivi qualcosa: ")) + test := []interface{}{ + Sum(Map(func (x int) int {return x*x}, Range(30))), + Chr(89), + Ord("a"), + Bin(144), + Hex(5831), + Bool(1.2), + Abs(-19.3), + Pow(2,16), + Type(true), + Open("ciao2.txt", "a"), + Any([]interface{}{0, 1, []int{}, ""}), + All([]interface{}{true,"",1}), + Input("Scrivi qualcosa: "), + } fmt.Println(test) - }