Skip to content
iFurySt edited this page Mar 18, 2024 · 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]
}

FindSliceDeltas

Find the added, removed, updated and unchanged items between s1 and s2.

package main

import (
	"fmt"

	"github.com/ifuryst/lol"
)

func main() {
	added, removed, updated, unchanged := lol.FindSliceDeltas([]int{1, 4, 7, 11}, []int{4, 1, 2}, func(t int) int {
		return t
	}, func(v1 int, v2 int) bool {
		return v1 == v2
	}, FindSliceDeltasOptions[int, int]{Compare: func(a, b int) int {
		return a - b
	}})
	fmt.Println(added)
	fmt.Println(removed)
	fmt.Println(updated)
	fmt.Println(unchanged)
	// Output:
	// [2]
	// [7 11]
	// []
	// [1 4]
}

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]
}

MapTo

Same as Map, but it can return another type of slice.

package main

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

func main() {
	type user struct {
		name string
		age  uint8
	}
	res := lol.MapTo([]user{
		{"Heisenberg", 35},
		{"Hank", 32},
		{"Saul", 33},
	}, func(u user) string { return u.name })
	fmt.Println(res)
	// Output:
	// [Heisenberg Hank Saul]
}

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
}

Include

Check if the slice includes the element.

package main

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

func main() {
	res1 := lol.Include([]int{1, 7, 3, 4}, 3)
	res2 := lol.Include([]string{"1", "7", "3", "4"}, "x")
	fmt.Println(res1)
	fmt.Println(res2)
	// Output:
	// true
	// false
}

Index & LastIndex

Get the index of the element in the slice. The Index traverses from left to right, and the LastIndex is reversed.

package main

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

func main() {
	res1 := lol.Index([]int{1, 7, 3, 4, 3}, 3)
	res2 := lol.LastIndex([]int{1, 7, 3, 4, 3}, 3)
	fmt.Println(res1)
	fmt.Println(res2)
	// Output:
	// 2
	// 4
}