Initial commit

This commit is contained in:
Rnhmjoj 2014-02-21 21:25:11 +01:00
commit ce8637173b
7 changed files with 286 additions and 0 deletions

18
README.md Normal file
View File

@ -0,0 +1,18 @@
# py
## Python builtins for golang
### Info
Attempt to implement most important python builtin functions in golang
### Usage
Install with `go get github.com/rnhmjoj/py`
Use with `import (. py)`
### Docs
See [Python docs](http://docs.python.org/3/library/functions.html). Functions should behave in the same way.
### License
Dual licensed under the MIT and GPL licenses:
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html

68
py/bool.go Normal file
View File

@ -0,0 +1,68 @@
package py
import ("reflect")
func Bool(x interface{}) bool {
if x == nil {
return false
}
vx := reflect.ValueOf(x)
switch vx.Kind() {
case reflect.Bool: {
if vx.Bool() {
return true
}
}
case reflect.Int, reflect.Int8,
reflect.Int16, reflect.Int32,
reflect.Int64:
if vx.Int() == 0 {
return false
}
case reflect.Uint, reflect.Uint8,
reflect.Uint16, reflect.Uint32,
reflect.Uint64:
if vx.Uint() == 0 {
return false
}
case reflect.Float32, reflect.Float64:
if vx.Float() == 0.0 {
return false
}
case reflect.String:
if vx.String() == "" {
return false
}
case reflect.Array, reflect.Slice,
reflect.Map:
if vx.Len() == 0 {
return false
}
default:
panic("Undefined for type: " + vx.Kind().String())
}
return true
}
//TODO
func Any(iter interface{}) bool {
viter := reflect.ValueOf(iter)
for i := 0; i < viter.Len(); i++ {
if Bool(viter.Index(i).Interface()) {
return true
}
}
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()) {
return false
}
}
return true
}

77
py/lambda.go Normal file
View File

@ -0,0 +1,77 @@
package py
import ("reflect")
func Map(f interface{}, xs interface{}) interface{} {
vf := reflect.ValueOf(f)
vxs := reflect.ValueOf(xs)
tys := reflect.SliceOf(vf.Type().Out(0))
vys := reflect.MakeSlice(tys, vxs.Len(), vxs.Len())
for i := 0; i < vxs.Len(); i++ {
y := vf.Call([]reflect.Value{vxs.Index(i)})[0]
vys.Index(i).Set(y)
}
return vys.Interface()
}
func add(a, b interface{}) interface{} {
va := reflect.ValueOf(a)
vb := reflect.ValueOf(b)
switch va.Kind() {
case reflect.Int, reflect.Int8,
reflect.Int16, reflect.Int32,
reflect.Int64:
return va.Int() + vb.Int()
case reflect.Uint, reflect.Uint8,
reflect.Uint16, reflect.Uint32,
reflect.Uint64:
return va.Uint() + vb.Uint()
case reflect.Float32, reflect.Float64:
return va.Float() + vb.Float()
case reflect.String:
return va.String() + vb.String()
default:
panic("Undefined for type: " + va.Kind().String())
}
}
func Sum(iter interface{}) interface{} {
viter := reflect.ValueOf(iter)
sf := viter.Index(0).Interface()
for i := 0; i < viter.Len(); i++ {
sf = add(sf, viter.Index(i).Interface())
}
return sf
}
func Range(args...int) ([]int) {
var start, stop, step int
switch len(args){
case 1:{
start = 0
stop = args[0]
step = 1
}
case 2:{
start = args[0]
stop = args[1]
step = 1
}
case 3:{
start = args[0]
stop = args[1]
step = args[2]
}
}
l := []int{}
for i := start; i < stop; i += step {
l = append(l, i)
}
return l
}

33
py/math.go Normal file
View File

@ -0,0 +1,33 @@
package py
import (
"strconv"
"math"
)
func Abs(x float64) float64 {
return math.Abs(x)
}
func Pow(args...float64) float64{
switch len(args) {
case 2:
return math.Pow(args[0], args[1])
case 3:
return math.Mod(math.Pow(args[0], args[1]), args[2])
default:
panic("Pow requires 2 or 3 arguments")
}
}
func Bin(x int64) string {
return strconv.FormatInt(x, 2)
}
func Hex(x int64) string {
return strconv.FormatInt(x, 16)
}

51
py/misc.go Normal file
View File

@ -0,0 +1,51 @@
package py
import (
"fmt"
"os"
"bufio"
"reflect"
"strings"
)
func Input(out string) string {
var (
in string
err error
reader *bufio.Reader
)
fmt.Print(out)
reader = bufio.NewReader(os.Stdin)
in, err = reader.ReadString('\n')
if err != nil {
panic(err)
}
return strings.TrimRight(in, "\n")
}
func Type(obj interface{}) reflect.Type {
return reflect.TypeOf(obj)
}
//TODO
func Open(args...string) *os.File{
var (
file *os.File
err error
)
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)
}
}
return file
}
//TODO min, max

16
py/str.go Normal file
View File

@ -0,0 +1,16 @@
package py
import ("fmt")
func Chr(x rune) string {
return fmt.Sprintf("%c", x)
}
func Ord(x string) rune {
if len(x) != 1 {
panic("Ord require a string with lenght 1")
}
return rune(x[0])
}

23
test.go Normal file
View File

@ -0,0 +1,23 @@
package main
import (
. "./py"
"fmt"
)
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: "))
fmt.Println(test)
}