@@ -47,8 +47,8 @@ func init() {
47
47
// py.MustNewMethod("iter", builtin_iter, 0, iter_doc),
48
48
py .MustNewMethod ("len" , builtin_len , 0 , len_doc ),
49
49
py .MustNewMethod ("locals" , py .InternalMethodLocals , 0 , locals_doc ),
50
- // py.MustNewMethod("max", builtin_max, 0, max_doc),
51
- // py.MustNewMethod("min", builtin_min, 0, min_doc),
50
+ py .MustNewMethod ("max" , builtin_max , 0 , max_doc ),
51
+ py .MustNewMethod ("min" , builtin_min , 0 , min_doc ),
52
52
py .MustNewMethod ("next" , builtin_next , 0 , next_doc ),
53
53
py .MustNewMethod ("open" , builtin_open , 0 , open_doc ),
54
54
// py.MustNewMethod("oct", builtin_oct, 0, oct_doc),
@@ -772,6 +772,95 @@ func builtin_len(self, v py.Object) (py.Object, error) {
772
772
return py .Len (v )
773
773
}
774
774
775
+ const max_doc = `
776
+ max(iterable, *[, default=obj, key=func]) -> value
777
+ max(arg1, arg2, *args, *[, key=func]) -> value
778
+
779
+ With a single iterable argument, return its biggest item. The
780
+ default keyword-only argument specifies an object to return if
781
+ the provided iterable is empty.
782
+ With two or more arguments, return the largest argument.`
783
+
784
+ func builtin_max (self py.Object , args py.Tuple , kwargs py.StringDict ) (py.Object , error ) {
785
+ return min_max (args , kwargs , "max" )
786
+ }
787
+
788
+ const min_doc = `
789
+ min(iterable, *[, default=obj, key=func]) -> value
790
+ min(arg1, arg2, *args, *[, key=func]) -> value
791
+
792
+ With a single iterable argument, return its smallest item. The
793
+ default keyword-only argument specifies an object to return if
794
+ the provided iterable is empty.
795
+ With two or more arguments, return the smallest argument.`
796
+
797
+ func builtin_min (self py.Object , args py.Tuple , kwargs py.StringDict ) (py.Object , error ) {
798
+ return min_max (args , kwargs , "min" )
799
+ }
800
+
801
+ func min_max (args py.Tuple , kwargs py.StringDict , name string ) (py.Object , error ) {
802
+ kwlist := []string {"key" , "default" }
803
+ positional := len (args )
804
+ var format string
805
+ var values py.Object
806
+ var cmp func (a py.Object , b py.Object ) (py.Object , error )
807
+ if name == "min" {
808
+ format = "|$OO:min"
809
+ cmp = py .Le
810
+ } else if name == "max" {
811
+ format = "|$OO:max"
812
+ cmp = py .Ge
813
+ }
814
+ var defaultValue py.Object
815
+ var keyFunc py.Object
816
+ var maxval py.Object
817
+
818
+ if positional > 1 {
819
+ values = args
820
+ } else {
821
+ err := py .UnpackTuple (args , nil , name , 1 , 1 , & values )
822
+ if err != nil {
823
+ return nil , err
824
+ }
825
+ }
826
+ err := py .ParseTupleAndKeywords (nil , kwargs , format , kwlist , & keyFunc , & defaultValue )
827
+ if err != nil {
828
+ return nil , err
829
+ }
830
+ if defaultValue != nil {
831
+ maxval = defaultValue
832
+ }
833
+
834
+ iter , err := py .Iter (values )
835
+ if err != nil {
836
+ return nil , err
837
+ }
838
+
839
+ for {
840
+ item , err := py .Next (iter )
841
+ if err != nil {
842
+ if py .IsException (py .StopIteration , err ) {
843
+ break
844
+ }
845
+ return nil , err
846
+ }
847
+ if maxval == nil {
848
+ //FIXME(@corona10): Should use keyfunc value
849
+ maxval = item
850
+ } else {
851
+ changed , err := cmp (item , maxval )
852
+ if err != nil {
853
+ return nil , err
854
+ }
855
+ if changed == py .True {
856
+ maxval = item
857
+ }
858
+ }
859
+
860
+ }
861
+ return maxval , nil
862
+ }
863
+
775
864
const chr_doc = `chr(i) -> Unicode character
776
865
777
866
Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.`
0 commit comments