Skip to content
/ go-co Public

go-co(routine) is a source to source compiler which rewrites trival yield expression to monadic style code.

License

Notifications You must be signed in to change notification settings

goghcrow/go-co

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

𝐖𝐡𝐚𝐭 𝐢𝐬 𝕘𝕠-𝕔𝕠

𝙏𝙝𝙚 𝙊𝙡𝙙 𝙉𝙚𝙬 𝙏𝙝𝙞𝙣𝙜

golang co-routine is a source to source compiler which rewrites trival yield statements to monadic style code.

Inspired by wind-js.

Quick Start

Create source files ending with _co.go / _co_test.go.

Build tag //go:build co required.

Then go generate -tags co ./... (or run by IDE whatever).

And it is a good idea to switch custom build tag to co when working in goland or vscode, so IDE will be happy to index and check your code.

//go:build co

//go:generate go install github.com/goghcrow/go-co/cmd/cogen
//go:generate cogen

package main

import (
	. "github.com/goghcrow/go-co"
)

func Fibonacci() Iter[int] {
  a, b := 1, 1
  for {
    Yield(b)
    a, b = b, a+b
  }
}

func main() {
	for n := range Fibonacci() {
		if n > 1000 {
			println(n)
			break
		}
	}
}

Example

API

go get github.com/goghcrow/go-co@latest

package main

import (
    "github.com/goghcrow/go-co/rewriter"
    "github.com/goghcrow/go-loader"
)

func main() {
    rewriter.Compile(
        "./src",
        "./out",
        loader.WithLoadTest(),
    )
}

Control Flow Support

Rewrite control flow to monadic func invoking.

  • IfStmt
  • SwitchStmt
    • Fallthrough
  • TypeSwitchStmt
  • ForStmt
  • RangeStmt
    • string
    • slice
    • map
    • array
    • integer
    • channel
    • range func
  • BlockStmt
  • Break / Continue
    • Non-Label
    • Label
  • Goto
  • SelectStmt
  • DeferStmt

About

go-co(routine) is a source to source compiler which rewrites trival yield expression to monadic style code.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published