-
Notifications
You must be signed in to change notification settings - Fork 93
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
func Find(ElementType) int #115
Comments
Hey there, You mean something like The way I read this it would be something like: package main
import (
"fmt"
"strings"
"github.com/elliotchance/pie/pie"
)
func main() {
nameIdx := pie.Strings{"Bob", "Sally", "John", "Jane"}.
Find(func (name string) bool {
return name == "John"
})
fmt.Println("John found on position: " + nameIdx) // "2"
} Or will it be something like I'm excited to help this pie to get bigger 😅, but I'll wait to hear back from you to submit a PR. |
You are right. They will take a value as the argument and return the respective index. When a callback is used instead of a value we append "Using", like: nameIdx := pie.Strings{"Bob", "Sally", "John", "Jane"}.
First("John") nameIdx := pie.Strings{"Bob", "Sally", "John", "Jane"}.
FirstUsing(func (name string) bool {
return name == "John"
}) I'm glad you are so excited to contribute! I'm looking forward to the PR. |
Oops, I should have read my own documentation. |
Ops, do you want to make both |
Hi everyone. I looked into implementation and I came up one question to myself. I might be wrong, but even if I'm wrong you also could find index by binary search and move to left for the first. |
@zhiburt: First, Index, IndexOf, etc in my experience means the lowest index for a match. The easiest way to do this is to check from 0th index and up. Binary search would only be meaningful for sorted slices as you say, which is rarely the case, and so it doesn’t make sense to add the IsSorted overhead. One other thing worth mentioning is that a binary search will also have to be aware of duplicate values to ensure that it always traverses to the lowest index on a match. It’s easiest and the most general case to just brute-force it. @danielpsf: I’m totally fine with either. |
|
@DylanMeeus, it makes sense, although Now that
I'm still working on |
I know using "Index" in the name is common in other languages but that's really not necessary here because I would rather be explicit about the direction. The prototypes are: func FindFirst(ElementType) int
func FindFirstUsing(fn(ElementType) bool) int
func FindLast(ElementType) int
func FindLastUsing(fn(ElementType) bool) int Furthermore, there could also be: func FindAll(ElementType) []int
func FindAllUsing(fn(ElementType) bool) []int In my opinion, it's easier and more understandable to be consistent with the first word. "Find" means we are talking about returning indexes. If we wanted to return another type, such as all elements that might match we use a different prefix, like |
Makes sense to me. :)
…On Sat, May 18, 2019, 4:03 AM Elliot Chance ***@***.***> wrote:
I know using "Index" in the name is common in other languages but that's
really not necessary here because I would rather be explicit about the
direction. The prototypes are:
func FindFirst(ElementType) int
func FindFirstUsing(fn(ElementType) bool) int
func FindLast(ElementType) int
func FindLastUsing(fn(ElementType) bool) int
Furthermore, there could also be:
func FindAll(ElementType) []int
func FindAllUsing(fn(ElementType) bool) []int
In my opinion, it's easier and more understandable to be consistent with
the first word. "Find" means we are talking about returning indexes. If we
wanted to return another type, such as all elements that might match we use
a different prefix, like Filter, Match, etc.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#115?email_source=notifications&email_token=AAZHOZ3KCIUBMKKJKT26U6DPV6S5RA5CNFSM4HLWIMCKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVWJDXI#issuecomment-493654493>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAZHOZ3OAFV5QJMSETHIV2LPV6S5RANCNFSM4HLWIMCA>
.
|
Find will return the index of the first element that matches, or -1 if none match.
The text was updated successfully, but these errors were encountered: