Giter Site home page Giter Site logo

slice_fn's Introduction

slice_fn

This go module provides simple methods for manipulations of slices via callback functions. Examples can be found under slice_fn_test.go.

Examples

Map

The Map() method creates a new slice/array populated with the results of calling a provided function on every element in the calling array.

    in := []int{1, 2, 3, 4, 5}

    actual := slice_fn.Map(in, func(val int) int { return val + 1 })
    
    ->> [2, 3, 4, 5, 6]

Filter

The Filter() method creates a copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.

    in := []int{1, 2, 2, 4, 2}

    actual := slice_fn.Filter(in, func(val int) bool { return val == 2 })    

    ->> [2, 2, 2]

Reduce

The Reduce() method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

    in := []int{1, 2, 2, 4, 2}

    actual := slice_fn.Reduce(in, 0, func(previous int, current int) int { return previous + current })

    ->> 11

Find

The Find() method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, object/type with initialized values is returned. (Meaning f.e. int is used then 0 is returned)

    input := []int{1, 2, 3, 4, 5}

    actual := slice_fn.Find(input, func(val int) bool { return val == 4 })
    
    ->> 4

FindIndex

The FindIndex() method returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.

    input := []int{1, 2, 3, 4, 5}

    actual := slice_fn.FindIndex(input, func(val int) bool { return val == 3 })

    ->> 2

FindLast

The FindLast() method returns the value of the last element in an array that satisfies the provided testing function. If no values satisfy the testing function, object/type with initialized values is returned. (Meaning f.e. int is used then 0 is returned)

    input := []int{1, 5, 3, 4, 5}

    actual := slice_fn.FindLast(input, func(val int) bool { return val == 5 })

    ->> 5

FindLastIndex

The FindLastIndex() method returns the value of the last element in an array that satisfies the provided testing function. If no values satisfy the testing function, -1 is returned.

    input := []int{1, 5, 3, 4, 5}

    actual := slice_fn.FindLast(input, func(val int) bool { return val == 5 })

    ->> 4

ForEach

The ForEach() method executes a provided function once for each array element.

    in := []int{1, 2, 2, 4, 2}
    sum := 0

    slice_fn.ForEach(input, func(val int) { sum += val }) 

    ->> 11

slice_fn's People

Contributors

tkaelbel avatar

Watchers

 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.