Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add utilities for applying options and copying protobufs #809

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 8 additions & 29 deletions utils/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,16 @@

package utils

import (
"time"
import "math"

"golang.org/x/exp/constraints"
)

type Numeric interface {
constraints.Signed | constraints.Unsigned | time.Duration
}

func Max[T Numeric](vs ...T) T {
return Least(func(a, b T) bool { return a > b }, vs...)
}

func Min[T Numeric](vs ...T) T {
return Least(func(a, b T) bool { return a < b }, vs...)
}

func Most[T Numeric](less func(a, b T) bool, vs ...T) T {
return Least(func(a, b T) bool { return !less(a, b) }, vs...)
func LogisticFunc(x0, L, k float64) func(x float64) float64 {
return func(x float64) float64 {
return L / (1 + math.Pow(math.E, -k*(x-x0)))
}
}

func Least[T Numeric](less func(a, b T) bool, vs ...T) T {
if len(vs) == 0 {
return 0
}
v := vs[0]
for i := 1; i < len(vs); i++ {
if less(vs[i], v) {
v = vs[i]
}
func FastLogisticFunc(x0, L, k float64) func(x float64) float64 {
return func(x float64) float64 {
return L / 2 * (1 + k*(x-x0)/(1+math.Abs(k*(x-x0))))
}
return v
}
22 changes: 22 additions & 0 deletions utils/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package options

func Apply[T any, F ~func(T)](o T, opts []F) T {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry Paul, I already asked you this once, but I forgot the answer :-(. What does the ~ mean there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

~ means the type constraint permits any type with the same underlying type. so for example

type Foo func()

var f0 = func() {}
var f1 Foo = func() {}

func a[T ~func()](f T) {}
func b[T func()](f T)  {}

a(f0) // ok
b(f0) // ok
a(f1) // ok
b(f1) // not ok

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, got it. Thank you Paul. Let me commit this to my poor memory and hope it survives till the next time I see this pattern :-)

Copy link
Contributor Author

@paulwe paulwe Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is needed here because we have functional options like

type FooOption func(x *FooOptions)

// several implementations of FooOption

func SomeFoo(opts ...FooOption) {
    _ := options.Apply(&FooOptions{}, opts)
    // without ~ this wouldn't work because []FooOption is not []func(*FooOptions)
}

for _, opt := range opts {
opt(o)
}
return o
}
21 changes: 21 additions & 0 deletions utils/proto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package utils

import "google.golang.org/protobuf/proto"

func CloneProto[T proto.Message](m T) T {
return proto.Clone(m).(T)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!!!

}
Loading