Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Do we have a flow function like functionality? #546

Open
abhisekp opened this issue Oct 27, 2024 · 0 comments
Open

Question: Do we have a flow function like functionality? #546

abhisekp opened this issue Oct 27, 2024 · 0 comments

Comments

@abhisekp
Copy link

Do we have a flow function like functionality?

Something like this? How do I replace this with utilities from the lo library?

package main

import (
	"fmt"
	"iter"
	"math"
	"slices"
)

type FlowFn[S iter.Seq[E], E any] func(S) S

type FlowRetFn[S iter.Seq[E], E any] func(S) S

// Flow takes variadic functions and returns a composed function that processes a sequence.
func Flow[S iter.Seq[E], E any](fns ...FlowFn[S, E]) FlowRetFn[S, E] {
	return func(s S) S {
		for _, fn := range fns {
			s = fn(s)
		}
		return s
	}
}

// Generic Filter function that accepts a predicate.
func Filter[S iter.Seq[E], E any](s S, predicate func(E) bool) S {
	return func(yield func(E) bool) {
		for v := range s {
			if predicate(v) {
				if !yield(v) {
					return
				}
			}
		}
	}
}

// Generic Map function that accepts a transformer function.
func Map[S iter.Seq[E], E any, R any](s S, transformer func(E) R) iter.Seq[R] {
	return func(yield func(R) bool) {
		for v := range s {
			if !yield(transformer(v)) {
				return
			}
		}
	}
}


func Integers(n int) iter.Seq[int] {
	return func(yield func(int) bool) {
		for v := range n {
			if !yield(v) {
				return
			}
		}
	}
}

func IsOdd(s iter.Seq[int]) iter.Seq[int] {
	return Filter(s, func(v int) bool {
		return v%2 != 0
	})
}

func IsEven(s iter.Seq[int]) iter.Seq[int] {
	return Filter(s, func(v int) bool {
		return v%2 == 0
	})
}

func Double(s iter.Seq[int]) iter.Seq[int] {
	return Map(s, func(v int) int {
		return v * 2
	})
}


func Power(n int) FlowFn[iter.Seq[int], int] {
	return func (s iter.Seq[int]) iter.Seq[int] {
		return Map(s, func(v int) int {
			return int(math.Pow(float64(v), float64(n)))
		})
	}
}

func Log(msg string) FlowFn[iter.Seq[int], int] {
	return func (s iter.Seq[int]) iter.Seq[int] {
		fmt.Println(msg, slices.Collect(s))
		return s
	}
}

func Square(s iter.Seq[int]) iter.Seq[int] {
	return Map(s, func(v int) int {
		return v * v
	})
}



func main() {
	data := Integers(5)

	// Create the flow
	flowFn := Flow(IsOdd, Log("Odd:"), Double, Log("Double:"), Power(2), Log("Power:"))

	// Execute the flow
	flowFn(data)
}
@abhisekp abhisekp changed the title Do we have a flow function like functionality? Question: Do we have a flow function like functionality? Oct 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant