From 7dabb31483ee570faefefaf51bf582bbbb882f7b Mon Sep 17 00:00:00 2001 From: badkaktus Date: Tue, 8 Oct 2019 22:06:57 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=BE=D1=80=D1=82=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=BA=D0=B0=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20=D1=84?= =?UTF-8?q?=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples.txt | 2 +- .../sorting-by-functions.go | 41 ++++++++++--------- .../sorting-by-functions.sh | 14 +++---- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/examples.txt b/examples.txt index 4338bf1..ed785c4 100644 --- a/examples.txt +++ b/examples.txt @@ -38,7 +38,7 @@ WaitGroups Мьютексы (Mutexes) Управление состоянием горутин (Stateful Goroutines) Сортировка (Sorting) -Sorting by Functions +Сортировка через функции (Sorting by Functions) Panic Defer Collection Functions diff --git a/examples/sorting-by-functions/sorting-by-functions.go b/examples/sorting-by-functions/sorting-by-functions.go index 8524f66..e52dc97 100644 --- a/examples/sorting-by-functions/sorting-by-functions.go +++ b/examples/sorting-by-functions/sorting-by-functions.go @@ -1,8 +1,8 @@ -// Sometimes we'll want to sort a collection by something -// other than its natural order. For example, suppose we -// wanted to sort strings by their length instead of -// alphabetically. Here's an example of custom sorts -// in Go. +// Иногда мы хотим отсортировать коллекцию по какому-то +// другому признаку, кроме ее естественного порядка. +// Например, предположим, что мы хотели бы отсортировать +// строки по длине, а не по алфавиту. Вот пример +// пользовательских сортировок в Go. package main @@ -11,19 +11,20 @@ import ( "sort" ) -// In order to sort by a custom function in Go, we need a -// corresponding type. Here we've created a `byLength` -// type that is just an alias for the builtin `[]string` -// type. +// Для сортировки по пользовательской функции в Go нам +// нужен соответствующий тип. Здесь мы создали тип +// `byLength`, который является просто псевдонимом для +// `[]string`. type byLength []string -// We implement `sort.Interface` - `Len`, `Less`, and -// `Swap` - on our type so we can use the `sort` package's -// generic `Sort` function. `Len` and `Swap` -// will usually be similar across types and `Less` will -// hold the actual custom sorting logic. In our case we -// want to sort in order of increasing string length, so -// we use `len(s[i])` and `len(s[j])` here. +// Мы реализуем `sort.Interface` - Len`, `Less` и `Swap` +// - для нашего типа, чтобы мы могли использовать общую +// функцию `Sort` пакета `sort`. `Len` и `Swap` обычно +// одинаковы для разных типов, а `Less` будет содержать +// реальную пользовательскую логику сортировки. В нашем +// случае мы хотим отсортировать в порядке увеличения +// длины строки, поэтому мы используем `len(s[i])` и +// `len(s[j])` здесь. func (s byLength) Len() int { return len(s) } @@ -34,10 +35,10 @@ func (s byLength) Less(i, j int) bool { return len(s[i]) < len(s[j]) } -// With all of this in place, we can now implement our -// custom sort by converting the original `fruits` slice -// to `byLength`, and then use `sort.Sort` on that typed -// slice. +// Реализовав интерфейс, мы можем теперь реализовать +// нашу собственную сортировку, преобразовав исходный +// срез `fruits` в `byLength`, а затем использовать +// `sort.Sort` для этого типизированного среза. func main() { fruits := []string{"peach", "banana", "kiwi"} sort.Sort(byLength(fruits)) diff --git a/examples/sorting-by-functions/sorting-by-functions.sh b/examples/sorting-by-functions/sorting-by-functions.sh index d2a1c4c..264a4dd 100644 --- a/examples/sorting-by-functions/sorting-by-functions.sh +++ b/examples/sorting-by-functions/sorting-by-functions.sh @@ -1,10 +1,10 @@ -# Running our program shows a list sorted by string -# length, as desired. +# При запуске нашей программы отображается список, +# отсортированный по длине строки, как мы и хотели. $ go run sorting-by-functions.go [kiwi peach banana] -# By following this same pattern of creating a custom -# type, implementing the three `Interface` methods on that -# type, and then calling sort.Sort on a collection of that -# custom type, we can sort Go slices by arbitrary -# functions. +# Следуя той же схеме создания пользовательского типа, +# реализации трех методов `интерфейса` для этого типа +# и последующего вызова `sort.Sort` для коллекции +# этого типа, мы можем сортировать срезы Go +# по произвольным функциям.