Categorygithub.com/laijunbin/go-solve-kit
modulepackage
0.2.1
Repository: https://github.com/laijunbin/go-solve-kit.git
Documentation: pkg.go.dev

# README

Go-Solve-Kit

Designed specifically for programming problem solving.

It's like VB LINQ, JavaScript array Function ... more.

Install

$ go get github.com/laijunbin/go-solve-kit

Import

import (
	sk "github.com/laijunbin/go-solve-kit"
)

Example


Example 1: two number sum.

func main() {
	sum := sk.LineInput().Split(" ")[:2].ToIntArray().Sum()
	fmt.Println(sum)
}

Example 2:

func main() {
	nums := []int {1, 2, 3, 4, 5}
	output := sk.FromIntArray(nums).Map(func(v sk.Int, _ int) interface{} {
		return v * v
	}).Filter(func(v sk.Type, _ int) bool {
		return v.ToInt() % 2 == 0
	}).ToStringArray().Join("\n")
	fmt.Println(output)
}

Output:

4
16

Example 3:

func main() {
	array2D := sk.NewArray(5).Map(func(_ sk.Type, i int) interface{} {
		return sk.NewRange(1, 5, 2).Map(func(v sk.Int, j int) interface{} {
			return v.ValueOf() * i
		})
	})
	fmt.Println(array2D)
	fmt.Println(array2D.Get(2, 2).ToInt())
	fmt.Println(array2D.Get(4, 1).ToInt())
	fmt.Println("-------------------")
	fmt.Println(array2D)
	fmt.Println(array2D.Length())
	fmt.Println(array2D.Flatten())
	fmt.Println(array2D.Flatten().Length())
}

Output:

[{[{0} {0} {0}]} {[{1} {3} {5}]} {[{2} {6} {10}]} {[{3} {9} {15}]} {[{4} {12} {20}]}]
10
12
-------------------
[{[{0} {0} {0}]} {[{1} {3} {5}]} {[{2} {6} {10}]} {[{3} {9} {15}]} {[{4} {12} {20}]}]
5
[{0} {0} {0} {1} {3} {5} {2} {6} {10} {3} {9} {15} {4} {12} {20}]
15

Docs

Types

TypeDescription
IntA wrapper of int.
StringA wrapper of string.
TypeA wrapper of any type.
IntArrayA Int array.
StringArrayA String array.
TypeArrayA Type array.

Common Method

MethodReturn TypeDescription
NewType(v interface{})TypeNew a type from interface.
NewArray(size int)TypeArrayNew empty array.
NewRange(start, end, step int)IntArrayGenerate a range IntArray.
FromIntArray(items []int)IntArrayFrom []int create IntArray.
FromStringArray(items []string)StringArrayFrom []string create StringArray.
FromInterfaceArray(items interface{})TypeArrayFrom array of interface{} create TypeArray.
LineInput()StringRead a line of standard input.
If(boolVar bool, trueVal, falseVal interface{})interface {}If boolVar is true, return trueVal, otherwise return falseVal.

Basic Type

Int

MethodReturn TypeDescription
ValueOf()intGet int value.
ToString()StringTo String type.

String

MethodReturn TypeDescription
ValueOf()stringGet string value.
ToInt()IntTo Int type.
Split(sep string)StringArraySeparated by sep.
Length()IntGet string length.
Trim(cutset string)StringA wrapper of strings.Trim()
TrimSpace()StringA wrapper of strings.TrimSpace()
TrimLeft(prefix string)StringA wrapper of strings.TrimPrefix()
TrimRight(suffix string)StringA wrapper of strings.TrimSuffix()
IndexOf(v string)IntA wrapper of strings.Index()
Contains(v string)boolA wrapper of strings.Contains()
ToLower()StringA wrapper of strings.ToLower()
ToUpper()StringA wrapper of strings.ToUpper()

Type

MethodReturn TypeDescription
ValueOf()interface{}Get interface{} value.
ToString()StringTo String type.
ToInt()IntTo Int type.
ToArray()TypeArrayTo TypeArray type. (if fails, return an empty TypeArray)
ToStringArray()StringArrayTo StringArray type. (if fails, return an empty StringArray)
ToIntArray()IntArrayTo IntArray type. (if fails, return an empty IntArray)

