@@ -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,121 @@ 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 , maxItem py.Object
817
+ var kf * py.Function
818
+
819
+ if positional > 1 {
820
+ values = args
821
+ } else {
822
+ err := py .UnpackTuple (args , nil , name , 1 , 1 , & values )
823
+ if err != nil {
824
+ return nil , err
825
+ }
826
+ }
827
+ err := py .ParseTupleAndKeywords (nil , kwargs , format , kwlist , & keyFunc , & defaultValue )
828
+ if err != nil {
829
+ return nil , err
830
+ }
831
+ if keyFunc == py .None {
832
+ keyFunc = nil
833
+ }
834
+ if keyFunc != nil {
835
+ var ok bool
836
+ kf , ok = keyFunc .(* py.Function )
837
+ if ! ok {
838
+ return nil , py .ExceptionNewf (py .TypeError , "'%s' object is not callable" , keyFunc .Type ())
839
+ }
840
+ }
841
+ if defaultValue != nil {
842
+ maxItem = defaultValue
843
+ if keyFunc != nil {
844
+ maxVal , _ = py .Call (kf , py.Tuple {defaultValue }, nil )
845
+ } else {
846
+ maxVal = defaultValue
847
+ }
848
+ }
849
+ iter , err := py .Iter (values )
850
+ if err != nil {
851
+ return nil , err
852
+ }
853
+
854
+ for {
855
+ item , err := py .Next (iter )
856
+ if err != nil {
857
+ if py .IsException (py .StopIteration , err ) {
858
+ break
859
+ }
860
+ return nil , err
861
+ }
862
+ if maxVal == nil {
863
+ if keyFunc != nil {
864
+ maxVal , _ = py .Call (kf , py.Tuple {item }, nil )
865
+ } else {
866
+ maxVal = item
867
+ }
868
+ maxItem = item
869
+ } else {
870
+ var compareVal py.Object
871
+ if keyFunc != nil {
872
+ compareVal , _ = py .Call (kf , py.Tuple {item }, nil )
873
+ } else {
874
+ compareVal = item
875
+ }
876
+ changed , err := cmp (compareVal , maxVal )
877
+ if err != nil {
878
+ return nil , err
879
+ }
880
+ if changed == py .True {
881
+ maxVal = compareVal
882
+ maxItem = item
883
+ }
884
+ }
885
+
886
+ }
887
+ return maxItem , nil
888
+ }
889
+
775
890
const chr_doc = `chr(i) -> Unicode character
776
891
777
892
Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.`
0 commit comments