-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from wuqinqiang/feat/strategy
feat: Add LeastRecentlyUsed option for selector
- Loading branch information
Showing
6 changed files
with
73 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package selector | ||
|
||
type Option func(srv *Srv) | ||
|
||
func WithWordNumber(wordNumer int) Option { | ||
return func(srv *Srv) { | ||
srv.wordNumber = wordNumer | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package strategy | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/wuqinqiang/helloword/dao/model" | ||
) | ||
|
||
type LeastRecentlyUsed struct{} | ||
|
||
func NewLeastRecentlyUsed() *LeastRecentlyUsed { | ||
return &LeastRecentlyUsed{} | ||
} | ||
|
||
func (l LeastRecentlyUsed) Select(words model.Words) model.Words { | ||
// sort the words by NumRepetitions field in ascending order | ||
sort.Slice(words, func(i, j int) bool { | ||
return words[i].NumRepetitions < words[j].NumRepetitions | ||
}) | ||
|
||
// sort the words by last_used field in ascending order | ||
sort.Slice(words, func(i, j int) bool { | ||
return words[i].LastUsed < words[j].LastUsed | ||
}) | ||
return words | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters