This may not bother people very much, but I just noticed that there's
no package that exports Min and Max functions. Instead there are
several implementations in various places in the library, all private:
src/pkg/compress/flate/util.go:21
src/pkg/rand/rand_test.go:25
src/pkg/strconv/ftoa.go:403
src/pkg/testing/benchmark.go:81
If someone can suggest a good package to put them in, I'd be happy to
refactor. Oh, just noticed: The math package already has Fmin and Fmax
for float64. Add a few more versions there maybe?
Cheers,
Peter
--
Peter H. Froehlich <http://www.cs.jhu.edu/~phf/>
Senior Lecturer | Director, Johns Hopkins Gaming Lab
There are a lot of possible variations: min and max, each of the
number types e.g. int32, int, int64, float32, etc., and different
parameter lists. For example,
// Minimum of a pair of int arguments
func MinInt(v1, v2 int) int {
if v1 <= v2 {
return v1
}
return v2
}
// Minimum of a slice of int arguments
func MinIntS(v []int) (m int) {
if len(v) > 0 {
m = v[0]
}
for i := 1; i < len(v); i++ {
if v[i] < m {
m = v[i]
}
}
return
}
// Minimum of a variable number of int arguments
func MinIntV(v1 int, vn ...int) (m int) {
m = v1
for i := 0; i < len(vn); i++ {
if vn[i] < m {
m = vn[i]
}
}
return
}
Peter
On Apr 3, 5:06 pm, Peter Froehlich <peter.hans.froehl...@gmail.com>
wrote:
True, true. I guess that suggests generating them from a template
instead, maybe in the style that the vector package is done. On the
other hand, apparently so far nothing besides int and float64 is
needed... :-D
> // Minimum of a slice of int arguments
> func MinIntS(v []int) (m int) {
> if len(v) > 0 {
> m = v[0]
> }
> for i := 1; i < len(v); i++ {
> if v[i] < m {
> m = v[i]
> }
> }
> return
> }
Personally, I'd make that thing panic() if the slice is empty. But
that's just me, I like asserts too after all, and I am no fan of the
new "default value" rule for maps either. :-D
> // Minimum of a variable number of int arguments
> func MinIntV(v1 int, vn ...int) (m int) {
> m = v1
> for i := 0; i < len(vn); i++ {
> if vn[i] < m {
> m = vn[i]
> }
> }
> return
> }
Thanks for that one, I must have missed the version of Go that
introduced types for ... parameters. I'll see how the vector stuff is
done and then I'll check how hard this would be to add to the math
package.
how about adding to the reflect package something like:
func Compare(a, b *reflect.Value) int {...}
which would return -1, 0, or 1 depending on a < b, a == b or a > b
respectively?
this could cater for the basic numeric and string types. if in addition
the implementation of this function applies some intelligent conversion
rules it can also be used to compare unsigned and signed numeric values.
finally, it can also cater for other types if for example a and b
implement the same type which in addition implements a Comparable
interface --to borrow from the Java library:
type Comparable interface {
Compare(that *reflect.Value) int
}
cheers;
rsn
--
To unsubscribe, reply using "remove me" as the subject.
>I might be odd, but I don't like the idea of having to import "math" to get
>min and max; I would like to see min() and max() become built-in functions
>like cap() or len().
That doesn't seem like an odd desire; min and max are certainly used in a
wider variety of applications than, say, sine and cosine functions are.
>In my ideal world, min() and max() would take two numeric parameters of any
>type (but they'd have to be the same type) and return the smaller/larger
>value (which would be the same type as the parameters).
Why just two parameters? max(a,b,c) has an obvious meaning.
--
Norman Yarvin http://yarchive.net
Why just two parameters? max(a,b,c) has an obvious meaning.
If you were going to make min and max built-in, with all that implies --
a special piece of code for it in the compiler, a special section in the
language definition, and the programmer having to remember that it is
special -- you might as well make them have every meaning they can
reasonably bear. But when arguing for that, the question is: what makes
min and max so special and unique that they deserve a place in the
compiler and in the language definition, rather than in the standard
libraries? Having them take generic types is a feature that would be
useful for a lot of other things, too. Are min and max used so much more
often than those other things?
Peter,
Go will probally inline those at some point, but I suspect not if they're in a separate package.
Ryanne
- from my phone -
On Apr 4, 10:33 am, Ryanne Dolan <ryannedo...@gmail.com> wrote:
> Peter,
>
> Go will probally inline those at some point, but I suspect not if they're in
> a separate package.
>
> Ryanne
> - from my phone -
>
> On Apr 3, 2010 4:07 PM, "Peter Froehlich" <peter.hans.froehl...@gmail.com>
Here's the set of Min() functions for ints, rewritten, with a uniform
structure, to illustrate a pattern. The pattern encapsulates the min
algorithm in the fundamental MinInt() function and expression LTInt().
The MinIntS() and MinIntV() functions then become wrapper functions,
merely reflecting the data structure of their parameters. The same
pattern applies for both min, i.e. LT(), and max, i.e. GT(), and for
all numeric types for which LT() and GT() are valid.
This pattern could be used as the basis for deriving a numeric type
agnostic code generation template, perhaps using interface{}, like the
Go vector package.
// Less Than for a pair of int arguments
func LTInt(v2, v1 int) bool {
return v2 < v1
}
// Minimum of a pair of int arguments
func MinInt(v1, v2 int) (m int) {
m = v1
if LTInt(v2, v1) {
m = v2
}
return
}
// Minimum of a slice of int arguments
func MinIntS(v []int) (m int) {
if len(v) > 0 {
m = v[0]
}
for i := 1; i < len(v); i++ {
m = MinInt(m, v[i])
}
return
}
// Minimum of a variable number of int arguments
func MinIntV(v1 int, vn ...int) (m int) {
m = v1
if len(vn) > 0 {
m = MinInt(m, MinIntS(vn))
}
return
}
Peter
On Apr 3, 9:55 pm, Peter Froehlich <peter.hans.froehl...@gmail.com>
wrote:
What do you propose that Max(a, b complex) should return? Not all
numeric types may be compared using the full range of comparison
operators.
If you were going to make min and max built-in, with all that implies --
a special piece of code for it in the compiler, a special section in the
language definition, and the programmer having to remember that it is
special -- you might as well make them have every meaning they can
reasonably bear. But when arguing for that, the question is: what makes
min and max so special and unique that they deserve a place in the
compiler and in the language definition, rather than in the standard
libraries? Having them take generic types is a feature that would be
useful for a lot of other things, too. Are min and max used so much more
often than those other things?
We do have complex numbers. The complex numbers are unordered.
Complex numbers have a magnitude (also known as the absolute value or
modulus) which could be used to satisfy the requirements of min/max
functions.
Peter
The only logical comparison of complex numbers would be by their
moduli, in the general case. However, this could be done by taking the
modulus of each argument, just as you would if you were doing other
specific comparisons. Unfortunately, while real and imag are built in,
there's no mod or arg. You'd have to do it yourself/use a library
function.
It's much better to leave it undefined. That conforms to the normal
mathematical usages, which have it undefined. When people convert a
routine from real to complex arithmetic, it helps if they get an error
message for any calls to min or max: it lets them know they have to
replace those calls. Exactly what to replace them with would take some
decision-making: it might be the maximum of the real part of the number,
the maximum absolute value, or something else.
If modulus were used for the comparison, then:
min(-2, 1) = -2
min(-2+0i, 1+0i) = 1
I've not yet found the lack of a common 'max'/'min' to be an issue.Most of the time, I just do:if x < 4 {x = 4}Or the like.It's absurdly simple, convenient, and with the current absence of inlining, more efficient than a function.
I am missing templates the most (though generics as in Java would suffice given the cool interface duck typing lark), followed soon after by things like a lack of function overloading with different arguments (or any form of default arguments), and lack of ternary operator.
Isn't the door for generics still open if they can find a way to implement them that isn't atrocious?
As for the other three points.- Function overloading ... 1 function name, 27 implementations... left out for a reason.
- Default arguments can be emulated in various ways if you really want them, but isn't being explicit nice?
- Ternary operator ... if ternary operator comes up in a "top 4 gripes" the language is doing incredibly well -- also, they are both pointless and evil to my mind.
Right, so the ideal situation would be to use interfaces.If we could make 'func max(interface{<}, interface{<}) bool' and have it work on basic types. How awesome would it be?
On Monday, April 5, 2010 7:57:02 AM UTC+2, Daniel Smith wrote:Oh wow, you're right-- whoops, somehow I missed those. Sweet! And now that I think about it more, I wouldn't expect max/min to accept complex numbers, or any type that can't be compared with <.
On Mon, Apr 5, 2010 at 12:28 AM, jimmy frasche <soapbo...@gmail.com> wrote:On Sun, Apr 4, 2010 at 10:24 PM, Daniel Smith <luken...@gmail.com> wrote:We do have complex numbers. The complex numbers are unordered.
> I don't know off the top of my head what max/min mean on complex numbers,
> but it seems like that's an issue to be resolved when/if we get a complex
> number type.
--
Daniel Smith
http://www.schaumburggoclub.org/
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
Robert Melton
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
I completely understand that we will not see generics in Go until at least Go 2. However, can anyone help me understand a high-level overview of some of the nuances that would make implementation of the CLR generic approach difficult in Go?
--
What about option #3, generic template for the general case, but JIT compiler makes specialized copies for types that are used often?
Also, what do you mean by Java-esque memory problems?
Kevin,Thank you for the very detailed response. It was very helpful. I have one additional question, how were the problems you discussed resolved with the implementation of slices and maps?
--
last time I checked, I thought it simply generated the code for a "copy" or "append" inline, based on the size of the type.On Mon, Aug 26, 2013 at 4:34 PM, Luke Mauldin <lukem...@gmail.com> wrote:So the Go compiler examines all of the slices in the program and generates a small amount of custom code for each unique slice type?