Skip to content

Curried map, reduce and filter for partial application  #330

@Southclaws

Description

@Southclaws

I make use of partial application quite a lot, especially with lodash/fp, it's handy for building ad-hoc easily testable transformers.

Would be nice to see some added here! One repo that's growing out of #82 is dt where I'm dropping all these useful functions but it would be great to have it all in one place.

https://github.com/Southclaws/dt

An example of partial application would be:

func Map[T any, R any](iteratee func(T) R) func([]T) []R {
	return func(collection []T) []R {
		result := make([]R, len(collection))

		for i, item := range collection {
			result[i] = iteratee(item)
		}

		return result
	}
}

so the argument order is flipped and separated into two separate function calls. This allows you to "build" a "mapper": mapper := Map(someFunc) then re-use it later: updated := mapper(list)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions