Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
enetx committed Mar 29, 2024
1 parent 3c62ed0 commit fdaf08e
Show file tree
Hide file tree
Showing 18 changed files with 315 additions and 293 deletions.
14 changes: 7 additions & 7 deletions bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (bs Bytes) Split(sep ...Bytes) Slice[Bytes] {
func sliceBytesFromStd(bb [][]byte) Slice[Bytes] {
result := NewSlice[Bytes](0, len(bb))
for _, v := range bb {
result = result.Append(NewBytes(v))
result = append(result, v)
}

return result
Expand Down Expand Up @@ -136,7 +136,7 @@ func (bs Bytes) ContainsRune(r rune) bool { return bytes.ContainsRune(bs, r) }
func (bs Bytes) Count(obs Bytes) int { return bytes.Count(bs, obs) }

// Empty checks if the Bytes is empty.
func (bs Bytes) Empty() bool { return bs == nil || bs.Len() == 0 }
func (bs Bytes) Empty() bool { return bs == nil || len(bs) == 0 }

// Eq checks if the Bytes is equal to another Bytes.
func (bs Bytes) Eq(obs Bytes) bool { return bs.Compare(obs).Eq(0) }
Expand All @@ -156,10 +156,10 @@ func (bs Bytes) Index(obs Bytes) int { return bytes.Index(bs, obs) }
// IndexRegexp searches for the first occurrence of the regular expression pattern in the Bytes.
// If a match is found, it returns an Option containing an Slice with the start and end indices of the match.
// If no match is found, it returns None.
func (bs Bytes) IndexRegexp(pattern *regexp.Regexp) Option[Slice[Int]] {
result := SliceMap(pattern.FindIndex(bs), NewInt)
func (bs Bytes) IndexRegexp(pattern *regexp.Regexp) Option[Slice[int]] {
result := SliceOf(pattern.FindIndex(bs)...)
if result.Empty() {
return None[Slice[Int]]()
return None[Slice[int]]()
}

return Some(result)
Expand Down Expand Up @@ -219,7 +219,7 @@ func (bs Bytes) FindAllSubmatchRegexpN(pattern *regexp.Regexp, n Int) Option[Sli
var result Slice[Slice[Bytes]]

for _, v := range pattern.FindAllSubmatch(bs, n.Std()) {
result = result.Append(sliceBytesFromStd(v))
result = append(result, sliceBytesFromStd(v))
}

if result.Empty() {
Expand Down Expand Up @@ -263,7 +263,7 @@ func (bs Bytes) NormalizeNFC() Bytes { return norm.NFC.Bytes(bs) }
func (bs Bytes) Ne(obs Bytes) bool { return !bs.Eq(obs) }

// NotEmpty checks if the Bytes is not empty.
func (bs Bytes) NotEmpty() bool { return bs.Len() != 0 }
func (bs Bytes) NotEmpty() bool { return !bs.Empty() }

// Reader returns a *bytes.Reader initialized with the content of Bytes.
func (bs Bytes) Reader() *bytes.Reader { return bytes.NewReader(bs) }
Expand Down
2 changes: 0 additions & 2 deletions channel.go

This file was deleted.

11 changes: 7 additions & 4 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package g

import "fmt"

type (
ErrFileNotExist struct{ Msg string }
ErrFileClosed struct{ Msg string }
)
// ErrFileNotExist represents an error for when a file does not exist.
type ErrFileNotExist struct{ Msg string }

// Error returns the error message for ErrFileNotExist.
func (e *ErrFileNotExist) Error() string {
return fmt.Sprintf("no such file: %s", e.Msg)
}

// ErrFileClosed represents an error for when a file is already closed.
type ErrFileClosed struct{ Msg string }

// Error returns the error message for ErrFileClosed.
func (e *ErrFileClosed) Error() string {
return fmt.Sprintf("%s: file is already closed and unlocked", e.Msg)
}
4 changes: 2 additions & 2 deletions examples/floats.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ func main() {
g.NewFloat(1.3339).Print()
g.NewFloat(13339).Print()

fmt.Println(g.NewFloat(20).Eq(g.NewFloat(float32(20.0))))
fmt.Println(g.NewFloat(float64(20)).Eq(g.NewFloat(float32(20.0))))
fmt.Println(g.NewFloat(20).Eq(g.NewFloat(20.0)))
fmt.Println(g.NewFloat(20).Eq(g.NewFloat(20.0)))
}
42 changes: 22 additions & 20 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func NewFile(name String) *File { return &File{name: name} }
// // UPPERCASED_LINE6
func (f *File) Lines() Result[seqSlice[String]] {
if f.file == nil {
if err := f.Open().Err(); err != nil {
return Err[seqSlice[String]](err)
if r := f.Open(); r.IsErr() {
return Err[seqSlice[String]](r.Err())
}
}

Expand Down Expand Up @@ -83,8 +83,8 @@ func (f *File) Lines() Result[seqSlice[String]] {
// // UPPERCASED_CHUNK3
func (f *File) Chunks(size int) Result[seqSlice[String]] {
if f.file == nil {
if err := f.Open().Err(); err != nil {
return Err[seqSlice[String]](err)
if r := f.Open(); r.IsErr() {
return Err[seqSlice[String]](r.Err())
}
}

Expand Down Expand Up @@ -115,17 +115,17 @@ func (f *File) Chunks(size int) Result[seqSlice[String]] {
// Don't forget to close the file!
func (f *File) Append(content String, mode ...os.FileMode) Result[*File] {
if f.file == nil {
if mda := f.createAll(); mda.IsErr() {
return mda
if r := f.createAll(); r.IsErr() {
return r
}

fmode := FileDefault
if len(mode) != 0 {
fmode = mode[0]
}

if err := f.OpenFile(os.O_APPEND|os.O_CREATE|os.O_WRONLY, fmode).Err(); err != nil {
return Err[*File](err)
if r := f.OpenFile(os.O_APPEND|os.O_CREATE|os.O_WRONLY, fmode); r.IsErr() {
return r
}
}

Expand Down Expand Up @@ -197,8 +197,8 @@ func (f *File) Chown(uid, gid int) Result[*File] {
// Don't forget to close the file!
func (f *File) Seek(offset int64, whence int) Result[*File] {
if f.file == nil {
if err := f.Open().Err(); err != nil {
return Err[*File](err)
if r := f.Open(); r.IsErr() {
return r
}
}

Expand Down Expand Up @@ -234,9 +234,10 @@ func (f *File) Close() error {
// Copy copies the file to the specified destination, with the specified mode (optional).
// If no mode is provided, the default FileMode (0644) is used.
func (f *File) Copy(dest String, mode ...os.FileMode) Result[*File] {
if err := f.Open().Err(); err != nil {
return Err[*File](err)
if r := f.Open(); r.IsErr() {
return r
}

defer f.Close()

return NewFile(dest).WriteFromReader(f.file, mode...)
Expand Down Expand Up @@ -283,9 +284,10 @@ func (f *File) Guard() *File {

// MimeType returns the MIME type of the file as an String.
func (f *File) MimeType() Result[String] {
if err := f.Open().Err(); err != nil {
return Err[String](err)
if r := f.Open(); r.IsErr() {
return Err[String](r.Err())
}

defer f.Close()

const bufferSize = 512
Expand Down Expand Up @@ -370,8 +372,8 @@ func (f *File) Print() *File { fmt.Println(f); return f }

// Read opens the named file with a read-lock and returns its contents.
func (f *File) Read() Result[String] {
if err := f.Open().Err(); err != nil {
return Err[String](err)
if r := f.Open(); r.IsErr() {
return Err[String](r.Err())
}

defer f.Close()
Expand Down Expand Up @@ -509,8 +511,8 @@ func (f *File) Write(content String, mode ...os.FileMode) Result[*File] {
// If no FileMode is provided, the default FileMode (0644) is used.
func (f *File) WriteFromReader(scr io.Reader, mode ...os.FileMode) Result[*File] {
if f.file == nil {
if mda := f.createAll(); mda.IsErr() {
return mda
if r := f.createAll(); r.IsErr() {
return r
}
}

Expand All @@ -526,8 +528,8 @@ func (f *File) WriteFromReader(scr io.Reader, mode ...os.FileMode) Result[*File]

f = NewFile(filePath.Ok())

if err := f.OpenFile(os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fmode).Err(); err != nil {
return Err[*File](err)
if r := f.OpenFile(os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fmode); r.IsErr() {
return Err[*File](r.Err())
}

defer f.Close()
Expand Down
28 changes: 25 additions & 3 deletions float.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,14 @@ func (f Float) Mul(b Float) Float { return f * b }
func (f Float) Ne(b Float) bool { return !f.Eq(b) }

// Round rounds the Float to the nearest integer and returns the result as an Int.
func (f Float) Round() Int { return Int(math.Round(f.Std())) }
// func (f Float) Round() Int { return Int(math.Round(f.Std())) }
func (f Float) Round() Int {
if f >= 0 {
return Int(f + 0.5)
}

return Int(f - 0.5)
}

// RoundDecimal rounds the Float value to the specified number of decimal places.
//
Expand All @@ -94,8 +101,23 @@ func (f Float) Round() Int { return Int(math.Round(f.Std())) }
// f := g.Float(3.14159)
// rounded := f.RoundDecimal(2) // rounded will be 3.14
func (f Float) RoundDecimal(precision int) Float {
mult := Float(math.Pow(10, float64(precision)))
return f.Mul(mult).Round().ToFloat().Div(mult)
if precision < 0 {
return f
}

mult := 1
for i := 0; i < precision; i++ {
mult *= 10
}

result := f * Float(mult)
if result >= 0 {
result += 0.5
} else {
result -= 0.5
}

return Float(int(result)) / Float(mult)
}

// Sub subtracts two Floats and returns the result.
Expand Down
2 changes: 1 addition & 1 deletion int.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (i Int) Min(b ...Int) Int { return minmax.Min(i, b...) }
func (i Int) Max(b ...Int) Int { return minmax.Max(i, b...) }

// RandomRange returns a random Int in the range [from, to].
func (i Int) RandomRange(to Int) Int { return rand.N(to.Sub(i).Add(1)).Add(i) }
func (i Int) RandomRange(to Int) Int { return rand.N(to-i+1) + i }

// Bytes returns the Int as a byte slice.
func (i Int) Bytes() Bytes {
Expand Down
14 changes: 9 additions & 5 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func (m Map[K, V]) Iter() seqMap[K, V] { return liftMap(m) }
// types are guaranteed to be comparable.
func (m Map[K, V]) Invert() Map[any, K] {
result := NewMap[any, K](m.Len())
m.Iter().ForEach(func(k K, v V) { result.Set(v, k) })
for k, v := range m {
result.Set(v, k)
}

return result
}
Expand Down Expand Up @@ -86,7 +88,7 @@ func (m Map[K, V]) Std() map[K]V { return m }

// Eq checks if two Maps are equal.
func (m Map[K, V]) Eq(other Map[K, V]) bool {
if m.Len() != other.Len() {
if len(m) != len(other) {
return false
}

Expand All @@ -103,7 +105,9 @@ func (m Map[K, V]) Eq(other Map[K, V]) bool {
func (m Map[K, V]) String() string {
var builder strings.Builder

m.Iter().ForEach(func(k K, v V) { builder.WriteString(fmt.Sprintf("%v:%v, ", k, v)) })
for k, v := range m {
builder.WriteString(fmt.Sprintf("%v:%v, ", k, v))
}

return String(builder.String()).TrimRight(", ").Format("Map{%s}").Std()
}
Expand Down Expand Up @@ -147,7 +151,7 @@ func (m Map[K, V]) GetOrSet(key K, defaultValue V) V {
return value
}

m.Set(key, defaultValue)
m[key] = defaultValue

return defaultValue
}
Expand All @@ -156,7 +160,7 @@ func (m Map[K, V]) GetOrSet(key K, defaultValue V) V {
func (m Map[K, V]) Clear() Map[K, V] { clear(m); return m }

// Empty checks if the Map is empty.
func (m Map[K, V]) Empty() bool { return m.Len() == 0 }
func (m Map[K, V]) Empty() bool { return len(m) == 0 }

// Get retrieves the value associated with the given key.
func (m Map[K, V]) Get(k K) Option[V] {
Expand Down
15 changes: 9 additions & 6 deletions map_ordered.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ func (mo MapOrd[K, V]) Iter() seqMapOrd[K, V] { return liftMO(mo) }
// Converts the standard Map 'hmap' to an ordered Map.
func MapOrdFromMap[K comparable, V any](m Map[K, V]) MapOrd[K, V] {
mo := NewMapOrd[K, V](m.Len())
m.Iter().ForEach(func(k K, v V) { mo.Set(k, v) })

for k, v := range m {
mo.Set(k, v)
}

return mo
}
Expand Down Expand Up @@ -134,7 +137,7 @@ func (mo MapOrd[K, V]) SortBy(fn func(a, b Pair[K, V]) bool) MapOrd[K, V] {

// Clone creates a new ordered Map with the same key-value pairs.
func (mo MapOrd[K, V]) Clone() MapOrd[K, V] {
result := NewMapOrd[K, V](mo.Len())
result := NewMapOrd[K, V](len(mo))
mo.Iter().ForEach(func(k K, v V) { result.Set(k, v) })

return result
Expand All @@ -148,7 +151,7 @@ func (mo *MapOrd[K, V]) Copy(src MapOrd[K, V]) MapOrd[K, V] {

// ToMap converts the ordered Map to a standard Map.
// func (mo MapOrd[K, V]) ToMap() Map[K, V] {
// m := NewMap[K, V](mo.Len())
// m := NewMap[K, V](len(mo))
// mo.Iter().ForEach(func(k K, v V) { m.Set(k, v) })

// return m
Expand Down Expand Up @@ -232,7 +235,7 @@ func (mo *MapOrd[K, V]) GetOrSet(key K, defaultValue V) V {
// Invert inverts the key-value pairs in the ordered Map, creating a new ordered Map with the
// values as keys and the original keys as values.
func (mo MapOrd[K, V]) Invert() MapOrd[V, K] {
result := NewMapOrd[V, K](mo.Len())
result := NewMapOrd[V, K](len(mo))
mo.Iter().ForEach(func(k K, v V) { result.Set(v, k) })

return result
Expand Down Expand Up @@ -267,7 +270,7 @@ func (mo *MapOrd[K, V]) Delete(keys ...K) MapOrd[K, V] {

// Eq compares the current ordered Map to another ordered Map and returns true if they are equal.
func (mo MapOrd[K, V]) Eq(other MapOrd[K, V]) bool {
if mo.Len() != other.Len() {
if len(mo) != len(other) {
return false
}

Expand Down Expand Up @@ -297,7 +300,7 @@ func (mo *MapOrd[K, V]) Clear() MapOrd[K, V] { return mo.Delete(mo.Keys()...) }
func (mo MapOrd[K, V]) Contains(key K) bool { return mo.index(key) >= 0 }

// Empty checks if the ordered Map is empty.
func (mo MapOrd[K, V]) Empty() bool { return mo.Len() == 0 }
func (mo MapOrd[K, V]) Empty() bool { return len(mo) == 0 }

// Len returns the number of key-value pairs in the ordered Map.
func (mo MapOrd[K, V]) Len() int { return len(mo) }
Expand Down

0 comments on commit fdaf08e

Please sign in to comment.