fstord

package module
v0.0.0-...-186c73c Latest Latest
Warning

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

Go to latest
Published: May 4, 2016 License: MIT Imports: 2 Imported by: 0

README

fstord

===

fstord (read First Order - yeah you're right) is a package that implemented higher order functions.

Install

go get github.com/jaxi/fstord

Examples


package main

import (
	"fmt"

	"github.com/jaxi/fstord"
)

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

	fmt.Println(
		fstord.Map(slice, func(x int) int { return x * x }).([]int),
	)
	// [1 4 9 16 25]

	fmt.Println(
		fstord.Filter(slice, func(x int) bool { return x%2 == 1 }).([]int),
	)
	// [1 3 5]

	mp := map[int]int{
		1: 2,
		3: 4,
		5: 6,
	}

	fmt.Println(
		fstord.Map(mp, func(a, b int) int { return b + 1 }).(map[int]int),
	)
	// map[1:3 3:5 5:7]

	fmt.Println(
		fstord.Filter(mp, func(a, b int) bool { return a+b == 3 }),
	)
	// map[1:2]

	fmt.Println(
		fstord.Reduce([]int{1, 2, 3, 4}, func(acc, i int) int { return acc + i }, 0).(int),
	)
	// 10

	fmt.Println(
		fstord.Reduce(mp, func(a, k, v int) int { return a + v }, 0).(int),
	)
	// 12

	fmt.Println(
		fstord.Any([]int{1, 2, 3, 4}, func(i int) bool { return i > 4 }),
	)
	// false

	fmt.Println(
		fstord.Every([]int{1, 3, 5}, func(i int) bool { return i%2 == 1 }),
	)
	// true

	fmt.Println(
		fstord.Count([]int{1, 2, 3, 4, 5}, func(i int) bool { return i%2 == 1 }),
	)
	// 3
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

func Any(enumerable, fun interface{}) bool

Any returns whether any elements of a slice/map return true when invoke fun

func Count

func Count(enumerable, fun interface{}) int

Count returns the number of elements of a slice/map that return true when invoke fun

func Every

func Every(enumerable, fun interface{}) bool

Every returns whether all the elements of a slice/map return true when invoke fun How the code works is kind like the Any func, but IMO is a good duplication

func Filter

func Filter(enumerable, fun interface{}) interface{}

Filter the enumerable - returns the elements for which fun returns true

func Map

func Map(enumerable, fun interface{}) interface{}

Map Returns a slice/map where each element is the result of invoking fun on each corresponding element of slice/map

func Reduce

func Reduce(enumerable, fun, initial interface{}) interface{}

Reduce for each element in the enumerable. The accumulated and element are passed into fun as arguments.

Types

This section is empty.

Jump to

Keyboard shortcuts

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