Array Type

IntArray

MethodReturn TypeDescription
Length()IntGet array length.
Map(lambda func(v Int, i int) interface{})TypeArraySame as javascript array.map() or vb LINQ Select().
ForEach(lambda func(v Int, i int))voidSame as javascript array.forEach().
Filter(lambda func(v Int, i int) bool)IntArraySame as javascript array.filter() or vb LINQ FindAll().
ToStringArray()StringArrayTo StringArray.
ToTypeArray()TypeArrayTo TypeArray.
Sum()IntGet the sum of the array.
Fill(v Int)IntArraySame as javascript array.fill().
Every(lambda func(v Int, i int) bool)boolSame as javascript array.every().
Some(lambda func(v Int, i int) bool)boolSame as javascript array.some().
Contains(v int)boolSame as javascript array.includes() or vb collection type Contains().
FindIndex(lambda func(v Int, i int) bool)IntSame as javascript array.findIndex().
IndexOf(v int)IntSame as javascript array.indexOf() or vb collection type IndexOf().
Append(v int)voidAppend the item to last.
Pop()IntGet last item and remove.
Enqueue(v int)voidAppend the item to first.
Dequeue()IntGet first item and remove.
Remove(v int)voidRemove first match item.
First()IntGet first item.
Last()IntGet last item.
Sort()voidSort by increment.
SortBy(lambda func(x, y Int))voidSort by customize.
Copy()IntArrayCopy array.
Slice(start, end int)IntArraySimilar to javascript array.slice(), but when end = 0, it is equal to array.length

StringArray

MethodReturn TypeDescription
Length()IntGet array length.
Map(lambda func(s String, i int) interface{})TypeArraySame as javascript array.map() or vb LINQ Select().
ForEach(lambda func(v String, i int))voidSame as javascript array.forEach().
Filter(lambda func(s String, i int) bool)StringArraySame as javascript array.filter() or vb LINQ FindAll().
ToIntArray()IntArrayTo IntArray.
ToTypeArray()TypeArrayTo TypeArray.
Join(sep string)StringSame as javascript array.join().
Fill(v String)StringArraySame as javascript array.fill().
Every(lambda func(v String, i int) bool)boolSame as javascript array.every().
Some(lambda func(v String, i int) bool)boolSame as javascript array.some().
Contains(v string)boolSame as javascript array.includes() or vb collection type Contains().
FindIndex(lambda func(v String, i int) bool)IntSame as javascript array.findIndex().
IndexOf(v string)IntSame as javascript array.indexOf() or vb collection type IndexOf().
Append(v string)voidAppend the item to last.
Pop()StringGet last item and remove.
Enqueue(v string)voidAppend the item to first.
Dequeue()StringGet first item and remove.
Remove(v string)voidRemove first match item.
First()StringGet first item.
Last()StringGet last item.
Sort()voidSort by increment.
SortBy(lambda func(x, y String))voidSort by customize.
Copy()StringArrayCopy array.
Slice(start, end int)StringArraySimilar to javascript array.slice(), but when end = 0, it is equal to array.length

TypeArray

MethodReturn TypeDescription
Length()IntGet array length.
ToStringArray()StringArrayTo StringArray.
ToIntArray()IntArrayTo IntArray.
Map(lambda func(s Type, i int) interface{})TypeArraySame as javascript array.map() or vb LINQ Select().
ForEach(lambda func(v Type, i int))voidSame as javascript array.forEach().
Filter(lambda func(s Type, i int) bool)TypeArraySame as javascript array.filter() or vb LINQ FindAll().
Fill(v interface{})TypeArraySame as javascript array.fill().
Every(lambda func(v Type, i int) bool)boolSame as javascript array.every().
Some(lambda func(v Type, i int) bool)boolSame as javascript array.some().
FindIndex(lambda func(v Type, i int) bool)IntSame as javascript array.findIndex().
Sort(lambda func(x, y Type))voidSort by customize.
Get(indexes ...int)TypeGet item by indexes.
Flatten()TypeArraySame as javascript array.flat().
Copy()TypeArrayCopy array.
Slice(start, end int)TypeArraySimilar to javascript array.slice(), but when end = 0, it is equal to array.length

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author