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
}
//TODO
func Any(iter interface{}) bool {
viter := reflect.ValueOf(iter)
for i := 0; i < viter.Len(); i++ {
@ -56,11 +56,11 @@ 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
}
}

View File

@ -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:

View File

@ -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])
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
}

28
test.go
View File

@ -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)
}