go_solve_kit

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2022 License: MIT Imports: 6 Imported by: 3

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

Type Description
Int A wrapper of int.
String A wrapper of string.
Type A wrapper of any type.
IntArray A Int array.
StringArray A String array.
TypeArray A Type array.

Common Method

Method Return Type Description
NewType(v interface{}) Type New a type from interface.
NewArray(size int) TypeArray New empty array.
NewRange(start, end, step int) IntArray Generate a range IntArray.
FromIntArray(items []int) IntArray From []int create IntArray.
FromStringArray(items []string) StringArray From []string create StringArray.
FromInterfaceArray(items interface{}) TypeArray From array of interface{} create TypeArray.
LineInput() String Read a line of standard input.
If(boolVar bool, trueVal, falseVal interface{}) interface {} If boolVar is true, return trueVal, otherwise return falseVal.

Basic Type

Int
Method Return Type Description
ValueOf() int Get int value.
ToString() String To String type.
String
Method Return Type Description
ValueOf() string Get string value.
ToInt() Int To Int type.
Split(sep string) StringArray Separated by sep.
Length() Int Get string length.
Trim(cutset string) String A wrapper of strings.Trim()
TrimSpace() String A wrapper of strings.TrimSpace()
TrimLeft(prefix string) String A wrapper of strings.TrimPrefix()
TrimRight(suffix string) String A wrapper of strings.TrimSuffix()
IndexOf(v string) Int A wrapper of strings.Index()
Contains(v string) bool A wrapper of strings.Contains()
ToLower() String A wrapper of strings.ToLower()
ToUpper() String A wrapper of strings.ToUpper()
Type
Method Return Type Description
ValueOf() interface{} Get interface{} value.
ToString() String To String type.
ToInt() Int To Int type.
ToArray() TypeArray To TypeArray type. (if fails, return an empty TypeArray)
ToStringArray() StringArray To StringArray type. (if fails, return an empty StringArray)
ToIntArray() IntArray To IntArray type. (if fails, return an empty IntArray)

Array Type

IntArray
Method Return Type Description
Length() Int Get array length.
Map(lambda func(v Int, i int) interface{}) TypeArray Same as javascript array.map() or vb LINQ Select().
ForEach(lambda func(v Int, i int)) void Same as javascript array.forEach().
Filter(lambda func(v Int, i int) bool) IntArray Same as javascript array.filter() or vb LINQ FindAll().
ToStringArray() StringArray To StringArray.
ToTypeArray() TypeArray To TypeArray.
Sum() Int Get the sum of the array.
Fill(v Int) IntArray Same as javascript array.fill().
Every(lambda func(v Int, i int) bool) bool Same as javascript array.every().
Some(lambda func(v Int, i int) bool) bool Same as javascript array.some().
Contains(v int) bool Same as javascript array.includes() or vb collection type Contains().
FindIndex(lambda func(v Int, i int) bool) Int Same as javascript array.findIndex().
IndexOf(v int) Int Same as javascript array.indexOf() or vb collection type IndexOf().
Append(v int) void Append the item to last.
Pop() Int Get last item and remove.
Enqueue(v int) void Append the item to first.
Dequeue() Int Get first item and remove.
Remove(v int) void Remove first match item.
First() Int Get first item.
Last() Int Get last item.
Sort() void Sort by increment.
SortBy(lambda func(x, y Int)) void Sort by customize.
Copy() IntArray Copy array.
Slice(start, end int) IntArray Similar to javascript array.slice(), but when end = 0, it is equal to array.length
StringArray
Method Return Type Description
Length() Int Get array length.
Map(lambda func(s String, i int) interface{}) TypeArray Same as javascript array.map() or vb LINQ Select().
ForEach(lambda func(v String, i int)) void Same as javascript array.forEach().
Filter(lambda func(s String, i int) bool) StringArray Same as javascript array.filter() or vb LINQ FindAll().
ToIntArray() IntArray To IntArray.
ToTypeArray() TypeArray To TypeArray.
Join(sep string) String Same as javascript array.join().
Fill(v String) StringArray Same as javascript array.fill().
Every(lambda func(v String, i int) bool) bool Same as javascript array.every().
Some(lambda func(v String, i int) bool) bool Same as javascript array.some().
Contains(v string) bool Same as javascript array.includes() or vb collection type Contains().
FindIndex(lambda func(v String, i int) bool) Int Same as javascript array.findIndex().
IndexOf(v string) Int Same as javascript array.indexOf() or vb collection type IndexOf().
Append(v string) void Append the item to last.
Pop() String Get last item and remove.
Enqueue(v string) void Append the item to first.
Dequeue() String Get first item and remove.
Remove(v string) void Remove first match item.
First() String Get first item.
Last() String Get last item.
Sort() void Sort by increment.
SortBy(lambda func(x, y String)) void Sort by customize.
Copy() StringArray Copy array.
Slice(start, end int) StringArray Similar to javascript array.slice(), but when end = 0, it is equal to array.length
TypeArray
Method Return Type Description
Length() Int Get array length.
ToStringArray() StringArray To StringArray.
ToIntArray() IntArray To IntArray.
Map(lambda func(s Type, i int) interface{}) TypeArray Same as javascript array.map() or vb LINQ Select().
ForEach(lambda func(v Type, i int)) void Same as javascript array.forEach().
Filter(lambda func(s Type, i int) bool) TypeArray Same as javascript array.filter() or vb LINQ FindAll().
Fill(v interface{}) TypeArray Same as javascript array.fill().
Every(lambda func(v Type, i int) bool) bool Same as javascript array.every().
Some(lambda func(v Type, i int) bool) bool Same as javascript array.some().
FindIndex(lambda func(v Type, i int) bool) Int Same as javascript array.findIndex().
Sort(lambda func(x, y Type)) void Sort by customize.
Get(indexes ...int) Type Get item by indexes.
Flatten() TypeArray Same as javascript array.flat().
Copy() TypeArray Copy array.
Slice(start, end int) TypeArray Similar to javascript array.slice(), but when end = 0, it is equal to array.length

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func If

