Code Monkey home page Code Monkey logo

go-enumerable's Introduction

Go-Enumerable

Build Status

Create enumerable functions(map, filter, some, every, reduce, reduceRight).

Update

  • 2013-08-10 : Add type error.
  • 2013-07-25 : Add concurrent functions.
  • 2013-07-09 : First version

Install

go get github.com/swdyh/go-enumerable/src/enumerable

Example

package main

import (
	"fmt"
	"github.com/swdyh/go-enumerable/src/enumerable"
	"io/ioutil"
	"net/http"
)

func main() {

	// map for slice
	var twiceInt func([]int) []int
	enumerable.MakeMap(&twiceInt, func(i int) int { return i * 2 })
	fmt.Println(twiceInt([]int{1, 2, 3})) // [2 4 6]

	// type error
	var twiceIntWrongType func([]int) []int
	err := enumerable.MakeMap(&twiceIntWrongType, func(i int) string { return fmt.Sprintf("%d", i*2) })
	fmt.Println(err) // TypeError: int != string
         
	// filter for slice
	var filterOdd func([]int) []int
	enumerable.MakeFilter(&filterOdd, func(i int) bool { return i%2 == 1 })
	fmt.Println(filterOdd([]int{1, 2, 3})) // [1 3]

	// map for slice (concurrnt)
	urls := []string{
		"http://www.google.co.jp",
		"http://www.yahoo.co.jp"}
	var getAll func([]string) []string
	enumerable.MakeMapC(&getAll, func(url string) string {
		res, _ := http.Get(url)
		b, _ := ioutil.ReadAll(res.Body)
		return res.Status + " " + string(b)[0:50]
	}, 2)
	for _, v := range getAll(urls) {
		fmt.Println(v)
	}

	// get first result (concurrnt)
	var getFirst func([]string) string
	enumerable.MakeFirst(&getFirst, func(url string) string {
		res, _ := http.Get(url)
		b, _ := ioutil.ReadAll(res.Body)
		return url + " " + res.Status + " " + string(b)[0:50]
	})
	fmt.Println(getFirst(urls))
}

https://github.com/swdyh/go-enumerable/blob/master/src/enumerable/example_makemap_test.go https://github.com/swdyh/go-enumerable/blob/master/src/enumerable/enumerable_test.go

Github

https://github.com/swdyh/go-enumerable

License

The MIT License (MIT) Copyright (c) 2013 swdyh

go-enumerable's People

Contributors

swdyh avatar

Watchers

James Cloos avatar Mustafa Aydın avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.