Skip to content
iFurySt edited this page Jul 28, 2023 · 9 revisions

Welcome to the 👾 lol wiki!

bool

NewTrue & NewFalse

Just for convenience, you create the pointer to a Boolean value on one line.

package main

import (
	"github.com/ifuryst/lol"
	"net"
)

type IP struct {
	Address string
	Status  *bool
}

func main() {
	s := IP{
		Address: net.ParseIP("8.8.8.8").String(),
		Status:  lol.NewFalse(),
	}
	// do sth
	_ = s
}

map

Keys

Get the all keys in the map struct.

package main

import (
	"fmt"
	"github.com/ifuryst/lol"
)

func main() {
	users := map[string]int{
		"Jack":       18,
		"Tom":        21,
		"Heisenberg": 48,
	}
	fmt.Println(lol.Keys(users))
	// Output:
	// [Jack Tom Heisenberg]
}

Values

Get the all values in the map struct.

package main

import (
	"fmt"
	"github.com/ifuryst/lol"
)

func main() {
	users := map[string]int{
		"Jack":       18,
		"Tom":        21,
		"Heisenberg": 48,
	}
	fmt.Println(lol.Values(users))
	// Output:
	// [18 21 48]
}

math

Abs

Get the absolute value of the given number.

package main

import (
	"fmt"
	"github.com/ifuryst/lol"
)

func main() {
	n := -3
	fmt.Println(lol.Abs(n))
	// Output:
	// 3
}

Max & Min

Get the maximum or minimum number from the given numbers.

package main

import (
	"fmt"
	"github.com/ifuryst/lol"
)

func main() {
	i, j := -3, 3
	fmt.Println(lol.Max(i, j))
	fmt.Println(lol.Min(i, j))
	// Output:
	// 3
	// -3
}

slice

SortSlice

Just an alias for slices.Sort in golang.org/x/exp/slices.

package main

import (
	"fmt"
	"github.com/ifuryst/lol"
)

func main() {
	s := lol.UniqSlice([]int{3, 1, 5}, []int{2, 5, 1})
	lol.SortSlice(s)
	fmt.Println(s)
	// Output:
	// [1 2 3 5]
}

MergeSlice

Merge the slices while keeping the elements not duplicated and not sorted according to the first time they are seen in the slice, and then return the merged slice.

package main

import (
	"fmt"
	"github.com/ifuryst/lol"
)

func main() {
	s := lol.MergeSlice([]int{3, 1, 5}, []int{2, 5, 1})
	fmt.Println(s)
	// Output:
	// [3 1 5 2 5 1]
}

UniqSlice & Union

Remove the duplications while keeping the elements not sorted according to the first time they are seen in the slice, and return the slice that contains all unique elements.

package main

import (
	"fmt"
	"github.com/ifuryst/lol"
)

func main() {
	s := lol.UniqSlice([]int{3, 1, 5}, []int{2, 5, 1})
	fmt.Println(s)
	// Output:
	// [3 1 5 2]
}

Intersection

Get the intersection sets of multiple elements.

package main

import (
	"fmt"
	"github.com/ifuryst/lol"
)

func main() {
	s := lol.Intersection([]int{3, 1, 5}, []int{2, 5, 1}, []int{5, 4, 2})
	fmt.Println(s)
	// Output:
	// [5]
}

Difference

Get the difference sets between a to b.

package main

import (
	"fmt"
	"github.com/ifuryst/lol"
)

func main() {
	s := lol.Difference([]int{3, 1, 5}, []int{2, 5, 1})
	fmt.Println(s)
	// Output:
	// [3]
}

Map

Apply the function to all the elements in the slice.

package main

import (
	"fmt"
	"github.com/ifuryst/lol"
)

func main() {
	s := lol.Map([]int{3, 1, 5}, func(v int) int {
		return v * v
	})
	fmt.Println(s)
	// Output:
	// [9 1 25]
}

Reduce & ReduceRight

Accumulate and combine elements through a function into a single value. You can specify an initial value. ReduceRight is like Reduce but the order is from right(high index) to left(low index)

package main

import (
	"fmt"
	"github.com/ifuryst/lol"
)

func main() {
	s := lol.Reduce([]int{3, 1, 5}, func(c, v, i int) int {
		return c + v*i
	}, 0)
	fmt.Println(s)
	// Output:
	// 11
}
Clone this wiki locally