func If(boolVar bool, trueVal, falseVal interface{}) interface{}

Types

type Int

type Int int

func (Int) ToString

func (i Int) ToString() String

func (Int) ValueOf

func (i Int) ValueOf() int

type IntArray

type IntArray []Int

func FromIntArray

func FromIntArray(items []int) IntArray

func NewRange

func NewRange(start, end, step int) IntArray

func (*IntArray) Append

func (array *IntArray) Append(v int)

func (IntArray) Contains

func (array IntArray) Contains(v int) bool

func (IntArray) Copy

func (array IntArray) Copy() IntArray

func (*IntArray) Dequeue

func (array *IntArray) Dequeue() Int

func (*IntArray) Enqueue

func (array *IntArray) Enqueue(v int)

func (IntArray) Every

func (array IntArray) Every(lambda func(v Int, i int) bool) bool

func (IntArray) Fill

func (array IntArray) Fill(v Int) IntArray

func (IntArray) Filter

func (array IntArray) Filter(lambda func(v Int, i int) bool) IntArray

func (IntArray) FindIndex

func (array IntArray) FindIndex(lambda func(v Int, i int) bool) Int

func (IntArray) First

func (array IntArray) First() Int

func (IntArray) ForEach

func (array IntArray) ForEach(lambda func(s Int, i int))

func (IntArray) IndexOf

func (array IntArray) IndexOf(v int) Int

func (IntArray) Last

func (array IntArray) Last() Int

func (IntArray) Length

func (array IntArray) Length() Int

func (IntArray) Map

func (array IntArray) Map(lambda func(v Int, i int) interface{}) TypeArray

func (*IntArray) Pop

func (array *IntArray) Pop() Int

func (*IntArray) Remove

func (array *IntArray) Remove(v int)

func (IntArray) Slice

func (array IntArray) Slice(start, end int) IntArray

func (IntArray) Some

func (array IntArray) Some(lambda func(v Int, i int) bool) bool

func (IntArray) Sort

func (array IntArray) Sort()

func (IntArray) SortBy

func (array IntArray) SortBy(lambda func(x, y Int) bool)

func (IntArray) Sum

func (array IntArray) Sum() Int

func (IntArray) ToStringArray

func (array IntArray) ToStringArray() StringArray

func (IntArray) ToTypeArray

func (array IntArray) ToTypeArray() TypeArray

type String

type String string

func LineInput

func LineInput() String

func (String) Contains

func (s String) Contains(v string) bool

func (String) IndexOf

func (s String) IndexOf(v string) Int

func (String) Length

func (s String) Length() Int

func (String) Split

func (s String) Split(sep string) StringArray

func (String) ToInt

func (s String) ToInt() Int

func (String) ToLower

func (s String) ToLower() String

func (String) ToUpper

func (s String) ToUpper() String

func (String) Trim

func (s String) Trim(cutset string) String

func (String) TrimLeft

func (s String) TrimLeft(prefix string) String

func (String) TrimRight

func (s String) TrimRight(suffix string) String

func (String) TrimSpace

func (s String) TrimSpace() String

func (String) ValueOf

func (s String) ValueOf() string

type StringArray

type StringArray []String

func FromStringArray

func FromStringArray(items []string) StringArray

func (*StringArray) Append

func (array *StringArray) Append(v string)

func (StringArray) Contains

func (array StringArray) Contains(v string) bool

func (StringArray) Copy

func (array StringArray) Copy() StringArray

func (*StringArray) Dequeue

func (array *StringArray) Dequeue() String

func (*StringArray) Enqueue

func (array *StringArray) Enqueue(v String)

func (StringArray) Every

func (array StringArray) Every(lambda func(v String, i int) bool) bool

func (StringArray) Fill

func (array StringArray) Fill(v String) StringArray

func (StringArray) Filter

func (array StringArray) Filter(lambda func(s String, i int) bool) StringArray

func (StringArray) FindIndex

func (array StringArray) FindIndex(lambda func(v String, i int) bool) Int

func (StringArray) First

func (array StringArray) First() String

func (StringArray) ForEach

func (array StringArray) ForEach(lambda func(s String, i int))

func (StringArray) IndexOf

func (array StringArray) IndexOf(v string) Int

func (StringArray) Join

func (array StringArray) Join(sep string) String

func (StringArray) Last

func (array StringArray) Last() String

func (StringArray) Length

func (array StringArray) Length() Int

func (StringArray) Map

func (array StringArray) Map(lambda func(s String, i int) interface{}) TypeArray

func (*StringArray) Pop

func (array *StringArray) Pop() String

func (*StringArray) Remove

func (array *StringArray) Remove(v string)

func (StringArray) Slice

func (array StringArray) Slice(start, end int) StringArray

func (StringArray) Some

func (array StringArray) Some(lambda func(v String, i int) bool) bool

func (StringArray) Sort

func (array StringArray) Sort()

func (StringArray) SortBy

func (array StringArray) SortBy(lambda func(x, y String) bool)

func (StringArray) ToIntArray

func (array StringArray) ToIntArray() IntArray

func (StringArray) ToTypeArray

func (array StringArray) ToTypeArray() TypeArray

type Type

type Type struct {
	// contains filtered or unexported fields
}

func NewType

func NewType(v interface{}) Type

func (Type) ToArray

func (t Type) ToArray() TypeArray

func (Type) ToInt

func (t Type) ToInt() Int

func (Type) ToIntArray

func (t Type) ToIntArray() IntArray

func (Type) ToString

func (t Type) ToString() String

func (Type) ToStringArray

func (t Type) ToStringArray() StringArray

func (Type) ValueOf added in v0.2.1

func (t Type) ValueOf() interface{}

type TypeArray

type TypeArray []Type

func FromInterfaceArray

func FromInterfaceArray(items interface{}) TypeArray

func NewArray

func NewArray(size int) TypeArray

func (TypeArray) Copy

func (array TypeArray) Copy() TypeArray

func (TypeArray) Every

func (array TypeArray) Every(lambda func(v Type, i int) bool) bool

func (TypeArray) Fill

func (array TypeArray) Fill(v interface{}) TypeArray

func (TypeArray) Filter

func (array TypeArray) Filter(lambda func(s Type, i int) bool) TypeArray

func (TypeArray) FindIndex

func (array TypeArray) FindIndex(lambda func(v Type, i int) bool) Int

func (TypeArray) Flatten

func (array TypeArray) Flatten() TypeArray

func (TypeArray) ForEach

func (array TypeArray) ForEach(lambda func(v Type, i int))

func (TypeArray) Get

func (array TypeArray) Get(indexes ...int) Type

func (TypeArray) Length

func (array TypeArray) Length() Int

func (TypeArray) Map

func (array TypeArray) Map(lambda func(v Type, i int) interface{}) TypeArray

func (TypeArray) Slice

func (array TypeArray) Slice(start, end int) TypeArray

func (TypeArray) Some

func (array TypeArray) Some(lambda func(v Type, i int) bool) bool

func (TypeArray) Sort

func (array TypeArray) Sort(lambda func(x, y Type) bool)

func (TypeArray) ToIntArray

func (array TypeArray) ToIntArray() IntArray

func (TypeArray) ToStringArray

func (array TypeArray) ToStringArray() StringArray

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL