All Projects → thecasualcoder → godash

thecasualcoder / godash

Licence: MIT License
Lodash for Golang

Programming Languages

go
31211 projects - #10 most used programming language

Labels

Projects that are alternatives of or similar to godash

ljq eleme
vue2 +vue-router2 + es6 +webpack 高仿饿了么Web app商家详情核心模块
Stars: ✭ 22 (-48.84%)
Mutual labels:  lodash
php-lodash
php-lodash is a PHP utility library, similar to Underscore/Lodash.
Stars: ✭ 35 (-18.6%)
Mutual labels:  lodash
Aris
Aris - A fast and powerful tool to write HTML in JS easily. Includes syntax highlighting, templates, SVG, CSS autofixing, debugger support and more...
Stars: ✭ 61 (+41.86%)
Mutual labels:  lodash
tooltip-generator
💫 A tool to generate CSS code for tooltips. Built with Vue.js and Tailwind CSS
Stars: ✭ 32 (-25.58%)
Mutual labels:  lodash
elf-taro
Taro 小程序 脚手架 (Taro 3 + Dva + Typescript + Immer) - 内置 Redux 计数器, 异步数据请求 与 腾讯小程序地图 demo
Stars: ✭ 61 (+41.86%)
Mutual labels:  lodash
gameplate
🎮 Boilerplate for creating game with WebGL & Redux 🎲
Stars: ✭ 39 (-9.3%)
Mutual labels:  lodash
Code-Runner
A portal for programmers to code online
Stars: ✭ 50 (+16.28%)
Mutual labels:  lodash
underwater
~2kb - ES6 Collection of helper functions. Lodash like
Stars: ✭ 18 (-58.14%)
Mutual labels:  lodash
api
_api is an autogenerated CRUD API built on LowDB and ExpressJS.
Stars: ✭ 73 (+69.77%)
Mutual labels:  lodash
Javascript-Interview-Preparation
A curated collection of javascript interview questions & solutions.
Stars: ✭ 163 (+279.07%)
Mutual labels:  lodash
R.apex
Functional utility library for Apex
Stars: ✭ 80 (+86.05%)
Mutual labels:  lodash
webpack-starter
Simple webpack config with babel, scss, and lodash
Stars: ✭ 47 (+9.3%)
Mutual labels:  lodash
map-keys-deep-lodash
Map/rename keys recursively with Lodash
Stars: ✭ 16 (-62.79%)
Mutual labels:  lodash
fast-cartesian
Fast cartesian product
Stars: ✭ 51 (+18.6%)
Mutual labels:  lodash
KodersHub
CodeEditor Mern WebApp specifically designed for Kids and Teens🤩
Stars: ✭ 25 (-41.86%)
Mutual labels:  lodash
lancet
A comprehensive, efficient, and reusable util function library of go.
Stars: ✭ 2,228 (+5081.4%)
Mutual labels:  lodash
youmightnotneed
🤯 A collection of `You might not need ${something}` resources
Stars: ✭ 131 (+204.65%)
Mutual labels:  lodash
eslint-plugin-lodash-template
ESLint plugin for John Resig-style micro template, Lodash's template, Underscore's template and EJS.
Stars: ✭ 15 (-65.12%)
Mutual labels:  lodash
rudash
Rudash - Lodash for Ruby Apps
Stars: ✭ 27 (-37.21%)
Mutual labels:  lodash
utils.js
Fast, small and purely functional utility library
Stars: ✭ 132 (+206.98%)
Mutual labels:  lodash

Godash

Build Status Go Doc

DeepSource

Inspired from Lodash for golang

Why?

  • I did not like most map/reduce implementations that returned an interface{} which had to be typecasted. This library follows the concept of how json.Marshal works. Create an output variable outside the functions and pass a pointer reference to it, so it can be set.
  • This library heavily makes use of reflect package and hence will have an impact on performance. DO NOT USE THIS IN PRODUCTION. This repository is more of a way to learn the reflect package and measure its performance impact.
  • All functions have validations on how mapper function/predicate functions should be written. So even if we lose out on compile time validation, the library still does not panic if it does not know how to handle an argument passed to it.

Available Functions

  1. Map
  2. Filter
  3. Reduce
  4. Any or Some
  5. Find
  6. All or Every

Usages

Map

Map applies a mapper function on each element of an input and sets it in output. For more docs.

func main() {
	input := []int{1, 2, 3, 4, 5}
	var output []int

	godash.Map(input, &output, func(el int) int {
		return el * el
	})

	fmt.Println(output) // prints 1 4 9 16 25
}
type Person struct {
	Name string
	Age Int
}

func main() {
	input := []Person{
		{Name: "John", Age: 22},
		{Name: "Doe", Age: 23},
	}
	var output []string

	godash.Map(input, &output, func(person Person) string {
		return person.Name
	})

	fmt.Println(output) // prints John Doe
}
func main() {
	input := map[string]int{
			"key1": 1,
			"key2": 2,
			"key3": 3,
		}
	var output []int

	godash.Map(input, &output, func(el int) int {
		return el * el
	})

	fmt.Println(output) // prints 1 4 9
}

Filter

Filter out elements that fail the predicate. For more docs.

func main() {
	input := []int{1, 2, 3, 4, 5}
	var output []int

	godash.Filter(input, &output, func(element int) bool {
		return element % 2 == 0
	})

	fmt.Println(output) // prints 2 4
}
func main() {
	input := []Person{
		{Name: "John", Age: 20},
		{Name: "Doe", Age: 30},
	}
	var output []string

	godash.Filter(input, &output, func(person Person) bool {
		return person.Age > 25
	})

	fmt.Println(output) // prints {Doe 30}
}

Reduce

Reduce can accept a reducer and apply the reducer on each element of the input slice while providing an accumulator to save the reduce output. For more docs.

func main() {
    input := []string{"count", "words", "and", "print", "words", "count"}
	accumulator := map[string]int{}

	_ = godash.Reduce(input, &accumulator, func(acc map[string]int, element string) map[string]int {
		if _, present := acc[element]; present {
			acc[element] = acc[element] + 1
		} else {
			acc[element] = 1
		}
		return acc
	})

	bytes, _ := json.MarshalIndent(accumulator, "", "  ")
	fmt.Println(string(bytes))

	// Output:
	//{
	//   "and": 1,
	//   "count": 2,
	//   "print": 1,
	//   "words": 2
	//}

}
func main() {
	input := []Person{
		{Name: "John", Age: 22},
		{Name: "Doe", Age: 23},
	}
	var output int

	godash.Reduce(input, &output, func(sum int, person Person) int {
		return sum + person.Age
	})

	fmt.Println(output) // prints 45
}

Any or Some

Any or Some checks if predicate returns truthy for any element of collection. Iteration is stopped once predicate returns truthy. For more docs.

func main() {
	input := []int{1, 2, 3, 4, 5}
	var output []int
	output, _ := godash.Any(input, func(num int) bool {
		return num % 7 == 0
	})
	fmt.Println(output) // prints false
}
func main() {
	input := []Person{
		{Name: "John", Age: 25},
		{Name: "Doe", Age: 15},
	}
	var output int
	output, _ := godash.Some(input, func(person Person) bool {
		return person.Age < 18
	})
	fmt.Println(output) // prints true
}

Find

Returns the first element which passes the predicate. For more docs.

func main() {
	input := []string{"john","wick","will"}
	var output string

	godash.Find(input, &output, func(element string) bool {
    	return strings.HasPrefix(element, "w") // starts with
	}
	// output is "wick"
	fmt.Println(output)
}

All or Every

All or Every checks if predicate returns truthy for all element of collection. Iteration is stopped once predicate returns falsely. For more docs.

func main() { 
	input := []int{1, 2, 3, 4, 5} 
	var output bool 
	output, _ := godash.All(input, func(num int) bool { 
		return num >= 1 
	}) 
	fmt.Println(output) // prints true 
} 
func main() { 
	input := []Person{ 
		{Name: "John", Age: 25}, 
		{Name: "Doe", Age: 15}, 
	} 
	var output bool 
	output, _ := godash.Every(input, func(person Person) bool {
		return person.Age < 18 
	}) 
	fmt.Println(output) // prints false 
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].