Code Monkey home page Code Monkey logo

Comments (11)

hamidb80 avatar hamidb80 commented on May 31, 2024 1

I'm working on Zoom's idea presented in: zero-functional/zero-functional#74

the idea is to convert defined iterator to macros

iterator flatten[T](row: openArray[T]): T {.adapter.} =
  block wrapper: ...
  block before:     ...
  block after:    ...
  
  block reducer:  
     for it in row:
        yield it

# generated code --------------------------------

# type checking stuff - 
iterator flattenType[T](row: openArray[T]): T = discard

macro flattenReducer(row, accFnIdent): untyped =
  result = quote:
    for n in `row`:
      yield n

  const yieldPaths = @[
    @[0, 4, 0],
  ]

  result.replaceYieldsWrapperBody yieldPaths, accFnIdent

macro flattenBefore(): untyped = ... [same as flattenBody but in different context]
macro flattenAfter(): untyped = ...
template flattenWrapper(bodt): untyped = body

the pattern is gonna be:

def states
wrapper:
    before
    loop it:
      reducer it
    after

from iterrr.

hamidb80 avatar hamidb80 commented on May 31, 2024

I have thought about it for some time, IMO the performant and extensible solution doesn't exist.

For example, think of cycle. How would you implement that with regular code? OK then how would you convert it to extensible pattern, in a way that pattern also works with take or repeat?

And if you could find that pattern, is it performent?

from iterrr.

hamidb80 avatar hamidb80 commented on May 31, 2024

Use nested loops mate. You have more control

from iterrr.

hamidb80 avatar hamidb80 commented on May 31, 2024

maybe there are some hopes

from iterrr.

hamidb80 avatar hamidb80 commented on May 31, 2024

the current implementation, detects the result type by tracking maps.

implementing repeat & cycle is possible, but transformers like flatten & zip requires CORE re-implementation.

from iterrr.

hamidb80 avatar hamidb80 commented on May 31, 2024

well, I have an idea to implement this.

var s: seq[string] # let's assume that it has some value

s.items |> filter(it.len > 3) |> flatten().map[ch]($ch).filter(it != "0")

you can apply a transformer by |>.

with different syntax (compared to filter & map), it's easy to track data transformation.

from iterrr.

hamidb80 avatar hamidb80 commented on May 31, 2024

actually I'm trying to figure out a way to easily extend iterrr like mangle.

see how easy it is with closure iterators!
https://github.com/baabelfish/mangle/blob/master/mangle.nim#L143-L149

from iterrr.

hamidb80 avatar hamidb80 commented on May 31, 2024

I didn't want to use a global variable to store custom adapters, the downside is you loose private/public access control, but there is no other choice [at least I can't think of it at the time - that's what zero_functional does too]

initial states - for now take a look at:

iterrr/tests/tplay.nim

Lines 6 to 30 in 36962d8

iterator cycle(itrbl: T; `limit`: int): T {.adapter.} =
block cycleLoop:
var `c` = 0
while true:
for it in itrbl:
yield it
inc `c`
if `c` == `limit`:
break cycleLoop
iterator flatten(itrbl: T): typeof itrbl[0] {.adapter.} =
for it in itrbl:
for it in it:
yield it
iterator group(loop: T; `every`: int, `inComplete`: bool = false): seq[T] {.adapter.} =
var `gacc` = newseq[T]()
for it in loop:
`gacc`.add it
if `gacc`.len == `every`:
yield `gacc`
`gacc` = @[]
if (`gacc`.len != 0) and `inComplete`:
yield `gacc`

and see how easy it is to define a custom adapter.

these examples actually work:

iterrr/tests/tplay.nim

Lines 35 to 51 in 36962d8

let matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# single adapter
echo matrix.items !> cycle(5).toseq()
echo matrix.items !> flatten().toseq()
# chain of adapters
echo matrix.items |> flatten().cycle(10).group(2).toseq()
echo matrix.items !> flatten().cycle(10).toseq()
# :: group
echo matrix.items !> group(2).toseq()
echo matrix.items !> group(2, true).toseq()

result:

@[[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, 3], [4, 5, 6]]
@[1, 2, 3, 4, 5, 6, 7, 8, 9]
@[@[1, 2], @[3, 4], @[5, 6], @[7, 8], @[9, 1]]
@[1, 2, 3, 4, 5, 6, 7, 8, 9, 1]
@[@[[1, 2, 3], [4, 5, 6]]]
@[@[[1, 2, 3], [4, 5, 6]], @[[7, 8, 9]]]

from iterrr.

hamidb80 avatar hamidb80 commented on May 31, 2024

there are issues with adapters overloading. you can't overload a adapter with the same arity for sure, but there might be solution for overloading with different arity[number of arguments].

from iterrr.

ZoomRmc avatar ZoomRmc commented on May 31, 2024

Don't forget about group tails.

iterator group(loop: T; `every`, `wow`: int = 1): seq[T] {.adapter.} =
  var `gacc` = newSeqOfCap[T](`every`)
  for it in loop:
    `gacc`.add it
    if `gacc`.len == `every`:
      yield `gacc`
      `gacc`.setLen(0)
  if `gacc`.len > 0:
    yield `gacc`

from iterrr.

hamidb80 avatar hamidb80 commented on May 31, 2024

Don't forget about group tails.

thanks. I've added inComplete paramter.

iterrr/tests/tplay.nim

Lines 21 to 30 in 0d6577c

iterator group(loop: T; `every`: int, `inComplete`: bool = false): seq[T] {.adapter.} =
var `gacc` = newSeqOfCap[T](`every`)
for it in loop:
`gacc`.add it
if `gacc`.len == `every`:
yield `gacc`
setLen `gacc`, 0
if (`gacc`.len != 0) and `inComplete`:
yield `gacc`

UPDATE

from iterrr.

Related Issues (20)

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.